stream_scanner.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package helper
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  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/bytedance/gopkg/util/gopool"
  16. "github.com/gin-gonic/gin"
  17. )
  18. const (
  19. InitialScannerBufferSize = 64 << 10 // 64KB (64*1024)
  20. MaxScannerBufferSize = 10 << 20 // 10MB (10*1024*1024)
  21. DefaultPingInterval = 10 * time.Second
  22. )
  23. func StreamScannerHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo, dataHandler func(data string) bool) {
  24. if resp == nil || dataHandler == nil {
  25. return
  26. }
  27. // 确保响应体总是被关闭
  28. defer func() {
  29. if resp.Body != nil {
  30. resp.Body.Close()
  31. }
  32. }()
  33. streamingTimeout := time.Duration(constant.StreamingTimeout) * time.Second
  34. if strings.HasPrefix(info.UpstreamModelName, "o") {
  35. // twice timeout for thinking model
  36. streamingTimeout *= 2
  37. }
  38. var (
  39. stopChan = make(chan bool, 3) // 增加缓冲区避免阻塞
  40. scanner = bufio.NewScanner(resp.Body)
  41. ticker = time.NewTicker(streamingTimeout)
  42. pingTicker *time.Ticker
  43. writeMutex sync.Mutex // Mutex to protect concurrent writes
  44. wg sync.WaitGroup // 用于等待所有 goroutine 退出
  45. )
  46. generalSettings := operation_setting.GetGeneralSetting()
  47. pingEnabled := generalSettings.PingIntervalEnabled
  48. pingInterval := time.Duration(generalSettings.PingIntervalSeconds) * time.Second
  49. if pingInterval <= 0 {
  50. pingInterval = DefaultPingInterval
  51. }
  52. if pingEnabled {
  53. pingTicker = time.NewTicker(pingInterval)
  54. }
  55. if common.DebugEnabled {
  56. // print timeout and ping interval for debugging
  57. println("relay timeout seconds:", common.RelayTimeout)
  58. println("streaming timeout seconds:", int64(streamingTimeout.Seconds()))
  59. println("ping interval seconds:", int64(pingInterval.Seconds()))
  60. }
  61. // 改进资源清理,确保所有 goroutine 正确退出
  62. defer func() {
  63. // 通知所有 goroutine 停止
  64. common.SafeSendBool(stopChan, true)
  65. ticker.Stop()
  66. if pingTicker != nil {
  67. pingTicker.Stop()
  68. }
  69. // 等待所有 goroutine 退出,最多等待5秒
  70. done := make(chan struct{})
  71. go func() {
  72. wg.Wait()
  73. close(done)
  74. }()
  75. select {
  76. case <-done:
  77. case <-time.After(5 * time.Second):
  78. common.LogError(c, "timeout waiting for goroutines to exit")
  79. }
  80. close(stopChan)
  81. }()
  82. scanner.Buffer(make([]byte, InitialScannerBufferSize), MaxScannerBufferSize)
  83. scanner.Split(bufio.ScanLines)
  84. SetEventStreamHeaders(c)
  85. ctx, cancel := context.WithCancel(context.Background())
  86. defer cancel()
  87. ctx = context.WithValue(ctx, "stop_chan", stopChan)
  88. // Handle ping data sending with improved error handling
  89. if pingEnabled && pingTicker != nil {
  90. wg.Add(1)
  91. gopool.Go(func() {
  92. defer func() {
  93. wg.Done()
  94. if r := recover(); r != nil {
  95. common.LogError(c, fmt.Sprintf("ping goroutine panic: %v", r))
  96. common.SafeSendBool(stopChan, true)
  97. }
  98. if common.DebugEnabled {
  99. println("ping goroutine exited")
  100. }
  101. }()
  102. // 添加超时保护,防止 goroutine 无限运行
  103. maxPingDuration := 30 * time.Minute // 最大 ping 持续时间
  104. pingTimeout := time.NewTimer(maxPingDuration)
  105. defer pingTimeout.Stop()
  106. for {
  107. select {
  108. case <-pingTicker.C:
  109. // 使用超时机制防止写操作阻塞
  110. done := make(chan error, 1)
  111. go func() {
  112. writeMutex.Lock()
  113. defer writeMutex.Unlock()
  114. done <- PingData(c)
  115. }()
  116. select {
  117. case err := <-done:
  118. if err != nil {
  119. common.LogError(c, "ping data error: "+err.Error())
  120. return
  121. }
  122. if common.DebugEnabled {
  123. println("ping data sent")
  124. }
  125. case <-time.After(10 * time.Second):
  126. common.LogError(c, "ping data send timeout")
  127. return
  128. case <-ctx.Done():
  129. return
  130. case <-stopChan:
  131. return
  132. }
  133. case <-ctx.Done():
  134. return
  135. case <-stopChan:
  136. return
  137. case <-c.Request.Context().Done():
  138. // 监听客户端断开连接
  139. return
  140. case <-pingTimeout.C:
  141. common.LogError(c, "ping goroutine max duration reached")
  142. return
  143. }
  144. }
  145. })
  146. }
  147. // Scanner goroutine with improved error handling
  148. wg.Add(1)
  149. common.RelayCtxGo(ctx, func() {
  150. defer func() {
  151. wg.Done()
  152. if r := recover(); r != nil {
  153. common.LogError(c, fmt.Sprintf("scanner goroutine panic: %v", r))
  154. }
  155. common.SafeSendBool(stopChan, true)
  156. if common.DebugEnabled {
  157. println("scanner goroutine exited")
  158. }
  159. }()
  160. for scanner.Scan() {
  161. // 检查是否需要停止
  162. select {
  163. case <-stopChan:
  164. return
  165. case <-ctx.Done():
  166. return
  167. case <-c.Request.Context().Done():
  168. return
  169. default:
  170. }
  171. ticker.Reset(streamingTimeout)
  172. data := scanner.Text()
  173. if common.DebugEnabled {
  174. println(data)
  175. }
  176. if len(data) < 6 {
  177. continue
  178. }
  179. if data[:5] != "data:" && data[:6] != "[DONE]" {
  180. continue
  181. }
  182. data = data[5:]
  183. data = strings.TrimLeft(data, " ")
  184. data = strings.TrimSuffix(data, "\r")
  185. if !strings.HasPrefix(data, "[DONE]") {
  186. info.SetFirstResponseTime()
  187. // 使用超时机制防止写操作阻塞
  188. done := make(chan bool, 1)
  189. go func() {
  190. writeMutex.Lock()
  191. defer writeMutex.Unlock()
  192. done <- dataHandler(data)
  193. }()
  194. select {
  195. case success := <-done:
  196. if !success {
  197. return
  198. }
  199. case <-time.After(10 * time.Second):
  200. common.LogError(c, "data handler timeout")
  201. return
  202. case <-ctx.Done():
  203. return
  204. case <-stopChan:
  205. return
  206. }
  207. }
  208. }
  209. if err := scanner.Err(); err != nil {
  210. if err != io.EOF {
  211. common.LogError(c, "scanner error: "+err.Error())
  212. }
  213. }
  214. })
  215. // 主循环等待完成或超时
  216. select {
  217. case <-ticker.C:
  218. // 超时处理逻辑
  219. common.LogError(c, "streaming timeout")
  220. case <-stopChan:
  221. // 正常结束
  222. common.LogInfo(c, "streaming finished")
  223. case <-c.Request.Context().Done():
  224. // 客户端断开连接
  225. common.LogInfo(c, "client disconnected")
  226. }
  227. }