mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-06 21:35:52 +08:00
feat(timeout): implement extendMessagesTimeout middleware and set server timeouts
This commit is contained in:
parent
a1d14b9292
commit
e1a0dd6810
@ -14,6 +14,13 @@ import { modelsRoutes } from './routes/models'
|
|||||||
|
|
||||||
const logger = loggerService.withContext('ApiServer')
|
const logger = loggerService.withContext('ApiServer')
|
||||||
|
|
||||||
|
const LONG_POLL_TIMEOUT_MS = 120 * 60_000 // 120 minutes
|
||||||
|
const extendMessagesTimeout: express.RequestHandler = (req, res, next) => {
|
||||||
|
req.setTimeout(LONG_POLL_TIMEOUT_MS)
|
||||||
|
res.setTimeout(LONG_POLL_TIMEOUT_MS)
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(
|
app.use(
|
||||||
express.json({
|
express.json({
|
||||||
@ -122,7 +129,7 @@ app.get('/', (_req, res) => {
|
|||||||
setupOpenAPIDocumentation(app)
|
setupOpenAPIDocumentation(app)
|
||||||
|
|
||||||
// Provider-specific messages route requires authentication
|
// Provider-specific messages route requires authentication
|
||||||
app.use('/:provider/v1/messages', authMiddleware, messagesProviderRoutes)
|
app.use('/:provider/v1/messages', authMiddleware, extendMessagesTimeout, messagesProviderRoutes)
|
||||||
|
|
||||||
// API v1 routes with auth
|
// API v1 routes with auth
|
||||||
const apiRouter = express.Router()
|
const apiRouter = express.Router()
|
||||||
@ -130,7 +137,7 @@ apiRouter.use(authMiddleware)
|
|||||||
// Mount routes
|
// Mount routes
|
||||||
apiRouter.use('/chat', chatRoutes)
|
apiRouter.use('/chat', chatRoutes)
|
||||||
apiRouter.use('/mcps', mcpRoutes)
|
apiRouter.use('/mcps', mcpRoutes)
|
||||||
apiRouter.use('/messages', messagesRoutes)
|
apiRouter.use('/messages', extendMessagesTimeout, messagesRoutes)
|
||||||
apiRouter.use('/models', modelsRoutes)
|
apiRouter.use('/models', modelsRoutes)
|
||||||
apiRouter.use('/agents', agentsRoutes)
|
apiRouter.use('/agents', agentsRoutes)
|
||||||
app.use('/v1', apiRouter)
|
app.use('/v1', apiRouter)
|
||||||
|
|||||||
@ -7,6 +7,10 @@ import { config } from './config'
|
|||||||
|
|
||||||
const logger = loggerService.withContext('ApiServer')
|
const logger = loggerService.withContext('ApiServer')
|
||||||
|
|
||||||
|
const GLOBAL_REQUEST_TIMEOUT_MS = 5 * 60_000
|
||||||
|
const GLOBAL_HEADERS_TIMEOUT_MS = GLOBAL_REQUEST_TIMEOUT_MS + 5_000
|
||||||
|
const GLOBAL_KEEPALIVE_TIMEOUT_MS = 60_000
|
||||||
|
|
||||||
export class ApiServer {
|
export class ApiServer {
|
||||||
private server: ReturnType<typeof createServer> | null = null
|
private server: ReturnType<typeof createServer> | null = null
|
||||||
|
|
||||||
@ -26,6 +30,7 @@ export class ApiServer {
|
|||||||
|
|
||||||
// Create server with Express app
|
// Create server with Express app
|
||||||
this.server = createServer(app)
|
this.server = createServer(app)
|
||||||
|
this.applyServerTimeouts(this.server)
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -38,6 +43,13 @@ export class ApiServer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private applyServerTimeouts(server: ReturnType<typeof createServer>): void {
|
||||||
|
server.requestTimeout = GLOBAL_REQUEST_TIMEOUT_MS
|
||||||
|
server.headersTimeout = Math.max(GLOBAL_HEADERS_TIMEOUT_MS, server.requestTimeout + 1_000)
|
||||||
|
server.keepAliveTimeout = GLOBAL_KEEPALIVE_TIMEOUT_MS
|
||||||
|
server.setTimeout(0)
|
||||||
|
}
|
||||||
|
|
||||||
async stop(): Promise<void> {
|
async stop(): Promise<void> {
|
||||||
if (!this.server) return
|
if (!this.server) return
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user