-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.go
More file actions
70 lines (57 loc) · 1.07 KB
/
loops.go
File metadata and controls
70 lines (57 loc) · 1.07 KB
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
package stomp
import (
"errors"
"time"
)
func (c *Conn) writeLoop() {
for {
select {
case <-c.closeC:
return
case <-timeout(c.whb):
err := c.unsafeWrite(&Frame{})
if err != nil {
c.error(err)
}
case frame := <-c.writeC:
err := c.unsafeWrite(frame.body, frame.options...)
if err != nil {
c.error(err)
}
frame.ch <- err
}
}
}
func (c *Conn) readLoop() {
for {
select {
case <-c.closeC:
return
case <-timeout(2 * c.rhb):
c.error(errors.New("no heartbeat received"))
case frame := <-c.safeRead():
switch frame.Command {
case "MESSAGE":
c.dispatchMessage(frame)
case "ERROR":
c.error(NewError(frame))
}
//NOTE: if a default case is created, do not forget
// about an empty frame (=heartbeat)
}
}
}
func timeout(d time.Duration) <-chan time.Time {
if d == 0 {
return make(chan time.Time)
}
return time.After(d)
}
func (c *Conn) dispatchMessage(frame *Frame) {
c.subsMu.Lock()
msg := &Message{*frame}
if sub, ok := c.subs[msg.Subscription()]; ok {
sub.C <- msg
}
c.subsMu.Unlock()
}