/
broadcast.go
195 lines (152 loc) · 4.5 KB
/
broadcast.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
// Copyright 2022 Fluidity Money. All rights reserved. Use of this
// source code is governed by a GPL-style license that can be found in the
// LICENSE.md file.
package websocket
import (
"encoding/json"
"github.com/fluidity-money/fluidity-app/lib/log"
)
// ContextBroadcast used to identify the broadcast server in logging
const ContextBroadcast = "WEBSOCKET/BROADCAST"
type (
// registration for a broadcast. If replies is nil, then it's assumed
// that someone is unsubscribing!
registration struct {
cookieReply chan uint64
replies chan []byte
}
// Broadcast for sending messages to channels subscribing to events here
Broadcast struct {
broadcastRequests chan []byte
subscriptionRequests chan registration
unsubscriptionRequests chan uint64
shutdownRequests chan bool
subscribedCount uint64
subscribed map[uint64]chan []byte
}
)
// NewBroadcast, creating a new map and new counter for messages and set
// up the server that handles new subscriptions.
func NewBroadcast() *Broadcast {
var (
broadcastRequests = make(chan []byte)
subscriptionRequests = make(chan registration)
unsubscriptionRequests = make(chan uint64)
shutdownRequests = make(chan bool)
)
broadcast := Broadcast{
broadcastRequests: broadcastRequests,
subscriptionRequests: subscriptionRequests,
unsubscriptionRequests: unsubscriptionRequests,
shutdownRequests: shutdownRequests,
subscribedCount: 0,
subscribed: make(map[uint64]chan []byte),
}
go func() {
for {
select {
case message := <-broadcastRequests:
log.Debug(func(k *log.Log) {
k.Context = ContextBroadcast
k.Message = "Received a message to broadcast!"
})
for cookie, subscribed := range broadcast.subscribed {
if subscribed == nil {
delete(broadcast.subscribed, cookie)
continue
}
select {
case subscribed <- message:
default:
}
}
case subscription := <-subscriptionRequests:
var (
cookieReply = subscription.cookieReply
replies = subscription.replies
)
previous := broadcast.incrementCookie()
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Received a request to subscribe with cookie!"
k.Payload = previous
})
cookieReply <- previous
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Done sending a message with the cookie to the request to subscribe!"
k.Payload = previous
})
broadcast.subscribed[previous] = replies
case cookie := <-unsubscriptionRequests:
log.Debug(func(k *log.Log) {
k.Context = Context
k.Format(
"Cookie %#v has sent a request to unsubscribe!",
cookie,
)
})
broadcast.subscribed[cookie] = nil
case _ = <-shutdownRequests:
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Received a request to shutdown the broadcast server!"
})
return
}
}
}()
return &broadcast
}
func (broadcast *Broadcast) incrementCookie() (previous uint64) {
previous = broadcast.subscribedCount
broadcast.subscribedCount++
return previous
}
// Subscribe to the broadcast, with a new channel receiving messages
// being sent
func (broadcast Broadcast) Subscribe(messages chan []byte) uint64 {
cookieChan := make(chan uint64)
var subscriptionId int
if log.DebugEnabled() {
subscriptionId = generateSubscriptionId()
}
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Subscribe to the channel request to receiving messages with subscription id!"
k.Payload = subscriptionId
})
broadcast.subscriptionRequests <- registration{
cookieReply: cookieChan,
replies: messages,
}
log.Debug(func(k *log.Log) {
k.Context = Context
k.Message = "Received a response with subscription id!"
k.Payload = subscriptionId
})
return <-cookieChan
}
func (broadcast Broadcast) Unsubscribe(cookie uint64) {
broadcast.unsubscriptionRequests <- cookie
}
// Shutdown the broadcast, closing the inner worker and setting the struct
// to nil
func (broadcast *Broadcast) Shutdown() {
broadcast.shutdownRequests <- true
broadcast = nil
}
func (broadcast Broadcast) Broadcast(message []byte) {
broadcast.broadcastRequests <- message
}
func (broadcast Broadcast) BroadcastJson(content interface{}) {
blob, err := json.Marshal(content)
if err != nil {
log.Fatal(func(k *log.Log) {
k.Context = Context
k.Message = "Failed to encode an interface to JSON!"
k.Payload = err
})
}
broadcast.Broadcast(blob)
}