Skip to content

Commit

Permalink
go/common/backoff: Add NewExponentialBackoff with sane defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Jun 21, 2021
1 parent 8d3b1dd commit 6d8c056
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions .changelog/4059.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/common/backoff: Add NewExponentialBackoff with sane defaults
11 changes: 11 additions & 0 deletions go/common/backoff/backoff.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion go/consensus/api/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion go/runtime/client/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 3 additions & 4 deletions go/runtime/host/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion go/runtime/tagindexer/tagindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions go/worker/beacon/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion go/worker/common/p2p/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion go/worker/common/p2p/peermgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion go/worker/keymanager/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand Down
5 changes: 2 additions & 3 deletions go/worker/registration/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
}
Expand Down
4 changes: 2 additions & 2 deletions go/worker/storage/committee/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 6d8c056

Please sign in to comment.