-
Notifications
You must be signed in to change notification settings - Fork 0
/
queuemanager.go
66 lines (50 loc) · 1.68 KB
/
queuemanager.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
package Agora
import (
log "github.com/cihub/seelog"
"github.com/verma-kartik/Agora/message"
)
type queueManager struct {
queues map[string]*messageQueue
metricsManager *MetricsManager
closeNotificationChannel chan *string
}
func newQueueManager() *queueManager {
qm := queueManager{}
qm.queues = make(map[string]*messageQueue)
qm.closeNotificationChannel = make(chan *string, 10)
qm.metricsManager = NewMetricsManager(&qm)
go qm.listenForClosingQueues()
log.Debug("Initialized QueueManager")
return &qm
}
func (qm *queueManager) Publish(queueName string, message *message.Msg) {
// log.Debugf("Publishing message to %s: %s", queueName, message)
queueToPublishTo := qm.getQueueSafely(queueName)
queueToPublishTo.Publish(message)
}
func (qm *queueManager) Subscribe(queueName string, client *Client) {
log.Infof("%s subscribed to %s", client.Name, queueName)
queueToSubscribeTo := qm.getQueueSafely(queueName)
queueToSubscribeTo.AddSubscriber(client)
}
func (qm *queueManager) CloseQueue(queueName string) {
log.Infof("Closing %s", queueName)
queueToClose := qm.getQueueSafely(queueName)
queueToClose.Close()
}
func (qm *queueManager) getQueueSafely(queueName string) *messageQueue {
queueToReturn, present := qm.queues[queueName]
if !present {
newQueue := newMessageQueue(queueName, qm.metricsManager.metricsChannel, qm.closeNotificationChannel)
qm.queues[queueName] = newQueue
queueToReturn = qm.queues[queueName]
}
return queueToReturn
}
func (qm *queueManager) listenForClosingQueues() {
for {
closingQueue := <-qm.closeNotificationChannel
log.Debugf("Removing %s from active queues", *closingQueue)
delete(qm.queues, *closingQueue)
}
}