Skip to content

Commit 4046d2b

Browse files
kalkinBahtiar Gadimov
authored andcommitted
fix: GitHub provider handle rate limiting
1 parent 28adf77 commit 4046d2b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

glv/providers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pathlib
77
import re
88
import sys
9+
from time import time
910
from typing import Any, Optional, Tuple
1011

1112
import certifi
@@ -74,6 +75,7 @@ def authorization(self) -> Optional[Tuple[str, str]]:
7475

7576
class GitHub(Provider):
7677
def __init__(self, repo, cache_dir: str) -> None:
78+
self._rate_limit: Optional[int] = None
7779
file_path = os.path.join(cache_dir, 'github.json')
7880
super().__init__(repo, Cache(file_path))
7981

@@ -111,9 +113,13 @@ def provide(self, subject: str) -> str:
111113
try:
112114
return self._cache[subject]
113115
except KeyError:
114-
if self.auth_failed:
116+
if (self._rate_limit and self._rate_limit >= time()) \
117+
or self.auth_failed:
115118
return subject
116119

120+
if self._rate_limit:
121+
self._rate_limit = None
122+
117123
results = self.pattern.search(subject)
118124
if results:
119125
_id = results.group(1)
@@ -130,6 +136,10 @@ def provide(self, subject: str) -> str:
130136
print('Failed to authenticate', file=sys.stderr)
131137
self.auth_failed = True
132138
return subject
139+
elif request.status == 403:
140+
self._rate_limit = int(
141+
request.headers['X-Ratelimit-Reset'])
142+
return subject
133143
else:
134144
print(request.data, file=sys.stderr)
135145
return subject

0 commit comments

Comments
 (0)