|
| 1 | +package clients |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "time" |
| 6 | + |
| 7 | + "oss.nandlabs.io/golly/config" |
| 8 | +) |
| 9 | + |
| 10 | +var EmptyClientOptions = &ClientOptions{ |
| 11 | + Attributes: config.NewMapAttributes(), |
| 12 | + CircuitBreaker: nil, |
| 13 | + Auth: nil, |
| 14 | + RetryPolicy: nil, |
| 15 | +} |
| 16 | + |
| 17 | +type RetryPolicy struct { |
| 18 | + MaxRetries int |
| 19 | + BackoffInterval time.Duration |
| 20 | + MaxBackoff time.Duration |
| 21 | + Exponential bool |
| 22 | +} |
| 23 | + |
| 24 | +func (r *RetryPolicy) WaitTime(retryCount int) time.Duration { |
| 25 | + backoff := r.BackoffInterval |
| 26 | + if r.Exponential { |
| 27 | + backoff = time.Duration(int64(math.Pow(2, float64(retryCount)))*backoff.Milliseconds()) * time.Millisecond |
| 28 | + backoff = min(backoff, r.MaxBackoff) |
| 29 | + } |
| 30 | + return backoff |
| 31 | +} |
| 32 | + |
| 33 | +type ClientOptions struct { |
| 34 | + // Attributes |
| 35 | + Attributes config.Attributes |
| 36 | + // RetryPolicy holds the retry configuration for the client |
| 37 | + RetryPolicy *RetryPolicy |
| 38 | + // CircuitBreaker holds the circuit breaker configuration for the client |
| 39 | + CircuitBreaker *CircuitBreaker |
| 40 | + // Auth holds the authentication configuration for the client |
| 41 | + Auth AuthProvider |
| 42 | +} |
| 43 | + |
| 44 | +type OptionsBuilder struct { |
| 45 | + options ClientOptions |
| 46 | +} |
| 47 | + |
| 48 | +func NewOptionsBuilder() *OptionsBuilder { |
| 49 | + return &OptionsBuilder{ |
| 50 | + options: ClientOptions{ |
| 51 | + Attributes: config.NewMapAttributes(), |
| 52 | + CircuitBreaker: nil, |
| 53 | + Auth: nil, |
| 54 | + RetryPolicy: nil, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// WithAtributes sets the attributes for the client. |
| 60 | +// Parameters: |
| 61 | +// - attributes: The attributes to set. |
| 62 | +// |
| 63 | +// Returns: |
| 64 | +// |
| 65 | +// *OptionsBuilder: The options builder. |
| 66 | +func (b *OptionsBuilder) WithAttributes(attributes config.Attributes) *OptionsBuilder { |
| 67 | + b.options.Attributes = attributes |
| 68 | + return b |
| 69 | +} |
| 70 | + |
| 71 | +// WithAuth sets the authenticator for the client. |
| 72 | +// Parameters: |
| 73 | +// - authenticator: The authenticator to set. |
| 74 | +// |
| 75 | +// Returns: |
| 76 | +// |
| 77 | +// *OptionsBuilder: The options builder. |
| 78 | +func (b *OptionsBuilder) WithAuth(authenticator AuthProvider) *OptionsBuilder { |
| 79 | + b.options.Auth = authenticator |
| 80 | + return b |
| 81 | +} |
| 82 | + |
| 83 | +// WithRetryPolicy sets the retry policy for the client. |
| 84 | +// Parameters: |
| 85 | +// - retryPolicy: The retry policy to set. |
| 86 | +// |
| 87 | +// Returns: |
| 88 | +// *OptionsBuilder: The options builder. |
| 89 | + |
| 90 | +func (b *OptionsBuilder) WithRetryPolicy(retryPolicy *RetryPolicy) *OptionsBuilder { |
| 91 | + b.options.RetryPolicy = retryPolicy |
| 92 | + return b |
| 93 | +} |
| 94 | + |
| 95 | +// WithCircuitBreaker sets the circuit breaker for the client. |
| 96 | +// Parameters: |
| 97 | +// - circuitBreaker: The circuit breaker to set. |
| 98 | +// |
| 99 | +// Returns: |
| 100 | +// *OptionsBuilder: The options builder. |
| 101 | + |
| 102 | +func (b *OptionsBuilder) WithCircuitBreaker(circuitBreaker *CircuitBreaker) *OptionsBuilder { |
| 103 | + b.options.CircuitBreaker = circuitBreaker |
| 104 | + return b |
| 105 | +} |
| 106 | + |
| 107 | +// AddRetryPolicy adds a retry policy to the client. |
| 108 | +// Parameters: |
| 109 | +// - maxRetries: The maximum number of retries allowed. |
| 110 | +// - backoffIntervalMs: The wait time between retries in milliseconds. If set to <=0, the client will retry immediately. |
| 111 | + |
| 112 | +func (b *OptionsBuilder) RetryPolicy(maxRetries int, backoffIntervalMs int, exponential bool, maxBackoffInMs int) *OptionsBuilder { |
| 113 | + b.options.RetryPolicy = &RetryPolicy{ |
| 114 | + MaxRetries: maxRetries, |
| 115 | + BackoffInterval: time.Duration(backoffIntervalMs) * time.Millisecond, |
| 116 | + Exponential: exponential, |
| 117 | + MaxBackoff: time.Duration(backoffIntervalMs) * time.Millisecond, |
| 118 | + } |
| 119 | + return b |
| 120 | +} |
| 121 | + |
| 122 | +// AddCircuitBreaker adds a circuit breaker to the client. |
| 123 | +// Parameters: |
| 124 | +// - failureThreshold: The number of consecutive failures required to open the circuit. |
| 125 | +// - successThreshold: The number of consecutive successes required to close the circuit. |
| 126 | +// - maxHalfOpen: The maximum number of requests allowed in the half-open state. |
| 127 | +// - timeout: The timeout duration for the circuit to transition from open to half-open state. |
| 128 | +func (b *OptionsBuilder) CircuitBreaker(failureThreshold uint64, successThreshold uint64, maxHalfOpen uint32, timeout uint32) *OptionsBuilder { |
| 129 | + breakerInfo := &BreakerInfo{ |
| 130 | + FailureThreshold: failureThreshold, |
| 131 | + SuccessThreshold: successThreshold, |
| 132 | + MaxHalfOpen: maxHalfOpen, |
| 133 | + Timeout: timeout, |
| 134 | + } |
| 135 | + b.options.CircuitBreaker = NewCircuitBreaker(breakerInfo) |
| 136 | + return b |
| 137 | +} |
| 138 | + |
| 139 | +// AddBasicAuth adds basic authentication to the client. |
| 140 | +// This method will override any existing authentication configuration. |
| 141 | +// Parameters: |
| 142 | +// - user: The username. |
| 143 | +// - pass: The password. |
| 144 | +func (b *OptionsBuilder) BasicAuth(user, pass string) *OptionsBuilder { |
| 145 | + b.options.Auth = &BasicAuth{ |
| 146 | + user: user, |
| 147 | + pass: pass, |
| 148 | + } |
| 149 | + return b |
| 150 | +} |
| 151 | + |
| 152 | +// AddTokenAuth adds token authentication to the client. |
| 153 | +// This method will override any existing authentication configuration. |
| 154 | +// Parameters: |
| 155 | +// - token: The token. |
| 156 | +func (b *OptionsBuilder) TokenBearerAuth(token string) *OptionsBuilder { |
| 157 | + b.options.Auth = &TokenBearerAuth{ |
| 158 | + token: token, |
| 159 | + } |
| 160 | + return b |
| 161 | +} |
| 162 | + |
| 163 | +// Build creates a new ClientOptions with the provided configuration. |
| 164 | +// Returns: |
| 165 | +// |
| 166 | +// *ClientOptions: The client. |
| 167 | +func (b *OptionsBuilder) Build() *ClientOptions { |
| 168 | + return &b.options |
| 169 | +} |
0 commit comments