github.com/bcskill/bcschain/v3@v3.4.9-beta2/core/rawdb/retry.go (about)

     1  package rawdb
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/bcskill/bcschain/v3/common"
     8  	"github.com/bcskill/bcschain/v3/log"
     9  )
    10  
    11  const (
    12  	initialDelay = 1 * time.Second
    13  	maxDelay     = 30 * time.Second
    14  )
    15  
    16  // Must executes fn, repeatedly retrying (with increasing delay) until it returns nil. op describes the operation for
    17  // error logging.
    18  func Must(op string, fn func() error) {
    19  	err := fn()
    20  	if err == nil {
    21  		return
    22  	}
    23  	start := time.Now()
    24  	cnt := 1
    25  	delay := initialDelay
    26  	msg := fmt.Sprintf("Failed to %q; retrying after delay", op)
    27  	for err != nil {
    28  		log.Error(msg, "attempt", cnt, "delay", common.PrettyDuration(delay), "err", err)
    29  		cnt++
    30  		delay *= 2
    31  		if delay > maxDelay {
    32  			delay = maxDelay
    33  		}
    34  		time.Sleep(delay)
    35  		err = fn()
    36  	}
    37  	log.Info(fmt.Sprintf("Successful %q after retrying", op), "attempts", cnt, "dur", common.PrettyDuration(time.Since(start)))
    38  }