github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/siatest/retry.go (about)

     1  package siatest
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // Retry will call 'fn' 'tries' times, waiting 'durationBetweenAttempts'
     8  // between each attempt, returning 'nil' the first time that 'fn' returns nil.
     9  // If 'nil' is never returned, then the final error returned by 'fn' is
    10  // returned.
    11  func Retry(tries int, durationBetweenAttempts time.Duration, fn func() error) (err error) {
    12  	for i := 1; i < tries; i++ {
    13  		err = fn()
    14  		if err == nil {
    15  			return nil
    16  		}
    17  		time.Sleep(durationBetweenAttempts)
    18  	}
    19  	return fn()
    20  }