Skip to content

Commit 7b5bee7

Browse files
committed
use backoff retry
1 parent f19eceb commit 7b5bee7

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

pkg/strategy/dca2/strategy.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99
"time"
1010

11+
"github.com/pkg/errors"
1112
"github.com/prometheus/client_golang/prometheus"
1213
"github.com/sirupsen/logrus"
1314
"go.uber.org/multierr"
@@ -372,7 +373,7 @@ func (s *Strategy) CleanUp(ctx context.Context) error {
372373
return werr
373374
}
374375

375-
func (s *Strategy) mustCalculateAndEmitProfit(ctx context.Context) error {
376+
func (s *Strategy) CalculateAndEmitProfitUntilSuccessful(ctx context.Context) error {
376377
fromOrderID := s.ProfitStats.FromOrderID
377378

378379
historyService, ok := s.ExchangeSession.Exchange.(types.ExchangeTradeHistoryService)
@@ -385,23 +386,19 @@ func (s *Strategy) mustCalculateAndEmitProfit(ctx context.Context) error {
385386
return fmt.Errorf("exchange %s doesn't support ExchangeOrderQueryService", s.ExchangeSession.Exchange.Name())
386387
}
387388

388-
maxTry := 10
389-
for try := 1; try < maxTry; try++ {
389+
var op = func() error {
390390
if err := s.CalculateAndEmitProfit(ctx, historyService, queryService); err != nil {
391-
s.logger.WithError(err).Warnf("failed to calculate and emit profit at #%d try, please check it", try)
392-
continue
391+
return errors.Wrapf(err, "failed to calculate and emit profit, please check it")
393392
}
394393

395-
if s.ProfitStats.FromOrderID > fromOrderID {
396-
break
394+
if s.ProfitStats.FromOrderID == fromOrderID {
395+
return fmt.Errorf("FromOrderID (%d) is not updated, retry it", s.ProfitStats.FromOrderID)
397396
}
398-
}
399397

400-
if s.ProfitStats.FromOrderID == fromOrderID {
401-
return fmt.Errorf("after trying %d times, we still can't calculate and emit profit, please check it", maxTry)
398+
return nil
402399
}
403400

404-
return nil
401+
return retry.GeneralLiteBackoff(ctx, op)
405402
}
406403

407404
func (s *Strategy) CalculateAndEmitProfit(ctx context.Context, historyService types.ExchangeTradeHistoryService, queryService types.ExchangeOrderQueryService) error {

0 commit comments

Comments
 (0)