From f2d5e32bc6cd4ffbe95819b6b5455f7bd6c7454a Mon Sep 17 00:00:00 2001 From: zhiqiangxu <652732310@qq.com> Date: Mon, 30 Sep 2024 07:48:16 +0800 Subject: [PATCH] add `retry.Do0` (#12194) * add retry.Do0 * update code to use Do0 --- op-node/node/conductor.go | 5 ++--- op-node/node/node.go | 6 +++--- op-service/retry/operation.go | 23 ++++++++++++++++++----- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/op-node/node/conductor.go b/op-node/node/conductor.go index 93bde641a453..ff5723889b95 100644 --- a/op-node/node/conductor.go +++ b/op-node/node/conductor.go @@ -91,12 +91,11 @@ func (c *ConductorClient) CommitUnsafePayload(ctx context.Context, payload *eth. ctx, cancel := context.WithTimeout(ctx, c.cfg.ConductorRpcTimeout) defer cancel() - // extra bool return value is required for the generic, can be ignored. - _, err := retry.Do(ctx, 2, retry.Fixed(50*time.Millisecond), func() (bool, error) { + err := retry.Do0(ctx, 2, retry.Fixed(50*time.Millisecond), func() error { record := c.metrics.RecordRPCClientRequest("conductor_commitUnsafePayload") err := c.apiClient.CommitUnsafePayload(ctx, payload) record(err) - return true, err + return err }) return err } diff --git a/op-node/node/node.go b/op-node/node/node.go index 9d9f6a4343ac..298c98aa2b18 100644 --- a/op-node/node/node.go +++ b/op-node/node/node.go @@ -262,12 +262,12 @@ func (n *OpNode) initRuntimeConfig(ctx context.Context, cfg *Config) error { } // initialize the runtime config before unblocking - if _, err := retry.Do(ctx, 5, retry.Fixed(time.Second*10), func() (eth.L1BlockRef, error) { - ref, err := reload(ctx) + if err := retry.Do0(ctx, 5, retry.Fixed(time.Second*10), func() error { + _, err := reload(ctx) if errors.Is(err, errNodeHalt) { // don't retry on halt error err = nil } - return ref, err + return err }); err != nil { return fmt.Errorf("failed to load runtime configuration repeatedly, last error: %w", err) } diff --git a/op-service/retry/operation.go b/op-service/retry/operation.go index 4f0142cde946..95925296811d 100644 --- a/op-service/retry/operation.go +++ b/op-service/retry/operation.go @@ -40,25 +40,38 @@ func Do2[T, U any](ctx context.Context, maxAttempts int, strategy Strategy, op f // Strategy. func Do[T any](ctx context.Context, maxAttempts int, strategy Strategy, op func() (T, error)) (T, error) { var empty, ret T + f := func() (err error) { + ret, err = op() + return + } + err := Do0(ctx, maxAttempts, strategy, f) + if err != nil { + return empty, err + } + return ret, err +} + +// Do0 is similar to Do and Do2, execept that `op` only returns an error +func Do0(ctx context.Context, maxAttempts int, strategy Strategy, op func() error) error { var err error if maxAttempts < 1 { - return empty, fmt.Errorf("need at least 1 attempt to run op, but have %d max attempts", maxAttempts) + return fmt.Errorf("need at least 1 attempt to run op, but have %d max attempts", maxAttempts) } for i := 0; i < maxAttempts; i++ { if ctx.Err() != nil { - return empty, ctx.Err() + return ctx.Err() } - ret, err = op() + err = op() if err == nil { - return ret, nil + return nil } // Don't sleep when we are about to exit the loop & return ErrFailedPermanently if i != maxAttempts-1 { time.Sleep(strategy.Duration(i)) } } - return empty, &ErrFailedPermanently{ + return &ErrFailedPermanently{ attempts: maxAttempts, LastErr: err, }