stream_scanner.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package helper
  2. import (
  3. "bufio"
  4. "context"
  5. "github.com/bytedance/gopkg/util/gopool"
  6. "io"
  7. "net/http"
  8. "one-api/common"
  9. "one-api/constant"
  10. relaycommon "one-api/relay/common"
  11. "one-api/setting/operation_setting"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/gin-gonic/gin"
  16. )
  17. const (
  18. InitialScannerBufferSize = 1 << 20 // 1MB (1*1024*1024)
  19. MaxScannerBufferSize = 10 << 20 // 10MB (10*1024*1024)
  20. DefaultPingInterval = 10 * time.Second
  21. )
  22. type DoRequestFunc func(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error)
  23. // Optional SSE Ping keep-alive mechanism
  24. //
  25. // Used to solve the problem of the connection with the client timing out due to no data being sent when the upstream
  26. // channel response time is long (e.g., thinking model).
  27. // When enabled, it will send ping data packets to the client via SSE at the specified interval to maintain the connection.
  28. func DoStreamRequestWithPinger(doRequest DoRequestFunc, c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  29. SetEventStreamHeaders(c)
  30. generalSettings := operation_setting.GetGeneralSetting()
  31. pingEnabled := generalSettings.PingIntervalEnabled
  32. pingInterval := time.Duration(generalSettings.PingIntervalSeconds) * time.Second
  33. pingerCtx, stopPinger := context.WithCancel(c.Request.Context())
  34. var pingerWg sync.WaitGroup
  35. var doRequestErr error
  36. var resp any
  37. if pingEnabled {
  38. pingerWg.Add(1)
  39. gopool.Go(func() {
  40. defer pingerWg.Done()
  41. if pingInterval <= 0 {
  42. pingInterval = DefaultPingInterval
  43. }
  44. ticker := time.NewTicker(pingInterval)
  45. defer ticker.Stop()
  46. var pingMutex sync.Mutex
  47. if common.DebugEnabled {
  48. println("SSE ping goroutine started.")
  49. }
  50. for {
  51. select {
  52. case <-ticker.C:
  53. pingMutex.Lock()
  54. err := PingData(c)
  55. pingMutex.Unlock()
  56. if err != nil {
  57. common.LogError(c, "SSE ping error: "+err.Error())
  58. return
  59. }
  60. if common.DebugEnabled {
  61. println("SSE ping data sent.")
  62. }
  63. case <-pingerCtx.Done():
  64. if common.DebugEnabled {
  65. println("SSE ping goroutine stopped.")
  66. }
  67. return
  68. }
  69. }
  70. })
  71. }
  72. resp, doRequestErr = doRequest(c, info, requestBody)
  73. stopPinger()
  74. if pingEnabled {
  75. pingerWg.Wait()
  76. }
  77. return resp, doRequestErr
  78. }
  79. func StreamScannerHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo, dataHandler func(data string) bool) {
  80. if resp == nil || dataHandler == nil {
  81. return
  82. }
  83. defer resp.Body.Close()
  84. streamingTimeout := time.Duration(constant.StreamingTimeout) * time.Second
  85. if strings.HasPrefix(info.UpstreamModelName, "o1") || strings.HasPrefix(info.UpstreamModelName, "o3") {
  86. // twice timeout for thinking model
  87. streamingTimeout *= 2
  88. }
  89. var (
  90. stopChan = make(chan bool, 2)
  91. scanner = bufio.NewScanner(resp.Body)
  92. ticker = time.NewTicker(streamingTimeout)
  93. writeMutex sync.Mutex // Mutex to protect concurrent writes
  94. )
  95. defer func() {
  96. ticker.Stop()
  97. close(stopChan)
  98. }()
  99. scanner.Buffer(make([]byte, InitialScannerBufferSize), MaxScannerBufferSize)
  100. scanner.Split(bufio.ScanLines)
  101. SetEventStreamHeaders(c)
  102. ctx, cancel := context.WithCancel(context.Background())
  103. defer cancel()
  104. ctx = context.WithValue(ctx, "stop_chan", stopChan)
  105. common.RelayCtxGo(ctx, func() {
  106. for scanner.Scan() {
  107. ticker.Reset(streamingTimeout)
  108. data := scanner.Text()
  109. if common.DebugEnabled {
  110. println(data)
  111. }
  112. if len(data) < 6 {
  113. continue
  114. }
  115. if data[:5] != "data:" && data[:6] != "[DONE]" {
  116. continue
  117. }
  118. data = data[5:]
  119. data = strings.TrimLeft(data, " ")
  120. data = strings.TrimSuffix(data, "\"")
  121. if !strings.HasPrefix(data, "[DONE]") {
  122. info.SetFirstResponseTime()
  123. writeMutex.Lock() // Lock before writing
  124. success := dataHandler(data)
  125. writeMutex.Unlock() // Unlock after writing
  126. if !success {
  127. break
  128. }
  129. }
  130. }
  131. if err := scanner.Err(); err != nil {
  132. if err != io.EOF {
  133. common.LogError(c, "scanner error: "+err.Error())
  134. }
  135. }
  136. common.SafeSendBool(stopChan, true)
  137. })
  138. select {
  139. case <-ticker.C:
  140. // 超时处理逻辑
  141. common.LogError(c, "streaming timeout")
  142. common.SafeSendBool(stopChan, true)
  143. case <-stopChan:
  144. // 正常结束
  145. common.LogInfo(c, "streaming finished")
  146. }
  147. }