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 f29a4be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/strategy/dca2/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (s *Strategy) runTakeProfitReady(ctx context.Context, next State) {
// reset position

// calculate profit stats
if err := s.mustCalculateAndEmitProfit(ctx); err != nil {
if err := s.CalculateAndEmitProfitUntilSuccessful(ctx); err != nil {
s.logger.WithError(err).Warn("failed to calculate and emit profit")
}

Expand Down
19 changes: 8 additions & 11 deletions pkg/strategy/dca2/strategy.go
Original file line number Diff line number Diff line change
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 f29a4be

Please sign in to comment.