github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/requester/config.go (about) 1 package requester 2 3 import ( 4 "math" 5 "time" 6 ) 7 8 type Config struct { 9 BatchInterval time.Duration // minimum interval between requests 10 BatchThreshold uint // maximum batch size for one request 11 RetryInitial time.Duration // interval after which we retry request for an entity 12 RetryFunction RetryFunc // function determining growth of retry interval 13 RetryMaximum time.Duration // maximum interval for retrying request for an entity 14 RetryAttempts uint // maximum amount of request attempts per entity 15 ValidateStaking bool // should staking of target/origin be checked 16 } 17 18 type RetryFunc func(time.Duration) time.Duration 19 20 func RetryConstant() RetryFunc { 21 return func(interval time.Duration) time.Duration { 22 return interval 23 } 24 } 25 26 func RetryLinear(increase time.Duration) RetryFunc { 27 return func(interval time.Duration) time.Duration { 28 return interval + increase 29 } 30 } 31 32 func RetryGeometric(factor float64) RetryFunc { 33 return func(interval time.Duration) time.Duration { 34 return time.Duration(float64(interval) * factor) 35 } 36 } 37 38 func RetryExponential(exponent float64) RetryFunc { 39 return func(interval time.Duration) time.Duration { 40 return time.Duration(math.Pow(float64(interval), exponent)) 41 } 42 } 43 44 type OptionFunc func(*Config) 45 46 // WithBatchInterval sets a custom interval at which we scan for pending items 47 // and batch them for requesting. 48 func WithBatchInterval(interval time.Duration) OptionFunc { 49 return func(cfg *Config) { 50 cfg.BatchInterval = interval 51 } 52 } 53 54 // WithBatchThreshold sets a custom threshold for the maximum size of a batch. 55 // If we have the given amount of pending items, we immediately send a batch. 56 func WithBatchThreshold(threshold uint) OptionFunc { 57 return func(cfg *Config) { 58 cfg.BatchThreshold = threshold 59 } 60 } 61 62 // WithRetryInitial sets the initial interval for dispatching a request for the 63 // second time. 64 func WithRetryInitial(interval time.Duration) OptionFunc { 65 return func(cfg *Config) { 66 cfg.RetryInitial = interval 67 } 68 } 69 70 // WithRetryFunction sets the function at which the retry interval increases. 71 func WithRetryFunction(retry RetryFunc) OptionFunc { 72 return func(cfg *Config) { 73 cfg.RetryFunction = retry 74 } 75 } 76 77 // WithRetryMaximum sets the maximum retry interval at which we will retry. 78 func WithRetryMaximum(interval time.Duration) OptionFunc { 79 return func(cfg *Config) { 80 cfg.RetryMaximum = interval 81 } 82 } 83 84 // WithRetryAttempts sets the number of attempts we will make before we give 85 // up on retrying. Use zero for infinite retries. 86 func WithRetryAttempts(attempts uint) OptionFunc { 87 return func(cfg *Config) { 88 cfg.RetryAttempts = attempts 89 } 90 } 91 92 // WithValidateStaking sets the flag which determines if the target and origin must be checked for staking 93 func WithValidateStaking(validateStaking bool) OptionFunc { 94 return func(cfg *Config) { 95 cfg.ValidateStaking = validateStaking 96 } 97 }