Skip to content

Commit

Permalink
feat: configurable retry after duration
Browse files Browse the repository at this point in the history
  • Loading branch information
hallazzang committed Nov 5, 2024
1 parent 038f7cb commit 0f8fc5e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/opinitd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

const (
flagPollingInterval = "polling-interval"
flagRetryAfter = "retry-after"
)

func startCmd(ctx *cmdContext) *cobra.Command {
Expand Down Expand Up @@ -56,6 +57,11 @@ Currently supported bots:
return err
}
ctx = types.WithPollingInterval(ctx, interval)
retryAfter, err := cmd.Flags().GetDuration(flagRetryAfter)
if err != nil {
return err
}
ctx = types.WithRetryAfter(ctx, retryAfter)
err = bot.Initialize(ctx)
if err != nil {
return err
Expand All @@ -65,7 +71,8 @@ Currently supported bots:
}

cmd = configFlag(ctx.v, cmd)
cmd.Flags().Duration(flagPollingInterval, time.Second, "Polling interval in milliseconds")
cmd.Flags().Duration(flagPollingInterval, 100*time.Millisecond, "Polling interval")
cmd.Flags().Duration(flagRetryAfter, time.Minute, "Waiting time for a retry")
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion node/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (n *Node) blockProcessLooper(ctx context.Context, processType nodetypes.Blo
if err != nil {
n.logger.Error("failed to handle new block", zap.String("error", err.Error()))
if errors.Is(err, nodetypes.ErrIgnoreAndTryLater) {
sleep := time.NewTimer(time.Minute)
sleep := time.NewTimer(types.RetryAfter(ctx))
select {
case <-ctx.Done():
return nil
Expand Down
15 changes: 14 additions & 1 deletion types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type contextKey string
var (
ContextKeyErrGrp = contextKey("ErrGrp")
ContextKeyPollingInterval = contextKey("PollingInterval")
ContextKeyRetryAfter = contextKey("RetryAfter")
ContextKeyTxTimeout = contextKey("TxTimeout")
)

Expand All @@ -32,5 +33,17 @@ func PollingInterval(ctx context.Context) time.Duration {
if interval == nil {
return 100 * time.Millisecond
}
return ctx.Value(ContextKeyPollingInterval).(time.Duration)
return interval.(time.Duration)
}

func WithRetryAfter(ctx context.Context, duration time.Duration) context.Context {
return context.WithValue(ctx, ContextKeyRetryAfter, duration)
}

func RetryAfter(ctx context.Context) time.Duration {
duration := ctx.Value(ContextKeyRetryAfter)
if duration == nil {
return time.Minute
}
return duration.(time.Duration)
}

0 comments on commit 0f8fc5e

Please sign in to comment.