refactor: Improve asynchronous message processing in WriteMessageAsync

- Introduce a done channel to track message processing completion.
- Increase timeout for message processing to 5 seconds for better reliability.
- Adjust queue timeout to 1 second to enhance responsiveness when the queue is full.
This commit is contained in:
Max 2025-04-27 12:19:54 +08:00
parent c76024fda1
commit 93be3acb97

View file

@ -84,17 +84,25 @@ func (mq *AsyncMessageQueue) worker() {
// WriteMessageAsync writes the message to response writer using the message queue
func WriteMessageAsync(m *Message, w gin.ResponseWriter) bool {
done := make(chan bool, 1)
task := &AsyncTask{
message: m,
writer: w,
done: nil, // No need for done channel anymore
done: done,
}
// Try to send the task to the queue with a short timeout
// Try to send the task to the queue with a timeout
select {
case GetQueue().queue <- task:
return true
case <-time.After(100 * time.Millisecond): // Reduced timeout since we don't wait for result
// Wait for the message to be processed with a longer timeout
select {
case success := <-done:
return success
case <-time.After(5 * time.Second): // Increased timeout to 5 seconds
log.Error("Message processing timeout")
return false
}
case <-time.After(1 * time.Second): // Increased queue timeout to 1 second
log.Error("Queue is full, message dropped")
return false
}