File tree Expand file tree Collapse file tree 3 files changed +31
-9
lines changed Expand file tree Collapse file tree 3 files changed +31
-9
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package proxy
22
33import (
44 "bytes"
5+ "context"
56 "fmt"
67 "io"
78 "mime/multipart"
@@ -33,6 +34,10 @@ type ProxyManager struct {
3334 muxLogger * LogMonitor
3435
3536 processGroups map [string ]* ProcessGroup
37+
38+ // shutdown signaling
39+ shutdownCtx context.Context
40+ shutdownCancel context.CancelFunc
3641}
3742
3843func New (config Config ) * ProxyManager {
@@ -63,6 +68,8 @@ func New(config Config) *ProxyManager {
6368 upstreamLogger .SetLogLevel (LevelInfo )
6469 }
6570
71+ shutdownCtx , shutdownCancel := context .WithCancel (context .Background ())
72+
6673 pm := & ProxyManager {
6774 config : config ,
6875 ginEngine : gin .New (),
@@ -72,6 +79,9 @@ func New(config Config) *ProxyManager {
7279 upstreamLogger : upstreamLogger ,
7380
7481 processGroups : make (map [string ]* ProcessGroup ),
82+
83+ shutdownCtx : shutdownCtx ,
84+ shutdownCancel : shutdownCancel ,
7585 }
7686
7787 // create the process groups
@@ -261,6 +271,7 @@ func (pm *ProxyManager) Shutdown() {
261271 }(processGroup )
262272 }
263273 wg .Wait ()
274+ pm .shutdownCancel ()
264275}
265276
266277func (pm * ProxyManager ) swapProcessGroup (requestedModel string ) (* ProcessGroup , string , error ) {
Original file line number Diff line number Diff line change @@ -96,5 +96,9 @@ func (pm *ProxyManager) apiListModelsSSE(c *gin.Context) {
9696 }
9797 })()
9898
99- <- c .Request .Context ().Done ()
99+ select {
100+ case <- c .Request .Context ().Done ():
101+ case <- pm .shutdownCtx .Done ():
102+ }
103+
100104}
Original file line number Diff line number Diff line change @@ -52,14 +52,18 @@ func (pm *ProxyManager) streamLogsHandler(c *gin.Context) {
5252 }
5353 }
5454
55- cancelFn := logger .OnLogData (func (data []byte ) {
55+ defer logger .OnLogData (func (data []byte ) {
5656 if c != nil && c .Writer != nil {
5757 c .Writer .Write (data )
5858 flusher .Flush ()
5959 }
60- })
61- defer cancelFn ()
62- <- c .Request .Context ().Done ()
60+ })()
61+
62+ select {
63+ case <- c .Request .Context ().Done ():
64+ case <- pm .shutdownCtx .Done ():
65+ }
66+
6367}
6468
6569func (pm * ProxyManager ) streamLogsHandlerSSE (c * gin.Context ) {
@@ -85,14 +89,17 @@ func (pm *ProxyManager) streamLogsHandlerSSE(c *gin.Context) {
8589 }
8690 }
8791
88- cancelFn := logger .OnLogData (func (data []byte ) {
92+ defer logger .OnLogData (func (data []byte ) {
8993 if c != nil && c .Writer != nil {
9094 c .SSEvent ("message" , string (data ))
9195 c .Writer .Flush ()
9296 }
93- })
94- defer cancelFn ()
95- <- c .Request .Context ().Done ()
97+ })()
98+
99+ select {
100+ case <- c .Request .Context ().Done ():
101+ case <- pm .shutdownCtx .Done ():
102+ }
96103}
97104
98105// getLogger searches for the appropriate logger based on the logMonitorId
You can’t perform that action at this time.
0 commit comments