Skip to content

Commit

Permalink
feature: Add logic to handle AppInvokeQuotaExceededError in the servi…
Browse files Browse the repository at this point in the history
…ce API interface.
  • Loading branch information
liuzhenghua-jk committed Jul 5, 2024
1 parent 60e9381 commit acd0269
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/controllers/console/app/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def post(self, app_model):
raise ProviderModelCurrentlyNotSupportError()
except InvokeError as e:
raise CompletionRequestError(e.description)
except ValueError as e:
except (ValueError, AppInvokeQuotaExceededError) as e:
raise e
except Exception as e:
logging.exception("internal server error.")
Expand Down
11 changes: 8 additions & 3 deletions api/controllers/service_api/app/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token
from core.app.apps.base_app_queue_manager import AppQueueManager
from core.app.entities.app_invoke_entities import InvokeFrom
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
from core.errors.error import (
AppInvokeQuotaExceededError,
ModelCurrentlyNotSupportError,
ProviderTokenNotInitError,
QuotaExceededError,
)
from core.model_runtime.errors.invoke import InvokeError
from libs import helper
from libs.helper import uuid_value
Expand Down Expand Up @@ -69,7 +74,7 @@ def post(self, app_model: App, end_user: EndUser):
raise ProviderModelCurrentlyNotSupportError()
except InvokeError as e:
raise CompletionRequestError(e.description)
except ValueError as e:
except (ValueError, AppInvokeQuotaExceededError) as e:
raise e
except Exception as e:
logging.exception("internal server error.")
Expand Down Expand Up @@ -132,7 +137,7 @@ def post(self, app_model: App, end_user: EndUser):
raise ProviderModelCurrentlyNotSupportError()
except InvokeError as e:
raise CompletionRequestError(e.description)
except ValueError as e:
except (ValueError, AppInvokeQuotaExceededError) as e:
raise e
except Exception as e:
logging.exception("internal server error.")
Expand Down
9 changes: 7 additions & 2 deletions api/controllers/service_api/app/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token
from core.app.apps.base_app_queue_manager import AppQueueManager
from core.app.entities.app_invoke_entities import InvokeFrom
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
from core.errors.error import (
AppInvokeQuotaExceededError,
ModelCurrentlyNotSupportError,
ProviderTokenNotInitError,
QuotaExceededError,
)
from core.model_runtime.errors.invoke import InvokeError
from libs import helper
from models.model import App, AppMode, EndUser
Expand Down Expand Up @@ -59,7 +64,7 @@ def post(self, app_model: App, end_user: EndUser):
raise ProviderModelCurrentlyNotSupportError()
except InvokeError as e:
raise CompletionRequestError(e.description)
except ValueError as e:
except (ValueError, AppInvokeQuotaExceededError) as e:
raise e
except Exception as e:
logging.exception("internal server error.")
Expand Down
1 change: 1 addition & 0 deletions api/core/app/features/rate_limiting/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def enter(self, request_id: Optional[str] = None) -> str:
request_id = RateLimit.gen_request_key()
if time.time() - self.last_recalculate_time > RateLimit._ACTIVE_REQUESTS_COUNT_FLUSH_INTERVAL:
self.flush_active_requests()

redis_client.hset(self.active_requests_key, request_id, str(time.time()))
active_requests_count = redis_client.hlen(self.active_requests_key)
if active_requests_count > self.max_active_requests:
Expand Down

0 comments on commit acd0269

Please sign in to comment.