Skip to content

Commit

Permalink
fix: handle retry for api mochi get transactions (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
baenv authored Dec 7, 2023
1 parent f2ec487 commit 0891bb8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/antchfx/xpath v1.2.3 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cloudflare/circl v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
Expand Down
41 changes: 32 additions & 9 deletions pkg/controller/discord/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/cenkalti/backoff/v4"

"github.com/dwarvesf/fortress-api/pkg/config"
"github.com/dwarvesf/fortress-api/pkg/logger"
"github.com/dwarvesf/fortress-api/pkg/model"
Expand Down Expand Up @@ -140,20 +142,41 @@ func (c *controller) PublicAdvanceSalaryLog(in model.LogDiscordInput) error {
func (c *controller) PublishIcyActivityLog() error {
logger := c.logger.Field("method", "PublishIcyActivityLog")

resp, err := c.service.MochiPay.GetListTransactions(mochipay.ListTransactionsRequest{
Status: mochipay.TransactionStatusSuccess,
ActionList: []mochipay.TransactionAction{
mochipay.TransactionActionVaultTransfer,
},
})
if err != nil {
logger.Error(err, "GetListTransactions failed")
txns := make([]mochipay.TransactionData, 0)

operation := func() error {
resp, err := c.service.MochiPay.GetListTransactions(mochipay.ListTransactionsRequest{
Size: 20,
Status: mochipay.TransactionStatusSuccess,
ActionList: []mochipay.TransactionAction{mochipay.TransactionActionVaultTransfer},
})
if err != nil {
logger.Error(err, "GetListTransactions failed")
return err
}

// reverse txns to send notification from oldest to newest
for i := len(resp.Data) - 1; i >= 0; i-- {
txns = append(txns, resp.Data[i])
}

return nil
}

// Configure backoff parameters
expBackoff := backoff.NewExponentialBackOff()
expBackoff.InitialInterval = 2 * time.Second // Initial sleep interval after an error
expBackoff.Multiplier = 2 // Next interval multiplier
expBackoff.MaxElapsedTime = 30 * time.Second // Maximum cumulative time for retries, retry 4 times = 2 + 4 + 8 + 16 = 30 seconds

if err := backoff.Retry(operation, expBackoff); err != nil {
logger.Error(err, "GetListTransactions failed after all retry")
return err
}

now := time.Now()

for _, transaction := range resp.Data {
for _, transaction := range txns {
// Just publish transaction in 3 minutes
if transaction.CreatedAt.Before(now.Add(-3 * time.Minute)) {
continue
Expand Down

0 comments on commit 0891bb8

Please sign in to comment.