/
sub.go
116 lines (100 loc) · 2.72 KB
/
sub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package aliyunfc
import (
"encoding/json"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/hibiken/asynq"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
const (
AliyunFCSubName = "aliyun_fc"
)
type aliyunFCSub struct {
routers map[string]*gin.Engine
config map[string]*aliyunFCSubConfig
mux map[string]*asynq.ServeMux
g errgroup.Group
}
var (
aliyunFCSubInstance *aliyunFCSub
subOnce sync.Once
)
func getAliyunFCSub() *aliyunFCSub {
subOnce.Do(func() {
aliyunFCSubInstance = &aliyunFCSub{
routers: make(map[string]*gin.Engine),
config: make(map[string]*aliyunFCSubConfig),
mux: make(map[string]*asynq.ServeMux),
}
})
return aliyunFCSubInstance
}
func GetMux(name string) *asynq.ServeMux {
registerSub()
return getAliyunFCSub().mux[name]
}
func (q *aliyunFCSub) Name() string { return AliyunFCSubName }
func (q *aliyunFCSub) Init(config map[string]any) error {
for name, raw := range config {
r := gin.Default()
r.ContextWithFallback = true
mux := asynq.NewServeMux()
tmpName := name
r.POST("/involke", involkeHandler(tmpName, mux))
q.routers[name] = r
// 解析配置
conf := &aliyunFCSubConfig{}
if err := mapstructure.Decode(raw, conf); err != nil {
return err
}
q.config[name] = conf
q.mux[name] = mux
}
return nil
}
func (q *aliyunFCSub) Run() error {
for k := range q.routers {
name := k
q.g.Go(func() error {
server := http.Server{
Addr: q.config[name].Addr,
Handler: q.routers[name],
ReadTimeout: time.Duration(q.config[name].ReadTimeout) * time.Millisecond,
WriteTimeout: time.Duration(q.config[name].WriteTimeout) * time.Millisecond,
}
return server.ListenAndServe()
})
}
return q.g.Wait()
}
type Event struct {
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
}
func involkeHandler(name string, mux *asynq.ServeMux) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
logger := logrus.WithContext(ctx)
event := &Event{}
if err := ctx.BindJSON(event); err != nil {
logger.Errorf("fail to parse body to event, err: %v", err)
ctx.JSON(http.StatusNotFound, err)
ctx.Header(FCStatusHeader, "404")
return
}
logger.Infof("mux %s receive event with type %s", name, event.Type)
logger.Debugf("mux %s receive event, type %s with payload %s", name, event.Type, string(event.Payload))
task := asynq.NewTask(event.Type, event.Payload)
if err := mux.ProcessTask(ctx, task); err != nil {
logger.Errorf("fail to parse body to event, err: %v", err)
ctx.JSON(http.StatusNotFound, err)
ctx.Header(FCStatusHeader, "404")
return
}
ctx.JSON(http.StatusOK, gin.H{})
ctx.Header(FCStatusHeader, "200")
}
}