From 255718a54a83747fba890f27e6602557c3a18191 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 11 Aug 2023 19:03:16 +0800 Subject: [PATCH] deposit2transfer: apply rate limiter on checkDeposits --- pkg/strategy/deposit2transfer/strategy.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/strategy/deposit2transfer/strategy.go b/pkg/strategy/deposit2transfer/strategy.go index ac86739b0..f27529d5d 100644 --- a/pkg/strategy/deposit2transfer/strategy.go +++ b/pkg/strategy/deposit2transfer/strategy.go @@ -9,6 +9,7 @@ import ( "time" "github.com/sirupsen/logrus" + "golang.org/x/time/rate" "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" @@ -115,10 +116,11 @@ func (s *Strategy) tickWatcher(ctx context.Context, interval time.Duration) { } func (s *Strategy) checkDeposits(ctx context.Context) { + accountLimiter := rate.NewLimiter(rate.Every(3*time.Second), 1) + for _, asset := range s.Assets { log.Infof("checking %s deposits...", asset) - account := s.session.Account succeededDeposits, err := s.scanDepositHistory(ctx, asset, 4*time.Hour) if err != nil { log.WithError(err).Errorf("unable to scan deposit history") @@ -133,6 +135,17 @@ func (s *Strategy) checkDeposits(ctx context.Context) { for _, d := range succeededDeposits { log.Infof("found succeeded deposit: %+v", d) + if err2 := accountLimiter.Wait(ctx); err2 != nil { + log.WithError(err2).Errorf("rate limiter error") + return + } + + account, err2 := s.session.UpdateAccount(ctx) + if err2 != nil { + log.WithError(err2).Errorf("unable to update account") + continue + } + bal, ok := account.Balance(d.Asset) if !ok { log.Errorf("unexpected error: %s balance not found", d.Asset)