forked from jpillora/velox
/
conn.go
182 lines (170 loc) · 4.03 KB
/
conn.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
package velox
import (
"fmt"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
)
//Conn represents a single live connection being synchronised.
//ID is current set to the connection's remote address.
type Conn interface {
ID() string
Connected() bool
Wait()
Push()
Close() error
}
type conn struct {
transport transport
state *State
connected bool
connectedCh chan struct{}
waiter sync.WaitGroup
id int64
addr string
first uint32
pushing uint32
queued uint32
uptime time.Time
version int64
sendingMut sync.Mutex //for msg/ping
}
func newConn(id int64, addr string, state *State, version int64) *conn {
return &conn{
connectedCh: make(chan struct{}),
id: id,
addr: addr,
state: state,
version: version,
}
}
//ID of this connection
func (c *conn) ID() string {
return strconv.FormatInt(c.id, 10)
}
//Status of this connection, should be true initially, then false after Wait().
func (c *conn) Connected() bool {
return c.connected
}
//Wait will block until the connection is closed.
func (c *conn) Wait() {
c.waiter.Wait()
}
//Push will the current state only to this client.
//Blocks until push is complete.
func (c *conn) Push() {
c.push()
}
//Force close the connection.
func (c *conn) Close() error {
return c.transport.close()
}
//connect using the provided transport
//and block until connection is ready
func (c *conn) connect(w http.ResponseWriter, r *http.Request) error {
//choose transport
if r.Header.Get("Accept") == "text/event-stream" {
c.transport = &eventSourceTransport{writeTimeout: c.state.WriteTimeout}
} else if r.Header.Get("Upgrade") == "websocket" {
c.transport = &websocketsTransport{writeTimeout: c.state.WriteTimeout}
} else {
return fmt.Errorf("Invalid sync request")
}
//non-blocking connect to client over set transport
if err := c.transport.connect(w, r); err != nil {
return err
}
//initial ping
if err := c.send(&update{Ping: true}); err != nil {
return fmt.Errorf("Failed to send initial event")
}
//successfully connected
c.connected = true
c.waiter.Add(1)
//while connected, ping loop (every 25s, browser timesout after 30s)
go func() {
for {
select {
case <-time.After(c.state.PingInterval):
if err := c.send(&update{Ping: true}); err != nil {
goto disconnected
}
case <-c.connectedCh:
goto disconnected
}
}
disconnected:
c.connected = false
c.Close()
//unblock waiters
c.waiter.Done()
}()
//non-blocking wait on connection
go func() {
if err := c.transport.wait(); err != nil {
//log error?
}
close(c.connectedCh)
}()
//now connected, consumer can connection.Wait()
return nil
}
func (c *conn) push() {
//attempt to mark state as 'pushing'
if !atomic.CompareAndSwapUint32(&c.pushing, 0, 1) {
//if already pushing, mark queued
atomic.StoreUint32(&c.queued, 1)
return
}
defer func() {
//no longer pushing
atomic.StoreUint32(&c.pushing, 0)
//queued? dequeue and push again
if atomic.CompareAndSwapUint32(&c.queued, 1, 0) {
c.push() //within same goroutine
}
}()
//current state data
d := &c.state.data
d.mut.RLock()
if c.version == d.version {
d.mut.RUnlock()
//already have this version
return
}
update := &update{Version: d.version}
//first push? include id
if atomic.CompareAndSwapUint32(&c.first, 0, 1) {
update.ID = d.id
}
//choose optimal update (send the smallest)
if d.delta != nil &&
c.version == (d.version-1) &&
len(d.bytes) > 0 &&
len(d.delta) < len(d.bytes) {
update.Delta = true
update.Body = d.delta
} else {
update.Delta = false
update.Body = d.bytes
}
d.mut.RUnlock()
//unlock data and send!
if err := c.send(update); err != nil {
c.Close()
return
}
//on success, relock and update client version
d.mut.RLock()
c.version = d.version
d.mut.RUnlock()
}
//send to connection, ensure only 1 concurrent sender
func (c *conn) send(upd *update) error {
c.sendingMut.Lock()
defer c.sendingMut.Unlock()
//send (transports responsiblity to enforce timeouts)
return c.transport.send(upd)
}