/
realtime_channel.go
393 lines (356 loc) · 11.1 KB
/
realtime_channel.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package ably
import (
"errors"
"fmt"
"sort"
"sync"
"github.com/ably/ably-go/ably/proto"
)
var (
errAttach = errors.New("attempted to attach channel to inactive connection")
errDetach = errors.New("attempted to detach channel from inactive connection")
)
type chanSlice []*RealtimeChannel
func (ch chanSlice) Len() int { return len(ch) }
func (ch chanSlice) Less(i, j int) bool { return ch[i].Name < ch[j].Name }
func (ch chanSlice) Swap(i, j int) { ch[i], ch[j] = ch[j], ch[i] }
func (ch chanSlice) Sort() { sort.Sort(ch) }
// Channels is a goroutine-safe container for realtime channels that allows
// for creating, deleting and iterating over existing channels.
type Channels struct {
mtx sync.Mutex
client *RealtimeClient
chans map[string]*RealtimeChannel
}
func newChannels(client *RealtimeClient) *Channels {
return &Channels{
client: client,
chans: make(map[string]*RealtimeChannel),
}
}
// Get looks up a channel given by the name and creates it if it does not exist
// already.
//
// It is safe to call Get from multiple goroutines - a single channel is
// guaranteed to be created only once for multiple calls to Get from different
// goroutines.
func (ch *Channels) Get(name string) *RealtimeChannel {
ch.mtx.Lock()
c, ok := ch.chans[name]
if !ok {
c = newRealtimeChannel(name, ch.client)
ch.chans[name] = c
}
ch.mtx.Unlock()
return c
}
// All returns a list of created channels.
//
// It is safe to call All from multiple goroutines, however there's no guarantee
// the returned list would not list a channel that was already released from
// different goroutine.
//
// The returned list is sorted by channel names.
func (ch *Channels) All() []*RealtimeChannel {
ch.mtx.Lock()
chans := make([]*RealtimeChannel, 0, len(ch.chans))
for _, c := range ch.chans {
chans = append(chans, c)
}
ch.mtx.Unlock()
chanSlice(chans).Sort()
return chans
}
// Release closes a channel looked up by the name.
//
// It is safe to call Release from multiple goroutines - if a channel happened
// to be already concurrently released, the method is a nop.
func (ch *Channels) Release(name string) error {
ch.mtx.Lock()
defer ch.mtx.Unlock()
if c, ok := ch.chans[name]; ok {
delete(ch.chans, name)
return c.Close()
}
return nil
}
func (ch *Channels) broadcastConnStateChange(state State) {
ch.mtx.Lock()
defer ch.mtx.Unlock()
for _, c := range ch.chans {
c.onConnState(state)
}
}
// RealtimeChannel represents a single named message channel.
type RealtimeChannel struct {
Name string // name used to create the channel
Presence *RealtimePresence //
client *RealtimeClient
state *stateEmitter
subs *subscriptions
queue *msgQueue
listen chan State
}
func newRealtimeChannel(name string, client *RealtimeClient) *RealtimeChannel {
c := &RealtimeChannel{
Name: name,
client: client,
state: newStateEmitter(StateChan, StateChanInitialized, name, client.logger()),
subs: newSubscriptions(subscriptionMessages, client.logger()),
listen: make(chan State, 1),
}
c.Presence = newRealtimePresence(c)
c.queue = newMsgQueue(client.Connection)
if c.opts().Listener != nil {
c.On(c.opts().Listener)
}
return c
}
func (c *RealtimeChannel) onConnState(state State) {
c.state.Lock()
active := c.isActive()
c.state.Unlock()
switch state.State {
case StateConnFailed:
if active {
c.state.syncSet(StateChanFailed, state.Err)
}
case StateConnClosed:
if active {
c.state.syncSet(StateChanClosed, state.Err)
}
}
}
// Attach initiates attach request, which is being processed on a separate
// goroutine.
//
// If channel is already attached, this method is a nop.
// If sending attach message failed, the returned error value is non-nil.
// If sending attach message succeed, the returned Result value can be used
// to wait until ack from server is received.
func (c *RealtimeChannel) Attach() (Result, error) {
return c.attach(true)
}
var attachResultStates = []StateEnum{
StateChanAttached, // expected state
StateChanClosing,
StateChanClosed,
StateChanFailed,
}
func (c *RealtimeChannel) attach(result bool) (Result, error) {
return c.mayAttach(result, true)
}
func (c *RealtimeChannel) mayAttach(result, checkActive bool) (Result, error) {
c.state.Lock()
defer c.state.Unlock()
if checkActive {
if c.isActive() {
return nopResult, nil
}
}
if !c.client.Connection.lockIsActive() {
return nil, c.state.set(StateChanFailed, errAttach)
}
c.state.set(StateChanAttaching, nil)
var res Result
if result {
res = c.state.listenResult(attachResultStates...)
}
msg := &proto.ProtocolMessage{
Action: proto.ActionAttach,
Channel: c.state.channel,
}
err := c.client.Connection.send(msg, nil)
if err != nil {
return nil, c.state.set(StateChanFailed, err)
}
return res, nil
}
// Detach initiates detach request, which is being processed on a separate
// goroutine.
//
// If channel is already detached, this method is a nop.
// If sending detach message failed, the returned error value is non-nil.
// If sending detach message succeed, the returned Result value can be used
// to wait until ack from server is received.
func (c *RealtimeChannel) Detach() (Result, error) {
return c.detach(true)
}
var detachResultStates = []StateEnum{
StateChanDetached, // expected state
StateChanClosing,
StateChanClosed,
StateChanFailed,
}
func (c *RealtimeChannel) detach(result bool) (Result, error) {
c.state.Lock()
defer c.state.Unlock()
switch {
case c.state.current == StateChanFailed:
return nil, stateError(StateChanFailed, errDetach)
case !c.isActive():
return nopResult, nil
}
if !c.client.Connection.lockIsActive() {
return nil, c.state.set(StateChanFailed, errDetach)
}
c.state.set(StateChanDetaching, nil)
var res Result
if result {
res = c.state.listenResult(detachResultStates...)
}
msg := &proto.ProtocolMessage{
Action: proto.ActionDetach,
Channel: c.state.channel,
}
err := c.client.Connection.send(msg, nil)
if err != nil {
return nil, c.state.set(StateChanFailed, err)
}
return res, nil
}
// Closes initiates closing sequence for the channel; it waits until the
// operation is complete.
//
// If connection is already closed, this method is a nop.
// If sending close message succeeds, it closes and unsubscribes all channels.
func (c *RealtimeChannel) Close() error {
err := wait(c.Detach())
c.subs.close()
if err != nil {
return c.state.syncSet(StateChanClosed, err)
}
return nil
}
// Subscribe subscribes to a realtime channel, which makes any newly received
// messages relayed to the returned Subscription value.
//
// If no names are given, returned Subscription will receive all messages.
// If ch is non-nil and it was already registered to receive messages with different
// names than the ones given, it will be added to receive also the new ones.
func (c *RealtimeChannel) Subscribe(names ...string) (*Subscription, error) {
if _, err := c.attach(false); err != nil {
return nil, err
}
return c.subs.subscribe(namesToKeys(names)...)
}
// Unsubscribe removes previous Subscription for the given message names.
//
// Unsubscribe panics if the given sub was subscribed for presence messages and
// not for regular channel messages.
//
// If sub was already unsubscribed, the method is a nop.
func (c *RealtimeChannel) Unsubscribe(sub *Subscription, names ...string) {
if sub.typ != subscriptionMessages {
panic(errInvalidType{typ: sub.typ})
}
c.subs.unsubscribe(true, sub, namesToKeys(names)...)
}
// On relays request channel states to c; on state transition
// connection will not block sending to c - the caller must ensure the incoming
// values are read at proper pace or the c is sufficiently buffered.
//
// If no states are given, c is registered for all of them.
// If c is nil, the method panics.
// If c is already registered, its state set is expanded.
func (c *RealtimeChannel) On(ch chan<- State, states ...StateEnum) {
c.state.on(ch, states...)
}
// Off removes c from listening on the given channel state transitions.
//
// If no states are given, c is removed for all of the connection's states.
// If c is nil, the method panics.
func (c *RealtimeChannel) Off(ch chan<- State, states ...StateEnum) {
c.state.off(ch, states...)
}
// Publish publishes a message on the channel, which is send on separate
// goroutine. Publish does not block.
//
// This implicitly attaches the channel if it's not already attached.
func (c *RealtimeChannel) Publish(name string, data interface{}) (Result, error) {
return c.PublishAll([]*proto.Message{{Name: name, Data: data}})
}
// PublishAll publishes all given messages on the channel at once.
// PublishAll does not block.
//
// This implicitly attaches the channel if it's not already attached.
func (c *RealtimeChannel) PublishAll(messages []*proto.Message) (Result, error) {
id := c.client.Auth.clientIDForCheck()
for _, v := range messages {
if v.ClientID != "" && id != wildcardClientID && v.ClientID != id {
// Spec RSL1g3,RSL1g4
return nil, fmt.Errorf("Unable to publish message containing a clientId (%s) that is incompatible with the library clientId (%s)", v.ClientID, id)
}
}
msg := &proto.ProtocolMessage{
Action: proto.ActionMessage,
Channel: c.state.channel,
Messages: messages,
}
return c.send(msg)
}
// History gives the channel's message history according to the given parameters.
// The returned result can be inspected for the messages via the Messages()
// method.
func (c *RealtimeChannel) History(params *PaginateParams) (*PaginatedResult, error) {
return c.client.rest.Channels.Get(c.Name, nil).History(params)
}
func (c *RealtimeChannel) send(msg *proto.ProtocolMessage) (Result, error) {
if _, err := c.attach(false); err != nil {
return nil, err
}
res, listen := newErrResult()
switch c.State() {
case StateChanInitialized, StateChanAttaching:
c.queue.Enqueue(msg, listen)
return res, nil
case StateChanAttached:
default:
return nil, &Error{Code: 90001}
}
if err := c.client.Connection.send(msg, listen); err != nil {
return nil, err
}
return res, nil
}
// State gives current state of the channel.
func (c *RealtimeChannel) State() StateEnum {
c.state.Lock()
defer c.state.Unlock()
return c.state.current
}
// Reason gives the last error that caused channel transition to failed state.
func (c *RealtimeChannel) Reason() error {
c.state.Lock()
defer c.state.Unlock()
return c.state.err
}
func (c *RealtimeChannel) notify(msg *proto.ProtocolMessage) {
switch msg.Action {
case proto.ActionAttached:
c.Presence.onAttach(msg)
c.state.syncSet(StateChanAttached, nil)
c.queue.Flush()
case proto.ActionDetached:
c.state.syncSet(StateChanDetached, nil)
case proto.ActionSync:
c.Presence.processIncomingMessage(msg, syncSerial(msg))
case proto.ActionPresence:
c.Presence.processIncomingMessage(msg, "")
case proto.ActionError:
c.state.syncSet(StateChanFailed, newErrorProto(msg.Error))
c.queue.Fail(newErrorProto(msg.Error))
case proto.ActionMessage:
c.subs.messageEnqueue(msg)
default:
}
}
func (c *RealtimeChannel) isActive() bool {
return c.state.current == StateChanAttaching || c.state.current == StateChanAttached
}
func (c *RealtimeChannel) opts() *ClientOptions {
return c.client.opts()
}
func (c *RealtimeChannel) logger() *LoggerOptions {
return c.client.logger()
}