/
mux_server.go
229 lines (211 loc) · 6.54 KB
/
mux_server.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"sync"
)
type MuxServerIntf interface {
AddJobs(jobs *Jobs) error
GetJobs(qn QueueName, tn TopicName, typ QueueTyp) (*Jobs, error)
AddQueue(conf *QueueConfig) error
DelQueue(conf *QueueConfig) error
UpdateQueue(conf *QueueConfig) error
GetMuxer(qn QueueName, tn TopicName) (Muxer, error)
UpdateConfigs(map[QueueName]*QueueConfig) error
Serve()
}
// a group of mux
type MuxServer struct {
cmdC chan *Cmd
context.Context
sync.RWMutex // protect following
mux map[QueueName]map[TopicName]Muxer
queueConfig map[QueueName]*QueueConfig
}
func NewMuxServer(ctx context.Context) *MuxServer {
return &MuxServer{
mux: make(map[QueueName]map[TopicName]Muxer),
queueConfig: make(map[QueueName]*QueueConfig),
cmdC: make(chan *Cmd, 1000),
Context: ctx,
}
}
// one loop per MuxServer
func (ms *MuxServer) Serve() {
for {
select {
case <-ms.Done():
flog.Warn("mux server done: ", ms.Context.Err())
return
case cmd := <-ms.cmdC:
qconf, op, queueName := cmd.conf, cmd.op, cmd.conf.QueueName
switch op {
case CmdQueueAdd:
// create a new mux server
ms.Lock()
if _, ok := ms.queueConfig[queueName]; !ok {
ms.mux[queueName] = make(map[TopicName]Muxer)
ms.queueConfig[queueName] = qconf
for topicName, topicConf := range qconf.Topic {
storage := NewStorage(topicConf.Storage)
switch topicConf.RunType {
case RunTypeCGI:
mux := NewMux(ms.Context, queueName, topicName, NewCgiManager(topicConf.CgiConfigKey, topicConf.ScriptEntry), storage, topicConf.NumOfWorkers)
ms.mux[queueName][topicName] = mux
consumerEnable := !netQueueConf().ConsumerDisable && (qconf.ConsumerEnable || topicConf.ConsumerEnable)
if consumerEnable {
go mux.Serve()
}
flog.Warn("add queue ["+string(queueName)+"], topic ["+string(topicName)+"],", "cgiConf,", topicConf.CgiConfigKey, "consumerEnable", consumerEnable)
case RunTypeHttp:
sender := &HttpClientsManager{
path: topicConf.ScriptEntry,
Topic : topicConf,
client: &http.Client{
Transport: &http.Transport{
MaxIdleConns: int(topicConf.NumOfWorkers),
IdleConnTimeout: HttpConfigManager.IdleConnTimeout,
DisableCompression: true,
},
Timeout: HttpConfigManager.RequestTimeout,
},
}
mux := NewHttpWorker(ms.Context, queueName, topicName, int64(topicConf.NumOfWorkers), storage, sender)
ms.mux[queueName][topicName] = mux
consumerEnable := !netQueueConf().ConsumerDisable && (qconf.ConsumerEnable || topicConf.ConsumerEnable)
if consumerEnable {
go mux.Serve()
}
flog.Warn("add queue ["+string(queueName)+"], topic ["+string(topicName)+"],", "runType", topicConf.RunType, "consumerEnable", consumerEnable)
case RunTypeCmd:
mux := NewWorker(ms.Context, queueName, storage, topicConf)
ms.mux[queueName][topicName] = mux
consumerEnable := qconf.ConsumerEnable || topicConf.ConsumerEnable
if consumerEnable {
go mux.Serve()
}
flog.Warn("add queue ["+string(queueName)+"], topic ["+string(topicName)+"],", "workerConf", topicConf.WorkerConfig, "runType", topicConf.RunType, "consumerEnable", consumerEnable)
}
}
}
ms.Unlock()
case CmdQueueDel:
ms.Lock()
delete(ms.queueConfig, queueName)
for _, muxer := range ms.mux[queueName] {
muxer.Close()
}
delete(ms.mux, queueName)
ms.Unlock()
}
}
}
}
// 添加队列
func (ms *MuxServer) AddQueue(conf *QueueConfig) error {
ms.cmdC <- &Cmd{conf: conf, op: CmdQueueAdd}
return nil
}
// 删除队列
func (ms *MuxServer) DelQueue(conf *QueueConfig) error {
ms.cmdC <- &Cmd{conf: conf, op: CmdQueueDel}
return nil
}
// 更新队列,先删除后添加
func (ms *MuxServer) UpdateQueue(conf *QueueConfig) error {
ms.cmdC <- &Cmd{conf: conf, op: CmdQueueDel}
ms.cmdC <- &Cmd{conf: conf, op: CmdQueueAdd}
return nil
}
// return the status of queue info
func (ms *MuxServer) QueueStatus() {
ms.RLock()
defer ms.RUnlock()
for q, cmap := range ms.mux {
fmt.Println(q, len(ms.mux))
for c, _ := range cmap {
fmt.Println(c, len(cmap))
}
}
}
// add job to mux Storage
// one topic of queue refer to one mux server
//todo 添加job的时候初始化重试数据
func (ms *MuxServer) AddJobs(jobs *Jobs) error {
var err error
ms.RLock()
defer ms.RUnlock()
if _, ok := ms.mux[jobs.queueName]; !ok || len(ms.mux[jobs.queueName]) == 0 {
return errors.New("queue or Topic not exist " + string(jobs.queueName))
}
for tName, mux := range ms.mux[jobs.queueName] {
jobs.topicName = tName
for _,job := range jobs.Slice {
if(ms.queueConfig[jobs.queueName].Topic[jobs.topicName].IsRetry == true) {
job.RetryResidueTime = ms.queueConfig[jobs.queueName].Topic[jobs.topicName].RetryTime
job.RetryTimes = 0
//job.Retry = ms.queueConfig[jobs.queueName].Topic[jobs.topicName].RetryTimes
job.IsRetry = true
job.BeginRetry = false
}else{
job.RetryResidueTime = 0
job.RetryTimes = 0
//job.Retry = 0
job.IsRetry = false
job.BeginRetry = false
}
}
e := mux.AddJobs(jobs, CommonQueue)
if e != nil {
err = e
flog.Error("mux add jobs fail: queue:"+string(jobs.queueName)+" Topic "+string(jobs.topicName)+" error "+err.Error(), "jobs", jobs)
}
flog.Trace("AddJobs success: ", jobs)
}
return err
}
// get Jobs from mux storage
func (ms *MuxServer) GetJobs(qn QueueName, tn TopicName, typ QueueTyp) (*Jobs, error) {
ms.RLock()
defer ms.RUnlock()
if _, ok := ms.mux[qn]; !ok || len(ms.mux[qn]) == 0 {
return nil, errors.New("queue not exist " + string(qn))
}
mux, ok := ms.mux[qn][tn]
if !ok {
return nil, errors.New("topic not exist " + string(tn))
}
return mux.GetJobsFromQueue(qn, tn, typ)
}
func (ms *MuxServer) GetMuxer(qn QueueName, tn TopicName) (Muxer, error) {
ms.RLock()
defer ms.RUnlock()
if _, ok := ms.mux[qn]; !ok || len(ms.mux[qn]) == 0 {
return nil, errors.New("queue not exist " + string(qn))
}
mux, ok := ms.mux[qn][tn]
if !ok {
return nil, errors.New("topic not exist " + string(tn))
}
return mux, nil
}
func (ms *MuxServer) UpdateConfigs(queueConfs map[QueueName]*QueueConfig) error {
ms.RLock()
defer ms.RUnlock()
for name, conf := range queueConfs {
if old, ok := ms.queueConfig[name]; !ok {
ms.AddQueue(conf)
} else if !reflect.DeepEqual(conf, old) {
ms.UpdateQueue(conf)
}
}
for name, conf := range ms.queueConfig {
if _, ok := queueConfs[name]; !ok {
ms.DelQueue(conf)
}
}
return nil
}