Skip to content

Commit

Permalink
use backoff retry
Browse files Browse the repository at this point in the history
  • Loading branch information
kbearXD committed Mar 14, 2024
1 parent f19eceb commit 7b5bee7
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions pkg/strategy/dca2/strategy.go
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.uber.org/multierr"
Expand Down Expand Up @@ -372,7 +373,7 @@ func (s *Strategy) CleanUp(ctx context.Context) error {
return werr
}

func (s *Strategy) mustCalculateAndEmitProfit(ctx context.Context) error {
func (s *Strategy) CalculateAndEmitProfitUntilSuccessful(ctx context.Context) error {
fromOrderID := s.ProfitStats.FromOrderID

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

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

if s.ProfitStats.FromOrderID > fromOrderID {
break
if s.ProfitStats.FromOrderID == fromOrderID {
return fmt.Errorf("FromOrderID (%d) is not updated, retry it", s.ProfitStats.FromOrderID)
}
}

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

return nil
return retry.GeneralLiteBackoff(ctx, op)
}

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

0 comments on commit 7b5bee7

Please sign in to comment.