Skip to content

Commit

Permalink
Retrier improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Baranov committed Sep 16, 2024
1 parent e831a18 commit 0774432
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 0 additions & 1 deletion adnl/adnl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func TestADNL_ClientServer(t *testing.T) {
case MessagePing:
if m.Value == 9999 {
client.Close()
println("DISCON")
return fmt.Errorf("handle mock err")
}

Expand Down
21 changes: 20 additions & 1 deletion ton/retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ type retryClient struct {
}

func (w *retryClient) QueryLiteserver(ctx context.Context, payload tl.Serializable, result tl.Serializable) error {
tries := 0
const maxRounds = 2

tries, rounds := 0, 0
ctxBackup := ctx

for {
err := w.original.QueryLiteserver(ctx, payload, result)
if w.maxRetries > 0 && tries >= w.maxRetries {
Expand All @@ -37,6 +41,13 @@ func (w *retryClient) QueryLiteserver(ctx context.Context, payload tl.Serializab
// try next node
ctx, err = w.original.StickyContextNextNode(ctx)
if err != nil {
rounds++
if rounds < maxRounds {
// try same nodes one more time
ctx = ctxBackup
continue
}

return fmt.Errorf("timeout error received, but failed to try with next node, "+
"looks like all active nodes was already tried, original error: %w", err)
}
Expand All @@ -50,7 +61,15 @@ func (w *retryClient) QueryLiteserver(ctx context.Context, payload tl.Serializab
lsErr.Code == -400 ||
lsErr.Code == -503 ||
(lsErr.Code == 0 && strings.Contains(lsErr.Text, "Failed to get account state"))) {

if ctx, err = w.original.StickyContextNextNode(ctx); err != nil { // try next node
rounds++
if rounds < maxRounds {
// try same nodes one more time
ctx = ctxBackup
continue
}

// no more nodes left, return as it is
return nil
}
Expand Down
5 changes: 1 addition & 4 deletions ton/wallet/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build integration
// +build integration

package wallet

import (
Expand Down Expand Up @@ -249,7 +246,7 @@ func Test_WalletFindTransactionByInMsgHash(t *testing.T) {
}

// find tx hash
tx, err := w.api.FindLastTransactionByInMsgHash(ctx, inMsgHash, 30)
tx, err := w.api.FindLastTransactionByInMsgHash(ctx, w.addr, inMsgHash, 30)
if err != nil {
t.Fatal("cannot find tx:", err.Error())
}
Expand Down

0 comments on commit 0774432

Please sign in to comment.