yym68686 commited on
Commit
9871835
·
1 Parent(s): 32f4b84

🐛 Bug: Fix a bug that may occur in a concurrent environment when multiple requests attempt to use the same database session simultaneously.

Browse files
Files changed (2) hide show
  1. .github/workflows/main.yml +1 -1
  2. main.py +32 -23
.github/workflows/main.yml CHANGED
@@ -62,7 +62,7 @@ jobs:
62
  git config --global user.email 'github-actions[bot]@users.noreply.github.com'
63
  git add VERSION
64
  git commit -m "Bump version to ${{ steps.bump_version.outputs.new_version }}"
65
- git push https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }} HEAD:main
66
 
67
  - name: Build and push Docker image
68
  uses: docker/[email protected]
 
62
  git config --global user.email 'github-actions[bot]@users.noreply.github.com'
63
  git add VERSION
64
  git commit -m "Bump version to ${{ steps.bump_version.outputs.new_version }}"
65
+ git push
66
 
67
  - name: Build and push Docker image
68
  uses: docker/[email protected]
main.py CHANGED
@@ -162,7 +162,6 @@ async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False
162
  class StatsMiddleware(BaseHTTPMiddleware):
163
  def __init__(self, app):
164
  super().__init__(app)
165
- self.db = async_session()
166
 
167
  async def dispatch(self, request: Request, call_next):
168
  if request.headers.get("x-api-key"):
@@ -236,30 +235,40 @@ class StatsMiddleware(BaseHTTPMiddleware):
236
  return response
237
 
238
  async def update_stats(self, endpoint, process_time, client_ip, model, token, is_flagged, moderated_content):
239
- async with self.db as session:
240
- new_request_stat = RequestStat(
241
- endpoint=endpoint,
242
- ip=client_ip,
243
- token=token,
244
- total_time=process_time,
245
- model=model,
246
- is_flagged=is_flagged,
247
- moderated_content=moderated_content
248
- )
249
- session.add(new_request_stat)
250
- await session.commit()
 
 
 
 
 
251
 
252
  async def update_channel_stats(self, provider, model, api_key, success, first_response_time):
253
- async with self.db as session:
254
- channel_stat = ChannelStat(
255
- provider=provider,
256
- model=model,
257
- api_key=api_key,
258
- success=success,
259
- first_response_time=first_response_time
260
- )
261
- session.add(channel_stat)
262
- await session.commit()
 
 
 
 
 
263
 
264
  async def moderate_content(self, content, token):
265
  moderation_request = ModerationRequest(input=content)
 
162
  class StatsMiddleware(BaseHTTPMiddleware):
163
  def __init__(self, app):
164
  super().__init__(app)
 
165
 
166
  async def dispatch(self, request: Request, call_next):
167
  if request.headers.get("x-api-key"):
 
235
  return response
236
 
237
  async def update_stats(self, endpoint, process_time, client_ip, model, token, is_flagged, moderated_content):
238
+ async with async_session() as session:
239
+ async with session.begin():
240
+ try:
241
+ new_request_stat = RequestStat(
242
+ endpoint=endpoint,
243
+ ip=client_ip,
244
+ token=token,
245
+ total_time=process_time,
246
+ model=model,
247
+ is_flagged=is_flagged,
248
+ moderated_content=moderated_content
249
+ )
250
+ session.add(new_request_stat)
251
+ await session.commit()
252
+ except Exception as e:
253
+ await session.rollback()
254
+ logger.error(f"Error updating stats: {str(e)}")
255
 
256
  async def update_channel_stats(self, provider, model, api_key, success, first_response_time):
257
+ async with async_session() as session:
258
+ async with session.begin():
259
+ try:
260
+ channel_stat = ChannelStat(
261
+ provider=provider,
262
+ model=model,
263
+ api_key=api_key,
264
+ success=success,
265
+ first_response_time=first_response_time
266
+ )
267
+ session.add(channel_stat)
268
+ await session.commit()
269
+ except Exception as e:
270
+ await session.rollback()
271
+ logger.error(f"Error updating channel stats: {str(e)}")
272
 
273
  async def moderate_content(self, content, token):
274
  moderation_request = ModerationRequest(input=content)