mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-12-12 06:06:10 +01:00
feat: add lock retry strategy
* previouly we relied on the task being re-queued which generally reduces the throughput of tasks
This commit is contained in:
parent
f4e3aedf33
commit
ad58d1da47
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bsm/redislock"
|
||||||
"github.com/celo-org/celo-blockchain/common/hexutil"
|
"github.com/celo-org/celo-blockchain/common/hexutil"
|
||||||
"github.com/grassrootseconomics/celoutils"
|
"github.com/grassrootseconomics/celoutils"
|
||||||
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
||||||
@ -36,7 +37,9 @@ func AccountGiftGasProcessor(cu *custodial.Custodial) func(context.Context, *asy
|
|||||||
ctx,
|
ctx,
|
||||||
lockPrefix+cu.SystemContainer.PublicKey,
|
lockPrefix+cu.SystemContainer.PublicKey,
|
||||||
cu.SystemContainer.LockTimeout,
|
cu.SystemContainer.LockTimeout,
|
||||||
nil,
|
&redislock.Options{
|
||||||
|
RetryStrategy: lockRetry(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/bsm/redislock"
|
||||||
"github.com/celo-org/celo-blockchain/common/hexutil"
|
"github.com/celo-org/celo-blockchain/common/hexutil"
|
||||||
"github.com/grassrootseconomics/celoutils"
|
"github.com/grassrootseconomics/celoutils"
|
||||||
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
||||||
@ -30,7 +31,9 @@ func GiftVoucherProcessor(cu *custodial.Custodial) func(context.Context, *asynq.
|
|||||||
ctx,
|
ctx,
|
||||||
lockPrefix+cu.SystemContainer.PublicKey,
|
lockPrefix+cu.SystemContainer.PublicKey,
|
||||||
cu.SystemContainer.LockTimeout,
|
cu.SystemContainer.LockTimeout,
|
||||||
nil,
|
&redislock.Options{
|
||||||
|
RetryStrategy: lockRetry(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bsm/redislock"
|
||||||
"github.com/celo-org/celo-blockchain/common/hexutil"
|
"github.com/celo-org/celo-blockchain/common/hexutil"
|
||||||
"github.com/grassrootseconomics/celoutils"
|
"github.com/grassrootseconomics/celoutils"
|
||||||
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
||||||
@ -55,7 +56,9 @@ func AccountRefillGasProcessor(cu *custodial.Custodial) func(context.Context, *a
|
|||||||
ctx,
|
ctx,
|
||||||
lockPrefix+cu.SystemContainer.PublicKey,
|
lockPrefix+cu.SystemContainer.PublicKey,
|
||||||
cu.SystemContainer.LockTimeout,
|
cu.SystemContainer.LockTimeout,
|
||||||
nil,
|
&redislock.Options{
|
||||||
|
RetryStrategy: lockRetry(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bsm/redislock"
|
||||||
"github.com/celo-org/celo-blockchain/common/hexutil"
|
"github.com/celo-org/celo-blockchain/common/hexutil"
|
||||||
"github.com/grassrootseconomics/celoutils"
|
"github.com/grassrootseconomics/celoutils"
|
||||||
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
||||||
@ -31,7 +32,9 @@ func AccountRegisterOnChainProcessor(cu *custodial.Custodial) func(context.Conte
|
|||||||
ctx,
|
ctx,
|
||||||
lockPrefix+cu.SystemContainer.PublicKey,
|
lockPrefix+cu.SystemContainer.PublicKey,
|
||||||
cu.SystemContainer.LockTimeout,
|
cu.SystemContainer.LockTimeout,
|
||||||
nil,
|
&redislock.Options{
|
||||||
|
RetryStrategy: lockRetry(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/bsm/redislock"
|
||||||
"github.com/celo-org/celo-blockchain/common/hexutil"
|
"github.com/celo-org/celo-blockchain/common/hexutil"
|
||||||
"github.com/grassrootseconomics/celoutils"
|
"github.com/grassrootseconomics/celoutils"
|
||||||
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
||||||
@ -49,7 +50,9 @@ func SignTransfer(cu *custodial.Custodial) func(context.Context, *asynq.Task) er
|
|||||||
ctx,
|
ctx,
|
||||||
lockPrefix+payload.From,
|
lockPrefix+payload.From,
|
||||||
cu.SystemContainer.LockTimeout,
|
cu.SystemContainer.LockTimeout,
|
||||||
nil,
|
&redislock.Options{
|
||||||
|
RetryStrategy: lockRetry(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1,5 +1,21 @@
|
|||||||
package task
|
package task
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bsm/redislock"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
lockPrefix = "lock:"
|
lockPrefix = "lock:"
|
||||||
|
lockRetryDelay = 25 * time.Millisecond
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// lockRetry will at most try to obtain the lock 20 times within ~0.5s.
|
||||||
|
// it is expected to prevent immidiate requeue of the task at the expense of more redis calls.
|
||||||
|
func lockRetry() redislock.RetryStrategy {
|
||||||
|
return redislock.LimitRetry(
|
||||||
|
redislock.LinearBackoff(lockRetryDelay),
|
||||||
|
20,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user