-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsubscribers.go
More file actions
72 lines (62 loc) · 1.33 KB
/
Copy pathsubscribers.go
File metadata and controls
72 lines (62 loc) · 1.33 KB
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
/*
* Copyright (c) 2016 Yanko Bolanos
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
package main
import (
log "github.com/Sirupsen/logrus"
"sync"
)
var gSubscribers map[string]Subscriber
var gSubMutex *sync.Mutex
func Publish(record *Record) {
select {
case gRecords <- record:
default:
log.Println("message was not sent")
}
}
type Subscriber struct {
SubscriberName string
Filename string
Ch chan *Record
}
func StartSubServer() chan *Record {
records := make(chan *Record, 1024)
gSubMutex = new(sync.Mutex)
gSubscribers = make(map[string]Subscriber)
go func() {
for {
r := <-records
for _, subs := range gSubscribers {
select {
case subs.Ch <- r:
default:
log.Println("message was not sent")
}
}
}
}()
return records
}
func AddSubscriber(s Subscriber) {
log.Printf("subscriber added: %v", s.SubscriberName)
gSubMutex.Lock()
defer gSubMutex.Unlock()
gSubscribers[s.SubscriberName] = s
}
func RemoveSubscriber(name string) {
log.Printf("subscriber removed: %v", name)
gSubMutex.Lock()
defer gSubMutex.Unlock()
for k, _ := range gSubscribers {
if k == name {
delete(gSubscribers, name)
return
}
}
}