mirror of
https://github.com/coaidev/coai.git
synced 2025-05-19 21:10:18 +09:00
fix: fix stop signal calling stack
fix: fix stop signal calling stack Co-Authored-By: Minghan Zhang <112773885+zmh-program@users.noreply.github.com>
This commit is contained in:
parent
b0a684cada
commit
3dffa7fdab
@ -10,6 +10,7 @@ import (
|
|||||||
"chat/globals"
|
"chat/globals"
|
||||||
"chat/manager/conversation"
|
"chat/manager/conversation"
|
||||||
"chat/utils"
|
"chat/utils"
|
||||||
|
"time"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
@ -51,7 +52,9 @@ type partialChunk struct {
|
|||||||
func createStopSignal(conn *Connection) chan bool {
|
func createStopSignal(conn *Connection) chan bool {
|
||||||
stopSignal := make(chan bool, 1)
|
stopSignal := make(chan bool, 1)
|
||||||
go func(conn *Connection, stopSignal chan bool) {
|
go func(conn *Connection, stopSignal chan bool) {
|
||||||
|
ticker := time.NewTicker(100 * time.Millisecond)
|
||||||
defer func() {
|
defer func() {
|
||||||
|
ticker.Stop()
|
||||||
if r := recover(); r != nil && !strings.Contains(fmt.Sprintf("%s", r), "closed channel") {
|
if r := recover(); r != nil && !strings.Contains(fmt.Sprintf("%s", r), "closed channel") {
|
||||||
stack := debug.Stack()
|
stack := debug.Stack()
|
||||||
globals.Warn(fmt.Sprintf("caught panic from stop signal: %s\n%s", r, stack))
|
globals.Warn(fmt.Sprintf("caught panic from stop signal: %s\n%s", r, stack))
|
||||||
@ -59,9 +62,14 @@ func createStopSignal(conn *Connection) chan bool {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if conn.PeekStop() != nil {
|
select {
|
||||||
stopSignal <- true
|
case <-ticker.C:
|
||||||
break
|
state := conn.PeekStop() != nil // check the stop state
|
||||||
|
stopSignal <- state
|
||||||
|
|
||||||
|
if state {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(conn, stopSignal)
|
}(conn, stopSignal)
|
||||||
@ -169,16 +177,19 @@ func createChatTask(
|
|||||||
|
|
||||||
return hit, sendPackError
|
return hit, sendPackError
|
||||||
}
|
}
|
||||||
case <-stopSignal:
|
case signal := <-stopSignal:
|
||||||
globals.Info(fmt.Sprintf("client stopped the chat request (model: %s, client: %s)", model, conn.GetCtx().ClientIP()))
|
// if stop signal is received
|
||||||
_ = conn.SendClient(globals.ChatSegmentResponse{
|
if signal {
|
||||||
Quota: buffer.GetQuota(),
|
globals.Info(fmt.Sprintf("client stopped the chat request (model: %s, client: %s)", model, conn.GetCtx().ClientIP()))
|
||||||
End: true,
|
_ = conn.SendClient(globals.ChatSegmentResponse{
|
||||||
Plan: plan,
|
Quota: buffer.GetQuota(),
|
||||||
})
|
End: true,
|
||||||
interruptSignal <- errors.New("signal")
|
Plan: plan,
|
||||||
|
})
|
||||||
|
interruptSignal <- errors.New("signal")
|
||||||
|
|
||||||
return
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user