/
state.go
210 lines (197 loc) · 4.66 KB
/
state.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
package velox
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"sync"
"sync/atomic"
"time"
"gomodules.xyz/jsonpatch/v3"
)
//Pusher implements a push method,
//similar to Flush
type Pusher interface {
Push() bool
}
var (
//MinThrottle is the minimum manual State.Throttle value.
//15ms is approximately highest resolution on the JS eventloop.
MinThrottle = 15 * time.Millisecond
//DefaultThrottle is the default State.Throttle value.
DefaultThrottle = 200 * time.Millisecond
//DefaultWriteTimeout is the default State.Throttle value.
DefaultWriteTimeout = 30 * time.Second
//DefaultPingInterval is the default State.PingInterval value.
DefaultPingInterval = 25 * time.Second
)
//State must be embedded into a struct to make it syncable.
type State struct {
//configuration
Throttle time.Duration `json:"-"`
WriteTimeout time.Duration `json:"-"`
PingInterval time.Duration `json:"-"`
//internal state
initMut sync.Mutex
initd bool
gostruct interface{}
connMut sync.Mutex
conns map[int64]*conn
data struct {
mut sync.RWMutex
id string //data id != conn id
bytes []byte
delta []byte
version int64
}
push struct {
mut sync.Mutex
ing uint32
queued uint32
}
}
func (s *State) init(gostruct interface{}) error {
if s.Throttle < MinThrottle {
s.Throttle = DefaultThrottle
}
if s.WriteTimeout == 0 {
s.WriteTimeout = DefaultWriteTimeout
}
if s.PingInterval == 0 {
s.PingInterval = DefaultPingInterval
}
//get initial JSON bytes and confirm gostruct is marshallable
l, ok := gostruct.(sync.Locker)
if ok {
l.Lock()
defer l.Unlock()
}
b, err := json.Marshal(gostruct)
if err != nil {
return fmt.Errorf("JSON marshalling failed: %s", err)
}
s.data.bytes = b
id := make([]byte, 4)
if n, _ := rand.Read(id); n > 0 {
s.data.id = hex.EncodeToString(id)
}
s.gostruct = gostruct
s.data.version = 1
s.conns = map[int64]*conn{}
s.initd = true
return nil
}
//ID uniquely identifies this state object
func (s *State) ID() string {
return s.data.id
}
//Version of this state object (when the underlying struct is
//and a Push is performed, this version number is incremented).
func (s *State) Version() int64 {
return s.data.version
}
func (s *State) sync(gostruct interface{}) (*State, error) {
s.initMut.Lock()
defer s.initMut.Unlock()
if !s.initd {
if err := s.init(gostruct); err != nil {
return nil, err
}
} else if s.gostruct != gostruct {
return nil, errors.New("A different struct is already synced")
}
return s, nil
}
func (s *State) subscribe(conn *conn) {
//subscribe
conn.waiter.Add(1)
s.connMut.Lock()
s.conns[conn.id] = conn
s.connMut.Unlock()
//and then unsubscribe on close
go func() {
<-conn.connectedCh //this unblocks before wait
s.connMut.Lock()
delete(s.conns, conn.id)
s.connMut.Unlock()
conn.waiter.Done()
}()
}
//NumConnections currently active
func (s *State) NumConnections() int {
s.connMut.Lock()
n := len(s.conns)
s.connMut.Unlock()
return n
}
//Push the changes from this object to all connected clients.
//Push is thread-safe and is throttled so it can be called
//with abandon. Returns false if a Push is already in progress.
func (s *State) Push() bool {
//attempt to mark state as 'pushing'
if atomic.CompareAndSwapUint32(&s.push.ing, 0, 1) {
go s.gopush()
return true
}
//if already pushing, mark queued
atomic.StoreUint32(&s.push.queued, 1)
return false
}
//non-blocking push
func (s *State) gopush() {
s.push.mut.Lock()
t0 := time.Now()
//queue cleanup
defer func() {
//measure time passed, ensure we wait at least Throttle time
tdelta := time.Now().Sub(t0)
if t := s.Throttle - tdelta; t > 0 {
time.Sleep(t)
}
//push complete
s.push.mut.Unlock()
atomic.StoreUint32(&s.push.ing, 0)
//if queued, auto-push again
if atomic.CompareAndSwapUint32(&s.push.queued, 1, 0) {
s.Push()
}
}()
//calculate new json state
l, hasLock := s.gostruct.(sync.Locker)
if hasLock {
l.Lock()
}
newBytes, err := json.Marshal(s.gostruct)
if hasLock {
l.Unlock()
}
if err != nil {
log.Printf("velox: marshal failed: %s", err)
return
}
//if changed, then calculate change set
if !bytes.Equal(s.data.bytes, newBytes) {
//calculate change set from last version
ops, _ := jsonpatch.CreatePatch(s.data.bytes, newBytes)
if len(s.data.bytes) > 0 && len(ops) > 0 {
//changes! bump version
s.data.mut.Lock()
s.data.delta, _ = json.Marshal(ops)
s.data.bytes = newBytes
s.data.version++
s.data.mut.Unlock()
}
}
//send this new change to each subscriber
s.connMut.Lock()
for _, c := range s.conns {
if c.version != s.data.version {
go c.push()
}
}
s.connMut.Unlock()
//defered cleanup()
}