forked from antage/eventsource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventsource.go
196 lines (166 loc) · 3.72 KB
/
eventsource.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
package eventsource
import (
"bytes"
"fmt"
"net/http"
)
const (
consumerBufSize = 10
defaultHistorySize = 50
)
type consumer struct {
id int
ch chan []byte
quit chan bool
}
type message struct {
event string
data []byte
}
type eventSource struct {
// History of last events.
H [][]byte
// Value of last event's id sent.
id int
sink chan *message
add chan *consumer
remove chan *consumer
close chan bool
consumers map[*consumer]bool
}
// EventSource interface provides methods for sending messages
// and closing all connections.
type EventSource interface {
// It should implement ServerHTTP method.
http.Handler
// Send message to all consumers.
SendMessage(data []byte, event string)
// Curried version of SendMessage, bound
// to a unique event namespace
Sender(event string) func(data []byte)
// Consumers count
ConsumersCount() int
// Close and clear all consumers.
Close()
// Subscribe a new consumer to eventsource
// wth the given last received event id (or -1)
Subscribe(id int) *consumer
// Unsubscribe the given consumer
Unsubscribe(c *consumer)
}
func newMessage(data []byte, event string) *message {
m := &message{
event: event,
data: make([]byte, len(data)),
}
copy(m.data, data)
return m
}
func (m *message) prepare(id int) []byte {
var buf bytes.Buffer
fmt.Fprintf(&buf, "id: %d\n", id)
fmt.Fprintf(&buf, "event: %s\n", m.event)
fmt.Fprintf(&buf, "data: %s\n\n", m.data)
return buf.Bytes()
}
func (es *eventSource) loop() {
for {
select {
// New message from the sink, broadcasted to consumers.
case m := <-es.sink:
b := m.prepare(es.id)
for c := range es.consumers {
select {
case c.ch <- b:
default:
}
}
es.appendHistory(b)
es.id++
// New consumer added to consumer list.
// Send missed events to the consumer if any.
case c := <-es.add:
es.consumers[c] = true
select {
case c.ch <- es.missedEvents(c):
default:
}
// Remove one consumer.
case c := <-es.remove:
delete(es.consumers, c)
close(c.ch)
close(c.quit)
// Close eventsource's channels and consumers.
case <-es.close:
close(es.sink)
close(es.add)
close(es.close)
for c := range es.consumers {
c.quit <- true
}
close(es.remove)
return
}
}
}
// New creates new EventSource instance.
func New() EventSource {
es := &eventSource{
H: make([][]byte, 0, defaultHistorySize),
id: 0,
sink: make(chan *message, 1),
add: make(chan *consumer),
remove: make(chan *consumer, 1),
close: make(chan bool),
consumers: make(map[*consumer]bool),
}
go es.loop()
return es
}
func (es *eventSource) Subscribe(id int) *consumer {
c := &consumer{
id: id,
ch: make(chan []byte, consumerBufSize),
quit: make(chan bool),
}
es.add <- c
return c
}
func (es *eventSource) Unsubscribe(c *consumer) {
es.remove <- c
}
func (es *eventSource) Close() {
es.close <- true
}
func (es *eventSource) SendMessage(data []byte, event string) {
es.sink <- newMessage(data, event)
}
func (es *eventSource) ConsumersCount() int {
return len(es.consumers)
}
func (es *eventSource) Sender(event string) func(data []byte) {
return func(data []byte) {
es.SendMessage(data, event)
}
}
// ServeHTTP implements http.Handler interface.
func (es *eventSource) ServeHTTP(w http.ResponseWriter, r *http.Request) {
consumerHandler(w, r, es)
}
func (es *eventSource) appendHistory(b []byte) {
if len(es.H) < defaultHistorySize {
es.H = append(es.H, b)
} else {
es.H = append(es.H[1:], b)
}
}
func (es *eventSource) missedEvents(c *consumer) []byte {
if c.id < 0 || c.id >= es.id {
return nil
}
i := c.id - es.id + len(es.H)
if i < 0 {
i = 0
}
return bytes.Join(es.H[i:], []byte{})
}