stream_scanner.go 6.3 KB

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