-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscriber.go
98 lines (80 loc) · 2.16 KB
/
subscriber.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
/* 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 broadcast
import (
"sync"
)
// Subscriber ...
type Subscriber struct {
id string
quit chan *Subscriber
replay chan *Connection
connections []*Connection
mu sync.Mutex
}
// NewSubscriber creates a new subscriber with defaults
func NewSubscriber(id string) *Subscriber {
return &Subscriber{
id: id,
connections: make([]*Connection, 0),
}
}
// Broadcast an event to all of a subscribers connections
func (s *Subscriber) Broadcast(e *Event) {
s.mu.Lock()
defer s.mu.Unlock()
for i := range s.connections {
s.connections[i].Send(e)
}
}
// Connect creates a new connection channel on a subscriber
func (s *Subscriber) Connect() chan *Event {
return s.ConnectAtID("0")
}
// ConnectAtID creates a new connection and replays events from a given event id
func (s *Subscriber) ConnectAtID(id string) chan *Event {
s.mu.Lock()
defer s.mu.Unlock()
c := Connection{
conn: make(chan *Event, 64),
eventid: id,
}
s.connections = append(s.connections, &c)
go func() {
s.replay <- &c
}()
return c.conn
}
// Disconnect a subscriber connection from the subscriber
func (s *Subscriber) Disconnect(c chan *Event) {
s.mu.Lock()
defer s.mu.Unlock()
for i := len(s.connections) - 1; i >= 0; i-- {
if s.connections[i].conn == c {
close(c)
s.connections = append(s.connections[:i], s.connections[i+1:]...)
}
}
}
// DisconnectAll closes all subscriber connections
func (s *Subscriber) DisconnectAll() {
s.mu.Lock()
defer s.mu.Unlock()
for i := len(s.connections) - 1; i >= 0; i-- {
if s.connections[i] != nil {
close(s.connections[i].conn)
}
s.connections = append(s.connections[:i], s.connections[i+1:]...)
}
}
// HasConnections returns true if there are any subscriber connections
func (s *Subscriber) HasConnections() bool {
return len(s.connections) > 0
}
// Close will let the stream know that the clients connection has terminated
func (s *Subscriber) Close() {
if s.quit != nil {
s.quit <- s
}
}