feat: enhance API and session management with Nacos and Redis integration
- Add Nacos registry for service registration and deregistration. - Implement Redis registry for session management with heartbeat and session claiming. - Improve completion service with session handling and request validation. - Enhance WebSocket handling for completion requests with JSON-RPC support. - Add tests for new registry implementations and completion manager functionalities. - Refactor existing code for better readability and maintainability.
This commit is contained in:
@@ -15,15 +15,18 @@ type CompletionService interface {
|
||||
Complete(ctx context.Context, req completion.Request) (completion.Response, error)
|
||||
}
|
||||
|
||||
// SessionStatsProvider 暴露会话统计信息,供就绪探针输出。
|
||||
type SessionStatsProvider interface {
|
||||
ActiveSessions() map[string]int
|
||||
}
|
||||
|
||||
// RouteOptions 控制 HTTP/WS 接口的超时与请求体上限。
|
||||
type RouteOptions struct {
|
||||
RequestTimeout time.Duration
|
||||
MaxBodyBytes int64
|
||||
RequestTimeout time.Duration // 单次补全调用超时时间。
|
||||
MaxBodyBytes int64 // 请求体最大字节数(HTTP/WS 共用)。
|
||||
}
|
||||
|
||||
// RegisterRoutes 注册健康检查、HTTP 补全接口和 WebSocket 补全接口。
|
||||
func RegisterRoutes(router *gin.Engine, service CompletionService, options ...RouteOptions) {
|
||||
opts := RouteOptions{
|
||||
RequestTimeout: 10 * time.Second,
|
||||
@@ -60,6 +63,7 @@ func RegisterRoutes(router *gin.Engine, service CompletionService, options ...Ro
|
||||
registerWSRoutes(router, service, opts)
|
||||
|
||||
handleCompletion := func(c *gin.Context) {
|
||||
// 为单次请求限制 body 大小,避免异常大包占满内存。
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, opts.MaxBodyBytes)
|
||||
var req completion.Request
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -68,10 +72,12 @@ func RegisterRoutes(router *gin.Engine, service CompletionService, options ...Ro
|
||||
}
|
||||
|
||||
routeLang := c.Param("language")
|
||||
// 若 body 未显式给出 language,则使用路由参数。
|
||||
if req.Language == "" {
|
||||
req.Language = routeLang
|
||||
}
|
||||
|
||||
// 对下游补全调用增加超时保护,防止请求长时间悬挂。
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), opts.RequestTimeout)
|
||||
defer cancel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user