stream_scanner.go 6.1 KB

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