forked from goodrain/rainbond
/
mq.go
132 lines (114 loc) · 3.31 KB
/
mq.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
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package mq
import (
"os"
"strings"
"sync"
"time"
"github.com/goodrain/rainbond/cmd/mq/option"
"golang.org/x/net/context"
etcdutil "github.com/goodrain/rainbond/util/etcd"
"github.com/Sirupsen/logrus"
"github.com/coreos/etcd/clientv3"
)
//ActionMQ 队列操作
type ActionMQ interface {
Enqueue(context.Context, string, string) error
Dequeue(context.Context, string) (string, error)
TopicIsExist(string) bool
GetAllTopics() []string
Start() error
Stop() error
}
var EnqueueNumber float64 = 0
var DequeueNumber float64 = 0
//NewActionMQ new etcd mq
func NewActionMQ(ctx context.Context, c option.Config) ActionMQ {
etcdQueue := etcdQueue{
config: c,
ctx: ctx,
queues: make(map[string]string),
}
return &etcdQueue
}
type etcdQueue struct {
config option.Config
ctx context.Context
queues map[string]string
queuesLock sync.Mutex
client *clientv3.Client
}
func (e *etcdQueue) Start() error {
logrus.Debug("etcd message queue client starting")
cli, err := clientv3.New(clientv3.Config{
Endpoints: e.config.EtcdEndPoints,
DialTimeout: time.Duration(e.config.EtcdTimeout) * time.Second,
})
if err != nil {
etcdutil.HandleEtcdError(err)
return err
}
e.client = cli
topics := os.Getenv("topics")
if topics != "" {
ts := strings.Split(topics, ",")
for _, t := range ts {
e.registerTopic(t)
}
}
e.registerTopic("worker")
e.registerTopic("builder")
logrus.Info("etcd message queue client started success")
return nil
}
//registerTopic 注册消息队列主题
func (e *etcdQueue) registerTopic(topic string) {
e.queuesLock.Lock()
defer e.queuesLock.Unlock()
e.queues[topic] = topic
}
func (e *etcdQueue) TopicIsExist(topic string) bool {
e.queuesLock.Lock()
defer e.queuesLock.Unlock()
_, ok := e.queues[topic]
return ok
}
func (e *etcdQueue) GetAllTopics() []string {
var topics []string
for k := range e.queues {
topics = append(topics, k)
}
return topics
}
func (e *etcdQueue) Stop() error {
if e.client != nil {
e.client.Close()
}
return nil
}
func (e *etcdQueue) queueKey(topic string) string {
return e.config.EtcdPrefix + "/" + topic
}
func (e *etcdQueue) Enqueue(ctx context.Context, topic, value string) error {
EnqueueNumber += 1
queue := etcdutil.NewQueue(e.client, e.queueKey(topic), ctx)
return queue.Enqueue(value)
}
func (e *etcdQueue) Dequeue(ctx context.Context, topic string) (string, error) {
DequeueNumber += 1
queue := etcdutil.NewQueue(e.client, e.queueKey(topic), ctx)
return queue.Dequeue()
}