diff --git a/.changelog/4059.internal.md b/.changelog/4059.internal.md new file mode 100644 index 00000000000..ec58f70f078 --- /dev/null +++ b/.changelog/4059.internal.md @@ -0,0 +1 @@ +go/common/backoff: Add NewExponentialBackoff with sane defaults diff --git a/go/common/backoff/backoff.go b/go/common/backoff/backoff.go new file mode 100644 index 00000000000..c9a24faa9bc --- /dev/null +++ b/go/common/backoff/backoff.go @@ -0,0 +1,11 @@ +// Package backoff contains helpers for dealing with backoffs. +package backoff + +import "github.com/cenkalti/backoff/v4" + +// NewExponentialBackOff creates an instance of ExponentialBackOff using reasonable defaults. +func NewExponentialBackOff() *backoff.ExponentialBackOff { + boff := backoff.NewExponentialBackOff() + boff.MaxElapsedTime = 0 // Make sure that the backoff never stops by default. + return boff +} diff --git a/go/consensus/api/submission.go b/go/consensus/api/submission.go index 384c53ea17b..0bddf95703c 100644 --- a/go/consensus/api/submission.go +++ b/go/consensus/api/submission.go @@ -7,6 +7,7 @@ import ( "github.com/cenkalti/backoff/v4" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" "github.com/oasisprotocol/oasis-core/go/common/errors" "github.com/oasisprotocol/oasis-core/go/common/logging" @@ -169,7 +170,7 @@ func (m *submissionManager) signAndSubmitTx(ctx context.Context, signer signatur // Implements SubmissionManager. func (m *submissionManager) SignAndSubmitTx(ctx context.Context, signer signature.Signer, tx *transaction.Transaction) error { - sched := backoff.NewExponentialBackOff() + sched := cmnBackoff.NewExponentialBackOff() sched.MaxInterval = maxSubmissionRetryInterval sched.MaxElapsedTime = maxSubmissionRetryElapsedTime diff --git a/go/runtime/client/submitter.go b/go/runtime/client/submitter.go index d6988e9cd79..8da45916f61 100644 --- a/go/runtime/client/submitter.go +++ b/go/runtime/client/submitter.go @@ -8,6 +8,7 @@ import ( "github.com/cenkalti/backoff/v4" "github.com/oasisprotocol/oasis-core/go/common" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/crypto/hash" "github.com/oasisprotocol/oasis-core/go/common/logging" "github.com/oasisprotocol/oasis-core/go/roothash/api/block" @@ -131,7 +132,7 @@ func (w *txSubmitter) checkBlocks() { // Start recheck ticker. if w.recheckTicker == nil { - boff := backoff.NewExponentialBackOff() + boff := cmnBackoff.NewExponentialBackOff() boff.InitialInterval = 5 * time.Second w.recheckTicker = backoff.NewTicker(boff) } diff --git a/go/runtime/host/sandbox/sandbox.go b/go/runtime/host/sandbox/sandbox.go index 06e0c2864cb..6a78ea7ff06 100644 --- a/go/runtime/host/sandbox/sandbox.go +++ b/go/runtime/host/sandbox/sandbox.go @@ -14,6 +14,7 @@ import ( "github.com/cenkalti/backoff/v4" "github.com/oasisprotocol/oasis-core/go/common" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/logging" "github.com/oasisprotocol/oasis-core/go/common/pubsub" "github.com/oasisprotocol/oasis-core/go/common/version" @@ -124,7 +125,7 @@ func (r *sandboxedRuntime) Call(ctx context.Context, body *protocol.Body) (rsp * } // Retry call in case the runtime is not yet ready. - err = backoff.Retry(callFn, backoff.WithContext(backoff.NewExponentialBackOff(), ctx)) + err = backoff.Retry(callFn, backoff.WithContext(cmnBackoff.NewExponentialBackOff(), ctx)) return } @@ -449,9 +450,7 @@ func (r *sandboxedRuntime) manager() { }) if ticker == nil { - boff := backoff.NewExponentialBackOff() - boff.MaxElapsedTime = 0 - ticker = backoff.NewTicker(boff) + ticker = backoff.NewTicker(cmnBackoff.NewExponentialBackOff()) tickerCh = ticker.C } continue diff --git a/go/runtime/tagindexer/tagindexer.go b/go/runtime/tagindexer/tagindexer.go index c04ff014fbc..0e7407088d4 100644 --- a/go/runtime/tagindexer/tagindexer.go +++ b/go/runtime/tagindexer/tagindexer.go @@ -8,6 +8,7 @@ import ( "github.com/cenkalti/backoff/v4" "github.com/oasisprotocol/oasis-core/go/common" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/logging" "github.com/oasisprotocol/oasis-core/go/common/service" roothash "github.com/oasisprotocol/oasis-core/go/roothash/api" @@ -71,7 +72,7 @@ func (s *Service) worker(storageBackend storage.Backend) { var txs []*transaction.Transaction var tags transaction.Tags if !blk.Header.IORoot.IsEmpty() { - off := backoff.NewExponentialBackOff() + off := cmnBackoff.NewExponentialBackOff() off.MaxElapsedTime = storageRetryTimeout err = backoff.Retry(func() error { diff --git a/go/worker/beacon/worker.go b/go/worker/beacon/worker.go index 7dbed613e66..6d2458a1a1e 100644 --- a/go/worker/beacon/worker.go +++ b/go/worker/beacon/worker.go @@ -10,6 +10,7 @@ import ( "github.com/cenkalti/backoff/v4" beacon "github.com/oasisprotocol/oasis-core/go/beacon/api" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/crypto/hash" "github.com/oasisprotocol/oasis-core/go/common/crypto/pvss" "github.com/oasisprotocol/oasis-core/go/common/identity" @@ -581,9 +582,7 @@ func (w *Worker) cancelSubmitTx() { func (w *Worker) retrySubmitTx(tx *transaction.Transaction) { ctx := w.newRetryCtx() - expOff := backoff.NewExponentialBackOff() - expOff.MaxElapsedTime = 0 - off := backoff.WithContext(expOff, ctx) + off := backoff.WithContext(cmnBackoff.NewExponentialBackOff(), ctx) fn := func() error { // Query state to make sure submitting the tx is still sensible. diff --git a/go/worker/common/p2p/dispatch.go b/go/worker/common/p2p/dispatch.go index bd19aea3b93..a11f2622a0e 100644 --- a/go/worker/common/p2p/dispatch.go +++ b/go/worker/common/p2p/dispatch.go @@ -13,6 +13,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" "github.com/oasisprotocol/oasis-core/go/common" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/cbor" "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" "github.com/oasisprotocol/oasis-core/go/common/logging" @@ -216,7 +217,7 @@ func (h *topicHandler) retryWorker(m *queuedMsg) { atomic.AddUint64(&h.numWorkers, ^uint64(0)) }() - off := backoff.WithMaxRetries(backoff.NewExponentialBackOff(), redispatchMaxRetries) + off := backoff.WithMaxRetries(cmnBackoff.NewExponentialBackOff(), redispatchMaxRetries) bctx := backoff.WithContext(off, h.ctx) err := backoff.Retry(func() error { diff --git a/go/worker/common/p2p/peermgmt.go b/go/worker/common/p2p/peermgmt.go index 8bbf4c36431..caf05237563 100644 --- a/go/worker/common/p2p/peermgmt.go +++ b/go/worker/common/p2p/peermgmt.go @@ -14,6 +14,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" manet "github.com/multiformats/go-multiaddr/net" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/crypto/hash" "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" "github.com/oasisprotocol/oasis-core/go/common/logging" @@ -307,7 +308,7 @@ func (p *p2pPeer) connectWorker(mgr *PeerManager, peerID core.PeerID) { "node_id", p.node.ID, ) - bctx := backoff.WithContext(backoff.NewExponentialBackOff(), p.ctx) + bctx := backoff.WithContext(cmnBackoff.NewExponentialBackOff(), p.ctx) err = backoff.Retry(func() (retError error) { // This is blocking, which is stupid. diff --git a/go/worker/keymanager/worker.go b/go/worker/keymanager/worker.go index ba4c76a40bd..4133a70bd7c 100644 --- a/go/worker/keymanager/worker.go +++ b/go/worker/keymanager/worker.go @@ -11,6 +11,7 @@ import ( "github.com/oasisprotocol/oasis-core/go/common" "github.com/oasisprotocol/oasis-core/go/common/accessctl" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/cbor" "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" "github.com/oasisprotocol/oasis-core/go/common/grpc/policy" @@ -187,7 +188,7 @@ func (w *Worker) updateStatus(status *api.Status, startedEvent *host.StartedEven if !initOk { // If initialization failed setup a retry ticker. if w.initTicker == nil { - w.initTicker = backoff.NewTicker(backoff.NewExponentialBackOff()) + w.initTicker = backoff.NewTicker(cmnBackoff.NewExponentialBackOff()) w.initTickerCh = w.initTicker.C } } diff --git a/go/worker/registration/worker.go b/go/worker/registration/worker.go index 75daebc12aa..da864c26108 100644 --- a/go/worker/registration/worker.go +++ b/go/worker/registration/worker.go @@ -15,6 +15,7 @@ import ( beacon "github.com/oasisprotocol/oasis-core/go/beacon/api" "github.com/oasisprotocol/oasis-core/go/common" "github.com/oasisprotocol/oasis-core/go/common/accessctl" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/common/cbor" "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" "github.com/oasisprotocol/oasis-core/go/common/entity" @@ -236,9 +237,7 @@ func (w *Worker) registrationLoop() { // nolint: gocyclo switch retry { case true: - expBackoff := backoff.NewExponentialBackOff() - expBackoff.MaxElapsedTime = 0 - off = expBackoff + off = cmnBackoff.NewExponentialBackOff() case false: off = &backoff.StopBackOff{} } diff --git a/go/worker/storage/committee/utils.go b/go/worker/storage/committee/utils.go index 0c168f4bcde..01139692b6e 100644 --- a/go/worker/storage/committee/utils.go +++ b/go/worker/storage/committee/utils.go @@ -8,6 +8,7 @@ import ( "github.com/cenkalti/backoff/v4" "github.com/oasisprotocol/oasis-core/go/common" + cmnBackoff "github.com/oasisprotocol/oasis-core/go/common/backoff" "github.com/oasisprotocol/oasis-core/go/roothash/api/block" storageApi "github.com/oasisprotocol/oasis-core/go/storage/api" ) @@ -95,9 +96,8 @@ func (h *heartbeat) reset() { h.Stop() } - boff := backoff.NewExponentialBackOff() + boff := cmnBackoff.NewExponentialBackOff() boff.InitialInterval = 5 * time.Second - boff.MaxElapsedTime = 0 boff.MaxInterval = 20 * time.Second h.Ticker = backoff.NewTicker(boff)