From 40601463c78a6f5d45081700164899b2559b7e55 Mon Sep 17 00:00:00 2001
From: "John L. Villalovos" <john@sodarock.com>
Date: Wed, 16 Feb 2022 17:25:16 -0800
Subject: [PATCH] fix: support RateLimit-Reset header

Some endpoints are not returning the `Retry-After` header when
rate-limiting occurrs. In those cases use the `RateLimit-Reset` [1]
header, if available.

Closes: #1889

[1] https://docs.gitlab.com/ee/user/admin_area/settings/user_and_ip_rate_limits.html#response-headers
---
 gitlab/client.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/gitlab/client.py b/gitlab/client.py
index 9d1eebdd9..d61915a4b 100644
--- a/gitlab/client.py
+++ b/gitlab/client.py
@@ -700,10 +700,14 @@ def http_request(
             if (429 == result.status_code and obey_rate_limit) or (
                 result.status_code in [500, 502, 503, 504] and retry_transient_errors
             ):
+                # Response headers documentation:
+                # https://docs.gitlab.com/ee/user/admin_area/settings/user_and_ip_rate_limits.html#response-headers
                 if max_retries == -1 or cur_retries < max_retries:
                     wait_time = 2 ** cur_retries * 0.1
                     if "Retry-After" in result.headers:
                         wait_time = int(result.headers["Retry-After"])
+                    elif "RateLimit-Reset" in result.headers:
+                        wait_time = int(result.headers["RateLimit-Reset"]) - time.time()
                     cur_retries += 1
                     time.sleep(wait_time)
                     continue