File tree Expand file tree Collapse file tree 1 file changed +14
-5
lines changed
Expand file tree Collapse file tree 1 file changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -194,31 +194,40 @@ func (c *Client) DoRequest(req *http.Request) ([]byte, error) {
194194 return nil , & ErrClientResponse {status : res .StatusCode , body : string (body )}
195195 }
196196
197+ err = c .AssignLimits (res , rateLimiter )
198+ if err != nil {
199+ return nil , err
200+ }
201+
202+ return body , nil
203+ }
204+
205+ // AssignLimits adjusts the rate limiter according to values received in response headers from the API
206+ func (c * Client ) AssignLimits (res * http.Response , rateLimiter * rate.Limiter ) error {
197207 rateHeader := res .Header .Get ("X-RateLimit-Replenish-Rate" )
198208 timeHeader := res .Header .Get ("X-RateLimit-Replenish-Time" )
199209 remainingHeader := res .Header .Get ("X-RateLimit-Remaining" )
200210
201211 if rateHeader != "" && timeHeader != "" && remainingHeader != "" {
202212 rateValue , err := strconv .Atoi (rateHeader )
203213 if err != nil {
204- return nil , err
214+ return err
205215 }
206216 timeValue , err := strconv .Atoi (timeHeader )
207217 if err != nil {
208- return nil , err
218+ return err
209219 }
210220 remainingValue , err := strconv .Atoi (remainingHeader )
211221 if err != nil {
212- return nil , err
222+ return err
213223 }
214224 if remainingValue <= 0 {
215225 remainingValue = 1
216226 }
217227 rateLimiter .SetLimit (rate .Every (time .Duration (timeValue * 1_000_000_000 / rateValue )))
218228 rateLimiter .SetBurst (remainingValue )
219229 }
220-
221- return body , nil
230+ return nil
222231}
223232
224233// GetV1Url returns the base URL for CloudConnexa API v1 endpoints.
You can’t perform that action at this time.
0 commit comments