-
Notifications
You must be signed in to change notification settings - Fork 1
/
pubsub.go
143 lines (125 loc) · 2.97 KB
/
pubsub.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
package mgo_pubsub
import (
"crypto/tls"
"errors"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"net"
"strings"
"time"
)
type (
PubSub struct {
address string
database string
collection string
events map[string][]*chan string
sess *mgo.Session
}
Message struct {
Event string `json:"event" bson:"event"`
Body string `json:"body" bson:"body"`
Timestamp time.Time `json:"timestamp" bson:"timestamp"`
}
)
func session(address string) (*mgo.Session, error) {
dialInfo, err := mgo.ParseURL(address)
if err != nil {
return nil, err
}
if strings.Contains(strings.ToLower(address), "ssl=true") {
tlsConfig := &tls.Config{}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
}
return mgo.DialWithInfo(dialInfo)
}
func NewPubSub(address, database, collection string) (*PubSub, error) {
sess, err := session(address)
if err != nil {
return nil, err
}
return &PubSub{
address,
database,
collection,
map[string][]*chan string{},
sess,
}, nil
}
func (this *PubSub) Initialize() error {
this.sess.SetMode(mgo.Strong, true)
defer this.sess.SetMode(mgo.SecondaryPreferred, true)
err := this.sess.DB(this.database).C(this.collection).Create(&mgo.CollectionInfo{
Capped: true,
MaxBytes: 1024 * 1024 * 16,
MaxDocs: 1024,
})
if err != nil && strings.Contains(err.Error(), "collection '"+ this.database + "." + this.collection + "' already exists") {
return nil
}
return err
}
func (this *PubSub) StartPubSub() (err chan error) {
sleep := 100 * time.Millisecond
t := time.Now()
tail := this.sess.DB(this.database).C(this.collection).Find(bson.M{}).Tail(10 * time.Second)
defer func() {
if e := tail.Close(); e != nil {
err <- e
}
}()
go func() {
var message Message
for {
time.Sleep(sleep)
for tail.Next(&message) {
if message.Timestamp.After(t) {
for _, ch := range this.events[message.Event] {
*ch <- message.Body
}
}
}
if tail.Err() != nil {
err <- tail.Err()
_ = tail.Close()
break
}
if tail.Timeout() {
continue
}
}
}()
return
}
func (this *PubSub) Publish(event string, body string) error {
this.sess.SetMode(mgo.Strong, true)
defer this.sess.SetMode(mgo.SecondaryPreferred, true)
return this.sess.DB(this.database).C(this.collection).Insert(&Message{
Event: event,
Body: body,
Timestamp: time.Now(),
})
}
func (this *PubSub) Subscribe(event string) *chan string {
ch := make(chan string)
this.events[event] = append(this.events[event], &ch)
return &ch
}
func (this *PubSub) UnSubscribe(event string, ch *chan string) error {
index := -1
for i, c := range this.events[event] {
if ch == c {
index = i
break
}
}
if index == -1 {
return errors.New("pubsub unsubscribe error. maybe leak resources.")
}
this.events[event] = append(this.events[event][:index], this.events[event][index+1:]...)
close(*ch)
return nil
}