Moonfanz commited on
Commit
8278da4
·
verified ·
1 Parent(s): 386e12e

Upload 4 files

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -144,7 +144,12 @@ def is_within_rate_limit(api_key):
144
  while request_counts[api_key] and request_counts[api_key][0] < now - timedelta(seconds=LIMIT_WINDOW):
145
  request_counts[api_key].popleft()
146
 
147
- return len(request_counts[api_key]) < MAX_REQUESTS
 
 
 
 
 
148
 
149
  def increment_request_count(api_key):
150
  now = datetime.now()
@@ -211,8 +216,9 @@ def chat_completions():
211
  return jsonify(error_response), 400
212
 
213
  def do_request(current_api_key, attempt):
214
- if not is_within_rate_limit(current_api_key):
215
- logger.warning(f"{current_api_key[:11]} 暂时超过限额")
 
216
  switch_api_key()
217
  return False, None
218
 
 
144
  while request_counts[api_key] and request_counts[api_key][0] < now - timedelta(seconds=LIMIT_WINDOW):
145
  request_counts[api_key].popleft()
146
 
147
+ if len(request_counts[api_key]) >= MAX_REQUESTS:
148
+ earliest_request_time = request_counts[api_key][0]
149
+ wait_time = (earliest_request_time + timedelta(seconds=LIMIT_WINDOW)) - now
150
+ return False, wait_time.total_seconds()
151
+ else:
152
+ return True, 0
153
 
154
  def increment_request_count(api_key):
155
  now = datetime.now()
 
216
  return jsonify(error_response), 400
217
 
218
  def do_request(current_api_key, attempt):
219
+ isok, time = is_within_rate_limit(current_api_key)
220
+ if not isok:
221
+ logger.warning(f"{current_api_key[:11]} → 暂时超过限额,请在 {time} 秒后重试...")
222
  switch_api_key()
223
  return False, None
224