Compare commits
3 commits
79460e4998
...
0a71012709
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a71012709 | |||
| 6dd26ad3d8 | |||
| 0af7179596 |
4 changed files with 14 additions and 8 deletions
|
|
@ -71,8 +71,9 @@ class Provider(ABC):
|
||||||
"""Usage percent when provider may switch active account/token."""
|
"""Usage percent when provider may switch active account/token."""
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def should_prepare_standby(self) -> bool:
|
def should_prepare_standby(self, usage_percent: int) -> bool:
|
||||||
"""Whether standby preparation should be triggered for current usage."""
|
"""Whether standby preparation should be triggered for current usage."""
|
||||||
|
_ = usage_percent
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def ensure_standby_account(
|
async def ensure_standby_account(
|
||||||
|
|
|
||||||
|
|
@ -122,8 +122,8 @@ class ChatGPTProvider(Provider):
|
||||||
return True
|
return True
|
||||||
return await self._create_next_account_under_lock()
|
return await self._create_next_account_under_lock()
|
||||||
|
|
||||||
def should_prepare_standby(self) -> bool:
|
def should_prepare_standby(self, usage_percent: int) -> bool:
|
||||||
return bool(load_next_tokens())
|
return usage_percent >= self.prepare_threshold and not bool(load_next_tokens())
|
||||||
|
|
||||||
async def ensure_standby_account(
|
async def ensure_standby_account(
|
||||||
self,
|
self,
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ def should_trigger_standby_prepare(provider_name: str, usage_percent: int) -> bo
|
||||||
provider = PROVIDERS.get(provider_name)
|
provider = PROVIDERS.get(provider_name)
|
||||||
if not provider:
|
if not provider:
|
||||||
return False
|
return False
|
||||||
return provider.should_prepare_standby()
|
return provider.should_prepare_standby(usage_percent)
|
||||||
|
|
||||||
|
|
||||||
async def ensure_provider_token_ready(provider_name: str):
|
async def ensure_provider_token_ready(provider_name: str):
|
||||||
|
|
@ -88,7 +88,7 @@ async def ensure_standby_task(provider_name: str, usage_percent: int, reason: st
|
||||||
if not provider:
|
if not provider:
|
||||||
return
|
return
|
||||||
|
|
||||||
if not provider.should_prepare_standby():
|
if not provider.should_prepare_standby(usage_percent):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -212,6 +212,7 @@ async def on_cleanup(app: web.Application):
|
||||||
|
|
||||||
|
|
||||||
def create_app() -> web.Application:
|
def create_app() -> web.Application:
|
||||||
|
app = web.Application()
|
||||||
app.on_startup.append(on_startup)
|
app.on_startup.append(on_startup)
|
||||||
app.on_cleanup.append(on_cleanup)
|
app.on_cleanup.append(on_cleanup)
|
||||||
app.router.add_get("/health", health_handler)
|
app.router.add_get("/health", health_handler)
|
||||||
|
|
@ -220,7 +221,7 @@ def create_app() -> web.Application:
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def main():
|
||||||
logger.info("Starting token service on port %s", PORT)
|
logger.info("Starting token service on port %s", PORT)
|
||||||
chatgpt_provider = PROVIDERS.get("chatgpt")
|
chatgpt_provider = PROVIDERS.get("chatgpt")
|
||||||
if chatgpt_provider:
|
if chatgpt_provider:
|
||||||
|
|
@ -239,3 +240,7 @@ if __name__ == "__main__":
|
||||||
port=PORT,
|
port=PORT,
|
||||||
access_log_class=AccessLogger,
|
access_log_class=AccessLogger,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,8 @@ class FakeProvider(Provider):
|
||||||
def prepare_threshold(self) -> int:
|
def prepare_threshold(self) -> int:
|
||||||
return self._prepare_threshold
|
return self._prepare_threshold
|
||||||
|
|
||||||
def should_prepare_standby(self) -> bool:
|
def should_prepare_standby(self, usage_percent: int) -> bool:
|
||||||
return False
|
return usage_percent >= self.prepare_threshold
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue