(c *gin.Context)
| 29 | } |
| 30 | |
| 31 | func streamHandler(c *gin.Context) { |
| 32 | // 创建一个通道,用于监测客户端关闭连接的信号 |
| 33 | model := c.Query("model") |
| 34 | p, err := PullModel(model, uuid.NewString(), nil) |
| 35 | if err != nil { |
| 36 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
| 37 | return |
| 38 | } |
| 39 | done := make(chan struct{}) |
| 40 | // 启动一个 goroutine 监听客户端关闭连接 |
| 41 | go func() { |
| 42 | select { |
| 43 | case <-c.Writer.CloseNotify(): |
| 44 | log.Info("client closed connection,close pipeline") |
| 45 | taskExecutor.ClosePipeline(model, p.id) |
| 46 | case <-done: |
| 47 | } |
| 48 | }() |
| 49 | |
| 50 | c.Stream(func(w io.Writer) bool { |
| 51 | select { |
| 52 | case msg, ok := <-p.channel: |
| 53 | if !ok { |
| 54 | return false |
| 55 | } |
| 56 | _, err := w.Write([]byte(fmt.Sprintf("%s\n", msg.Msg))) |
| 57 | if err != nil { |
| 58 | log.Error("write message error: %v", err) |
| 59 | return false |
| 60 | } |
| 61 | return true |
| 62 | } |
| 63 | }) |
| 64 | done <- struct{}{} |
| 65 | } |
| 66 | |
| 67 | func stopPull(c *gin.Context) { |
| 68 | model := c.Query("model") |
nothing calls this directly
no test coverage detected