mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-09 23:10:20 +08:00
refactor: simplify error handling and session finalization in StreamingService
- Removed retry logic for finalizing streaming sessions, streamlining the process to handle success and error cases more clearly. - Enhanced error logging to provide immediate feedback on failures without retaining session data, preventing potential memory leaks. - Improved code readability by consolidating the success and error handling paths, ensuring a more straightforward flow in the session finalization logic.
This commit is contained in:
parent
60204e2166
commit
24288cecf9
@ -206,43 +206,25 @@ class StreamingService {
|
||||
return
|
||||
}
|
||||
|
||||
const maxRetries = 3
|
||||
let lastError: Error | null = null
|
||||
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
try {
|
||||
// Route to appropriate data source based on topic type
|
||||
// TEMPORARY: Agent sessions use dbService until migration to Data API is complete
|
||||
if (isAgentSessionTopicId(session.topicId)) {
|
||||
const updatePayload = this.convertToUpdatePayload(session, status)
|
||||
|
||||
// Route to appropriate data source based on topic type
|
||||
// TEMPORARY: Agent sessions use dbService until migration to Data API is complete
|
||||
if (isAgentSessionTopicId(session.topicId)) {
|
||||
await dbService.updateMessageAndBlocks(session.topicId, updatePayload.messageUpdates, updatePayload.blocks)
|
||||
} else {
|
||||
// Normal topic → Use Data API for persistence (v2 target architecture)
|
||||
const dataApiPayload = this.convertToDataApiFormat(session, status)
|
||||
await dataApiService.patch(`/messages/${session.messageId}`, { body: dataApiPayload })
|
||||
}
|
||||
|
||||
// Success - cleanup session
|
||||
this.clearSession(messageId)
|
||||
logger.debug('Finalized streaming session', { messageId, status })
|
||||
return
|
||||
} catch (error) {
|
||||
lastError = error as Error
|
||||
logger.warn(`finalize attempt ${attempt}/${maxRetries} failed:`, error as Error)
|
||||
|
||||
if (attempt < maxRetries) {
|
||||
// Exponential backoff
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000 * attempt))
|
||||
}
|
||||
await dbService.updateMessageAndBlocks(session.topicId, updatePayload.messageUpdates, updatePayload.blocks)
|
||||
} else {
|
||||
// Normal topic → Use Data API for persistence (has built-in retry)
|
||||
const dataApiPayload = this.convertToDataApiFormat(session, status)
|
||||
await dataApiService.patch(`/messages/${session.messageId}`, { body: dataApiPayload })
|
||||
}
|
||||
}
|
||||
|
||||
// All retries failed
|
||||
logger.error(`finalize failed after ${maxRetries} attempts:`, lastError)
|
||||
// TRADEOFF: Don't clear session to allow manual retry
|
||||
// TTL will auto-clean to prevent permanent memory leak
|
||||
throw lastError
|
||||
this.clearSession(messageId)
|
||||
logger.debug('Finalized streaming session', { messageId, status })
|
||||
} catch (error) {
|
||||
logger.error('finalize failed:', error as Error)
|
||||
// Don't clear session on error - TTL will auto-clean to prevent memory leak
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user