MCPcopy Index your code
hub / github.com/APIParkLab/APIPark / streamHandler

Function streamHandler

ai-provider/local/executor_test.go:31–65  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

29}
30
31func 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
67func stopPull(c *gin.Context) {
68 model := c.Query("model")

Callers

nothing calls this directly

Calls 4

PullModelFunction · 0.85
WriteMethod · 0.80
InfoMethod · 0.65
ClosePipelineMethod · 0.45

Tested by

no test coverage detected