Skip to content

Commit a4a3be2

Browse files
committed
update: update
1 parent 81571d9 commit a4a3be2

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

docs/docs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ const docTemplate = `{
493493
"tags": [
494494
"工作池"
495495
],
496-
"summary": "详情",
496+
"summary": "详情, 支持SSE订阅",
497497
"responses": {
498498
"200": {
499499
"description": "OK",

docs/swagger.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@
485485
"tags": [
486486
"工作池"
487487
],
488-
"summary": "详情",
488+
"summary": "详情, 支持SSE订阅",
489489
"responses": {
490490
"200": {
491491
"description": "OK",

docs/swagger.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ paths:
866866
description: Internal Server Error
867867
schema:
868868
$ref: '#/definitions/types.SBase-any'
869-
summary: 详情
869+
summary: 详情, 支持SSE订阅
870870
tags:
871871
- 工作池
872872
post:

internal/server/api/v1/pool/get.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package pool
22

33
import (
4+
"io"
5+
"reflect"
6+
"time"
7+
48
"github.com/gin-gonic/gin"
59

610
"github.com/xmapst/AutoExecFlow/internal/server/api/base"
711
"github.com/xmapst/AutoExecFlow/internal/service"
12+
"github.com/xmapst/AutoExecFlow/types"
813
_ "github.com/xmapst/AutoExecFlow/types"
914
)
1015

1116
// Detail
12-
// @Summary 详情
17+
// @Summary 详情, 支持SSE订阅
1318
// @Description 获取工作池信息
1419
// @Tags 工作池
1520
// @Accept application/json
@@ -18,5 +23,32 @@ import (
1823
// @Failure 500 {object} types.SBase[any]
1924
// @Router /api/v1/pool [get]
2025
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+
})
2254
}

0 commit comments

Comments
 (0)