File tree 4 files changed +37
-5
lines changed
internal/server/api/v1/pool
4 files changed +37
-5
lines changed Original file line number Diff line number Diff line change @@ -493,7 +493,7 @@ const docTemplate = `{
493
493
"tags": [
494
494
"工作池"
495
495
],
496
- "summary": "详情",
496
+ "summary": "详情, 支持SSE订阅 ",
497
497
"responses": {
498
498
"200": {
499
499
"description": "OK",
Original file line number Diff line number Diff line change 485
485
"tags" : [
486
486
" 工作池"
487
487
],
488
- "summary" : " 详情" ,
488
+ "summary" : " 详情, 支持SSE订阅 " ,
489
489
"responses" : {
490
490
"200" : {
491
491
"description" : " OK" ,
Original file line number Diff line number Diff line change @@ -866,7 +866,7 @@ paths:
866
866
description : Internal Server Error
867
867
schema :
868
868
$ref : ' #/definitions/types.SBase-any'
869
- summary : 详情
869
+ summary : 详情, 支持SSE订阅
870
870
tags :
871
871
- 工作池
872
872
post :
Original file line number Diff line number Diff line change 1
1
package pool
2
2
3
3
import (
4
+ "io"
5
+ "reflect"
6
+ "time"
7
+
4
8
"github.com/gin-gonic/gin"
5
9
6
10
"github.com/xmapst/AutoExecFlow/internal/server/api/base"
7
11
"github.com/xmapst/AutoExecFlow/internal/service"
12
+ "github.com/xmapst/AutoExecFlow/types"
8
13
_ "github.com/xmapst/AutoExecFlow/types"
9
14
)
10
15
11
16
// Detail
12
- // @Summary 详情
17
+ // @Summary 详情, 支持SSE订阅
13
18
// @Description 获取工作池信息
14
19
// @Tags 工作池
15
20
// @Accept application/json
@@ -18,5 +23,32 @@ import (
18
23
// @Failure 500 {object} types.SBase[any]
19
24
// @Router /api/v1/pool [get]
20
25
func Detail (c * gin.Context ) {
21
- base .Send (c , base .WithData (service .Pool ().Get ()))
26
+ if c .GetHeader ("Accept" ) != base .EventStreamMimeType {
27
+ base .Send (c , base .WithData (service .Pool ().Get ()))
28
+ return
29
+ }
30
+
31
+ ticker := time .NewTicker (30 * time .Second ) // 每30秒发送心跳
32
+ defer ticker .Stop ()
33
+
34
+ var last * types.SPoolRes
35
+ c .Stream (func (w io.Writer ) bool {
36
+ select {
37
+ case <- ticker .C :
38
+ c .SSEvent ("heartbeat" , "keepalive" )
39
+ return true
40
+ case <- c .Done ():
41
+ return false
42
+ default :
43
+ current := service .Pool ().Get ()
44
+ if reflect .DeepEqual (last , current ) {
45
+ time .Sleep (1 * time .Second )
46
+ return true
47
+ }
48
+ c .SSEvent ("message" , base .WithData (current ))
49
+ last = current
50
+ time .Sleep (1 * time .Second )
51
+ return true
52
+ }
53
+ })
22
54
}
You can’t perform that action at this time.
0 commit comments