From a449e2b20ee39e33057d3138cb3c5320a727252e Mon Sep 17 00:00:00 2001 From: Marcus Watkins Date: Mon, 13 Nov 2023 15:10:21 -0700 Subject: [PATCH] Place a floor of 1 on the rate limit burst to prevent x/time from throwing an error. If GitLab is enforcing rate limits of fewer than 3 per second the existing logic will set burst to zero (it multiples the per-second value by 0.33 and then casts to int). rate.Limiter.Wait will fail if burst is zero with the message here: https://github.com/golang/time/blob/b24d3b5e50f7b0e18486d18f0a240d04d254ea73/rate/rate.go#L257 As Wait falls back to WaitN with n=1: https://github.com/golang/time/blob/b24d3b5e50f7b0e18486d18f0a240d04d254ea73/rate/rate.go#L231 --- gitlab.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gitlab.go b/gitlab.go index 0ed07f2df..08e10881b 100644 --- a/gitlab.go +++ b/gitlab.go @@ -533,6 +533,11 @@ func (c *Client) configureLimiter(ctx context.Context, headers http.Header) { limit := rate.Limit(rateLimit * 0.66) burst := int(rateLimit * 0.33) + // Need at least one allowed to burst or x/time will throw an error + if burst == 0 { + burst = 1 + } + // Create a new limiter using the calculated values. c.limiter = rate.NewLimiter(limit, burst)