stream_scanner.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // 改进资源清理,确保所有 goroutine 正确退出
  56. defer func() {
  57. // 通知所有 goroutine 停止
  58. common.SafeSendBool(stopChan, true)
  59. ticker.Stop()
  60. if pingTicker != nil {
  61. pingTicker.Stop()
  62. }
  63. // 等待所有 goroutine 退出,最多等待5秒
  64. done := make(chan struct{})
  65. go func() {
  66. wg.Wait()
  67. close(done)
  68. }()
  69. select {
  70. case <-done:
  71. case <-time.After(5 * time.Second):
  72. common.LogError(c, "timeout waiting for goroutines to exit")
  73. }
  74. close(stopChan)
  75. }()
  76. scanner.Buffer(make([]byte, InitialScannerBufferSize), MaxScannerBufferSize)
  77. scanner.Split(bufio.ScanLines)
  78. SetEventStreamHeaders(c)
  79. ctx, cancel := context.WithCancel(context.Background())
  80. defer cancel()
  81. ctx = context.WithValue(ctx, "stop_chan", stopChan)
  82. // Handle ping data sending with improved error handling
  83. if pingEnabled && pingTicker != nil {
  84. wg.Add(1)
  85. gopool.Go(func() {
  86. defer func() {
  87. wg.Done()
  88. if r := recover(); r != nil {
  89. common.LogError(c, fmt.Sprintf("ping goroutine panic: %v", r))
  90. common.SafeSendBool(stopChan, true)
  91. }
  92. if common.DebugEnabled {
  93. println("ping goroutine exited")
  94. }
  95. }()
  96. // 添加超时保护,防止 goroutine 无限运行
  97. maxPingDuration := 30 * time.Minute // 最大 ping 持续时间
  98. pingTimeout := time.NewTimer(maxPingDuration)
  99. defer pingTimeout.Stop()
  100. for {
  101. select {
  102. case <-pingTicker.C:
  103. // 使用超时机制防止写操作阻塞
  104. done := make(chan error, 1)
  105. go func() {
  106. writeMutex.Lock()
  107. defer writeMutex.Unlock()
  108. done <- PingData(c)
  109. }()
  110. select {
  111. case err := <-done:
  112. if err != nil {
  113. common.LogError(c, "ping data error: "+err.Error())
  114. return
  115. }
  116. if common.DebugEnabled {
  117. println("ping data sent")
  118. }
  119. case <-time.After(10 * time.Second):
  120. common.LogError(c, "ping data send timeout")
  121. return
  122. case <-ctx.Done():
  123. return
  124. case <-stopChan:
  125. return
  126. }
  127. case <-ctx.Done():
  128. return
  129. case <-stopChan:
  130. return
  131. case <-c.Request.Context().Done():
  132. // 监听客户端断开连接
  133. return
  134. case <-pingTimeout.C:
  135. common.LogError(c, "ping goroutine max duration reached")
  136. return
  137. }
  138. }
  139. })
  140. }
  141. // Scanner goroutine with improved error handling
  142. wg.Add(1)
  143. common.RelayCtxGo(ctx, func() {
  144. defer func() {
  145. wg.Done()
  146. if r := recover(); r != nil {
  147. common.LogError(c, fmt.Sprintf("scanner goroutine panic: %v", r))
  148. }
  149. common.SafeSendBool(stopChan, true)
  150. if common.DebugEnabled {
  151. println("scanner goroutine exited")
  152. }
  153. }()
  154. for scanner.Scan() {
  155. // 检查是否需要停止
  156. select {
  157. case <-stopChan:
  158. return
  159. case <-ctx.Done():
  160. return
  161. case <-c.Request.Context().Done():
  162. return
  163. default:
  164. }
  165. ticker.Reset(streamingTimeout)
  166. data := scanner.Text()
  167. if common.DebugEnabled {
  168. println(data)
  169. }
  170. if len(data) < 6 {
  171. continue
  172. }
  173. if data[:5] != "data:" && data[:6] != "[DONE]" {
  174. continue
  175. }
  176. data = data[5:]
  177. data = strings.TrimLeft(data, " ")
  178. data = strings.TrimSuffix(data, "\r")
  179. if !strings.HasPrefix(data, "[DONE]") {
  180. info.SetFirstResponseTime()
  181. // 使用超时机制防止写操作阻塞
  182. done := make(chan bool, 1)
  183. go func() {
  184. writeMutex.Lock()
  185. defer writeMutex.Unlock()
  186. done <- dataHandler(data)
  187. }()
  188. select {
  189. case success := <-done:
  190. if !success {
  191. return
  192. }
  193. case <-time.After(10 * time.Second):
  194. common.LogError(c, "data handler timeout")
  195. return
  196. case <-ctx.Done():
  197. return
  198. case <-stopChan:
  199. return
  200. }
  201. }
  202. }
  203. if err := scanner.Err(); err != nil {
  204. if err != io.EOF {
  205. common.LogError(c, "scanner error: "+err.Error())
  206. }
  207. }
  208. })
  209. // 主循环等待完成或超时
  210. select {
  211. case <-ticker.C:
  212. // 超时处理逻辑
  213. common.LogError(c, "streaming timeout")
  214. case <-stopChan:
  215. // 正常结束
  216. common.LogInfo(c, "streaming finished")
  217. case <-c.Request.Context().Done():
  218. // 客户端断开连接
  219. common.LogInfo(c, "client disconnected")
  220. }
  221. }