Skip to content

Commit

Permalink
fix exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
AllentDan committed Dec 16, 2024
1 parent 60dca49 commit 3e76283
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lmdeploy/serve/proxy/proxy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import asyncio
import copy
import json
import os
Expand All @@ -18,6 +19,7 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse
from pydantic import BaseModel, Field
from requests.exceptions import RequestException

from lmdeploy.serve.openai.api_server import (check_api_key,
create_error_response)
Expand Down Expand Up @@ -87,6 +89,9 @@ def __init__(self,
with open(self.config_path, 'r') as config_file:
self.nodes = yaml.safe_load(config_file)['nodes']
for url, status in self.nodes.items():
latency = deque(status.get('latency', []),
maxlen=LATENCY_DEEQUE_LEN)
status['latency'] = latency
status = Status(**status)
self.nodes[url] = status
self.heart_beat_thread = threading.Thread(target=heart_beat_controller,
Expand All @@ -99,7 +104,7 @@ def update_config_file(self):
nodes = copy.deepcopy(self.nodes)
for url, status in nodes.items():
nodes[url] = status.model_dump()
nodes[url]['latency'] = list(status.latency)
nodes[url]['latency'] = list(status.latency)[-LATENCY_DEEQUE_LEN:]
with open(self.config_path, 'w') as config_file: # update cfg yml
yaml.dump(dict(nodes=nodes), config_file)

Expand Down Expand Up @@ -262,7 +267,7 @@ def handle_api_timeout(self, node_url):
"""Handle the api time out."""
logger.warn(f'api timeout: {node_url}')
ret = {
'error_code': ErrorCodes.API_TIMEOUT,
'error_code': ErrorCodes.API_TIMEOUT.value,
'text': err_msg[ErrorCodes.API_TIMEOUT],
}
return json.dumps(ret).encode() + b'\n'
Expand All @@ -286,9 +291,8 @@ def stream_generate(self, request: Dict, node_url: str, node_path: str):
delimiter=b'\n'):
if chunk:
yield chunk + b'\n\n'
except requests.exceptions.RequestException as e: # noqa
except (Exception, GeneratorExit, RequestException) as e: # noqa
# exception happened, reduce unfinished num
self.nodes[node_url].unfinished -= 1
yield self.handle_api_timeout(node_url)

async def generate(self, request: Dict, node_url: str, node_path: str):
Expand All @@ -306,9 +310,7 @@ async def generate(self, request: Dict, node_url: str, node_path: str):
json=request,
timeout=API_TIMEOUT_LEN)
return response.text
except requests.exceptions.RequestException as e: # noqa
# exception happened, reduce unfinished num
self.nodes[node_url].unfinished -= 1
except (Exception, GeneratorExit, RequestException, asyncio.CancelledError) as e: # noqa # yapf: disable
return self.handle_api_timeout(node_url)

def pre_call(self, node_url):
Expand Down Expand Up @@ -385,7 +387,9 @@ def add_node(node: Node, raw_request: Request = None):
RPM or other metric. All the values of nodes should be the same metric.
"""
try:
node_manager.add(node.url, node.status)
res = node_manager.add(node.url, node.status)
if res is not None:
return res
return 'Added successfully'
except: # noqa
return 'Failed to add, please check the input url.'
Expand Down

0 comments on commit 3e76283

Please sign in to comment.