-
Notifications
You must be signed in to change notification settings - Fork 257
/
broadcaster.go
155 lines (136 loc) · 3.68 KB
/
broadcaster.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
/*
Copyright 2019-2021 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package broadcaster
import (
"errors"
"sync"
)
type Operation string
const (
Created Operation = "Created"
Deleted Operation = "Deleted"
Updated Operation = "Updated"
)
type SocketData struct {
Kind string `json:"kind"`
Operation Operation `json:"operation"`
Payload interface{} `json:"payload"`
}
// Only a pointer to the struct should be used
type Broadcaster struct {
expired bool
// Explicit name to specify locking condition
expiredLock sync.Mutex
subscribers *sync.Map //map[*Subscriber]struct{}
c chan SocketData
}
// Wrapper return type for subscriptions
type Subscriber struct {
subChan chan SocketData
unsubChan chan struct{}
}
// Read-Only access to the subscription channel
// Open or nil, never closed
func (s *Subscriber) SubChan() <-chan SocketData {
return s.subChan
}
// Closed on unsubscribe or when broadcast parent channel closes
func (s *Subscriber) UnsubChan() <-chan struct{} {
return s.unsubChan
}
var errExpired error = errors.New("broadcaster expired")
// Creates broadcaster from channel parameter and immediately starts broadcasting
// Without any subscribers, received data will be discarded
// Broadcaster should be the only channel reader
func NewBroadcaster(c chan SocketData) *Broadcaster {
if c == nil {
panic("channel passed cannot be nil")
}
b := &Broadcaster{subscribers: new(sync.Map)}
b.c = c
go func() {
for {
msg, channelOpen := <-b.c
if channelOpen {
b.subscribers.Range(func(key, value interface{}) bool {
subscriber := key.(*Subscriber)
select {
case subscriber.subChan <- msg:
case <-subscriber.unsubChan:
}
return true
})
} else {
b.expiredLock.Lock()
b.expired = true
b.subscribers.Range(func(key, value interface{}) bool {
subscriber := key.(*Subscriber)
close(subscriber.unsubChan)
return true
})
// Remove references
b.subscribers = nil
b.expiredLock.Unlock()
return
}
}
}()
return b
}
func (b *Broadcaster) Expired() bool {
b.expiredLock.Lock()
defer b.expiredLock.Unlock()
return b.expired
}
// Subscriber expected to constantly consume or unsubscribe
func (b *Broadcaster) Subscribe() (*Subscriber, error) {
b.expiredLock.Lock()
defer b.expiredLock.Unlock()
if b.expired {
return &Subscriber{}, errExpired
}
newSub := &Subscriber{
subChan: make(chan SocketData),
unsubChan: make(chan struct{}),
}
// Generate unique key
b.subscribers.Store(newSub, struct{}{})
return newSub, nil
}
func (b *Broadcaster) Unsubscribe(sub *Subscriber) error {
b.expiredLock.Lock()
defer b.expiredLock.Unlock()
if b.expired {
return errExpired
}
if _, ok := b.subscribers.Load(sub); ok {
b.subscribers.Delete(sub)
close(sub.unsubChan)
return nil
}
return errors.New("subscription not found")
}
// Iterates over sync.Map and returns number of elements
// Response can be oversized if counted subscriptions are cancelled while counting
func (b *Broadcaster) PoolSize() (size int) {
b.expiredLock.Lock()
defer b.expiredLock.Unlock()
if b.expired {
return 0
}
b.subscribers.Range(func(key, value interface{}) bool {
size++
return true
})
return size
}