Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
199 lines (170 loc) · 6.44 KB

File metadata and controls

199 lines (170 loc) · 6.44 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import logging
import os
import threading
import time
from datetime import datetime
from functools import partial
from typing import List
import github
from github import Github, RateLimitExceededException
class RateLimiter:
"""
Rate Limiter for the Github API
"""
def __init__(self, requests_limit: int, reset_seconds: int):
self.requests = 0
self.requests_limit = requests_limit
self.reset_seconds = reset_seconds
self.first_request = datetime.now()
self.lock = threading.Lock()
def request(self, fn, *args, **kwargs):
with self.lock:
time_after_reset = (datetime.now() - self.first_request).total_seconds()
retries = 3
if self.requests == 0:
self.first_request = datetime.now()
elif time_after_reset > self.reset_seconds:
self.requests = 0
self.first_request = datetime.now()
if self.requests == self.requests_limit:
time.sleep(self.reset_seconds - time_after_reset)
self.requests = 0
self.requests += 1
while retries > 0:
try:
return fn(*args, **kwargs)
except RateLimitExceededException as exc:
with self.lock:
logging.warning(f"Github Rate Limit Exceeded: {exc.headers}")
reset_time = datetime.fromtimestamp(
int(exc.headers["x-ratelimit-reset"])
)
retry_after = (reset_time - datetime.now()).total_seconds() + 1
retry_after = max(
retry_after, 0
) # In case we hit a negative total_seconds
time.sleep(retry_after)
retries -= 1
if retries == 0:
raise exc
def update_requests(self, requests: int):
with self.lock:
self.requests = requests
class SearchRateLimiter(RateLimiter):
"""
Rate Limiter for the Github search API. The search API has different rate limits
than the core API.
"""
def __init__(self):
super().__init__(
# The real limit is 30, but we try to avoid it
requests_limit=29,
reset_seconds=60,
)
class CoreRateLimiter(RateLimiter):
"""
Rate Limiter for the Github core API.
"""
def __init__(self):
super().__init__(
# The real limit is 5000, but we try to avoid it
requests_limit=4995,
reset_seconds=3600,
)
class GithubToken:
__TOKENS: List["GithubToken"] = None
__TOKENS_LOCK: threading.Lock = threading.Lock()
__CURRENT_TOKEN = 0
__OFFSET = 200
__UPDATE_RATE_INTERVAL = 5 # in seconds
def __init__(self, token: str):
self.lock_rate: threading.Lock = threading.Lock()
self.last_update: float = 0
self.token: str = token
self.search_rate_limiter = SearchRateLimiter()
self.core_rate_limiter = CoreRateLimiter()
GithubToken.__TOKENS.append(self)
self.github = GithubAPI(token=self)
def update_rate_limit(self):
with self.lock_rate:
if time.time() - self.last_update > GithubToken.__UPDATE_RATE_INTERVAL:
rate_limit = self.github.get_rate_limit()
self.search_rate_limiter.update_requests(
rate_limit.search.limit - rate_limit.search.remaining
)
self.core_rate_limiter.update_requests(
rate_limit.core.limit - rate_limit.core.remaining
)
@staticmethod
def has_tokens() -> bool:
return "GITHUB_ACCESS_TOKEN" in os.environ
@staticmethod
def init_tokens():
if GithubToken.has_tokens():
GithubToken.__TOKENS = []
tokens = os.environ["GITHUB_ACCESS_TOKEN"].split(",")
for token in tokens:
GithubToken(token)
else:
logging.error("No environment variable GITHUB_ACCESS_TOKEN provided.")
exit(1)
@staticmethod
def __wait_for_tokens():
if len(GithubToken.__TOKENS) == 0:
return
soonest_reset = GithubToken.__TOKENS[0].github.get_rate_limit().core.reset
for token in GithubToken.__TOKENS[1:]:
reset = token.github.get_rate_limit().core.reset
if reset < soonest_reset:
soonest_reset = reset
time.sleep((datetime.now() - soonest_reset).total_seconds())
@staticmethod
def get_token() -> "GithubToken":
with GithubToken.__TOKENS_LOCK:
if GithubToken.__TOKENS is None:
GithubToken.init_tokens()
len_tokens = (
0 if not GithubToken.has_tokens() else len(GithubToken.__TOKENS)
)
if len_tokens == 0:
return None
next_tokens = (
GithubToken.__TOKENS[GithubToken.__CURRENT_TOKEN :]
+ GithubToken.__TOKENS[: GithubToken.__CURRENT_TOKEN]
)
for token in next_tokens:
GithubToken.__CURRENT_TOKEN = (
GithubToken.__CURRENT_TOKEN + 1
) % len_tokens
if (
token.core_rate_limiter.requests_limit
- token.core_rate_limiter.requests
>= GithubToken.__OFFSET
):
return token
GithubToken.__wait_for_tokens()
class GithubAPI(Github):
def __init__(self, *args, token: GithubToken = None, **kwargs):
if "auth" not in kwargs:
self.token = GithubToken.get_token() if token is None else token
if self.token is not None:
kwargs["auth"] = github.Auth.Token(self.token.token)
else:
super().__init__(*args, **kwargs)
return
super().__init__(*args, **kwargs)
for attr, val in Github.__dict__.items():
if attr.startswith("_") or not callable(val):
continue
if attr.startswith("search"):
setattr(
self,
attr,
partial(self.token.search_rate_limiter.request, partial(val, self)),
)
else:
setattr(
self,
attr,
partial(self.token.core_rate_limiter.request, partial(val, self)),
)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.