authman.py 700 B

123456789101112131415161718192021222324
  1. import time
  2. class AuthManager:
  3. def __init__(self, auth_list, fallback=None):
  4. self.auth_list = auth_list
  5. self.blocked_until = [0 for auth in auth_list]
  6. self.cur = 0
  7. def _next(self):
  8. self.cur = (self.cur + 1) % len(self.auth_list)
  9. def get(self):
  10. for i in range(len(self.auth_list)):
  11. if self.blocked_until[self.cur] < time.time():
  12. auth = self.auth_list[self.cur]
  13. self._next()
  14. return auth
  15. self._next()
  16. if fallback is not None:
  17. return fallback()
  18. def block_last(self):
  19. self.blocked_until[(self.cur - 1) % len(self.auth_list)] = time.time() + 15