package req

import (
	
	
	
)

func defaultGetRetryInterval( *Response,  int) time.Duration {
	return 100 * time.Millisecond
}

// RetryConditionFunc is a retry condition, which determines
// whether the request should retry.
type RetryConditionFunc func(resp *Response, err error) bool

// RetryHookFunc is a retry hook which will be executed before a retry.
type RetryHookFunc func(resp *Response, err error)

// GetRetryIntervalFunc is a function that determines how long should
// sleep between retry attempts.
type GetRetryIntervalFunc func(resp *Response, attempt int) time.Duration

func backoffInterval(,  time.Duration) GetRetryIntervalFunc {
	 := float64()
	 := float64()
	return func( *Response,  int) time.Duration {
		 := math.Min(, *math.Exp2(float64()))
		 := int64( / 2)
		 :=  + rand.Int63n()
		return time.Duration()
	}
}

func newDefaultRetryOption() *RetryOption {
	return &RetryOption{
		GetRetryInterval: defaultGetRetryInterval,
	}
}

// RetryOption controls the retry behavior of a request.
// It is typically configured via Client.SetCommonRetry* or Request.SetRetry*
// methods and can be read from middleware with Request.GetRetryOption.
//
// MaxRetries is the maximum number of retries (not including the initial
// attempt). A negative value means retry infinitely. Zero means no retries.
// GetRetryOption may still return a non-nil option if only non-count setters
// (interval, condition, or hook) were used while leaving MaxRetries at zero.
type RetryOption struct {
	MaxRetries       int
	GetRetryInterval GetRetryIntervalFunc
	RetryConditions  []RetryConditionFunc
	RetryHooks       []RetryHookFunc
}

// Clone returns a deep copy of RetryOption.
func ( *RetryOption) () *RetryOption {
	if  == nil {
		return nil
	}
	 := &RetryOption{
		MaxRetries:       .MaxRetries,
		GetRetryInterval: .GetRetryInterval,
	}
	.RetryConditions = append(.RetryConditions, .RetryConditions...)
	.RetryHooks = append(.RetryHooks, .RetryHooks...)
	return 
}