-
Notifications
You must be signed in to change notification settings - Fork 1
/
jaws.go
407 lines (375 loc) · 10.9 KB
/
jaws.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Package jaws provides a mechanism to create dynamic
// webpages using Javascript and WebSockets.
//
// It integrates well with Go's html/template package,
// but can be used without it. It can be used with any
// router that supports the standard ServeHTTP interface.
//
// It comes with a small package 'jawsecho' that
// integrates with Echo and also doubles as an example
// for integration with other routers.
package jaws
import (
"bufio"
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
)
type Jaws struct {
Logger *log.Logger // If not nil, send debug info and errors here
doneCh <-chan struct{}
bcastCh chan *Message
subCh chan chan *Message
unsubCh chan chan *Message
nextId uint64
mu sync.Mutex // protects following
kg *bufio.Reader
closeCh chan struct{}
reqs map[uint64]*Request
}
// NewWithDone returns a new JaWS object using the given completion channel.
// This is expected to be created once per HTTP server and handles
// publishing HTML changes across all connections.
func NewWithDone(doneCh <-chan struct{}) *Jaws {
return &Jaws{
doneCh: doneCh,
bcastCh: make(chan *Message, 1),
subCh: make(chan chan *Message, 1),
unsubCh: make(chan chan *Message, 1),
kg: bufio.NewReader(rand.Reader),
reqs: make(map[uint64]*Request),
}
}
// New returns a new JaWS object that must be closed using Close().
// This is expected to be created once per HTTP server and handles
// publishing HTML changes across all connections.
func New() (jw *Jaws) {
closeCh := make(chan struct{})
jw = NewWithDone(closeCh)
jw.closeCh = closeCh
return
}
// Close frees resources associated with the JaWS object, and
// closes the completion channel if the JaWS was created with New().
// Once the completion channel is closed, broadcasts and sends are discarded.
// Subsequent calls to Close() have no effect.
func (jw *Jaws) Close() {
jw.mu.Lock()
if jw.closeCh != nil {
close(jw.closeCh)
jw.closeCh = nil
}
jw.mu.Unlock()
}
// Done returns the completion channel.
func (jw *Jaws) Done() <-chan struct{} {
return jw.doneCh
}
// Log sends an error to the Logger set in the Jaws.
// Has no effect if the err is nil or the Logger is nil.
// Returns err.
func (jw *Jaws) Log(err error) error {
if err != nil && jw != nil && jw.Logger != nil {
jw.Logger.Println(err.Error())
}
return err
}
// MustLog sends an error to the Logger set in the Jaws or
// panics with the given error if no Logger is set.
// Has no effect if the err is nil.
func (jw *Jaws) MustLog(err error) {
if err != nil {
if jw != nil && jw.Logger != nil {
jw.Logger.Println(err.Error())
} else {
panic(err)
}
}
}
// MakeID returns a string in the form 'jaws.X' where X is a string unique within the Jaws lifetime.
func (jw *Jaws) MakeID() string {
return "jaws." + strconv.FormatUint(atomic.AddUint64(&jw.nextId, 1), 32)
}
// NewRequest returns a new JaWS request.
// Call this as soon as you start processing a HTML request, and store the returned Request pointer so it can be used
// while constructing the HTML response in order to register the HTML entity id's you use in the response, and use
// it's Key attribute when sending the Javascript portion of the reply with GetBodyFooter.
//
// Don't use the http.Request's Context, as that will expire before the WebSocket call comes in.
func (jw *Jaws) NewRequest(ctx context.Context, remoteAddr string) (rq *Request) {
var err error
random := make([]byte, 8)
for rq == nil && err == nil {
jw.mu.Lock()
defer jw.mu.Unlock()
if _, err = io.ReadFull(jw.kg, random); err == nil {
if jawsKey := binary.LittleEndian.Uint64(random); jawsKey != 0 {
if _, ok := jw.reqs[jawsKey]; !ok {
rq = newRequest(ctx, jw, jawsKey, remoteAddr)
jw.reqs[jawsKey] = rq
}
}
}
}
maybePanic(err)
return
}
// UseRequest removes the JaWS request with the given key from the request
// map if it exists and the remoteAddr matches, and if so returns the Request.
//
// Call it when receiving the WebSocket connection on '/jaws/:key' to get the
// associated Request, and then call it's ServeHTTP method to process the
// WebSocket messages.
//
// Returns nil if the key was not found, in which case you should return a
// HTTP "404 Not Found" status.
func (jw *Jaws) UseRequest(jawsKey uint64, remoteAddr string) (rq *Request) {
jw.mu.Lock()
if waitingRq, ok := jw.reqs[jawsKey]; ok {
if err := waitingRq.start(remoteAddr); err == nil {
delete(jw.reqs, jawsKey)
rq = waitingRq
} else {
_ = jw.Log(err)
}
}
jw.mu.Unlock()
return
}
// Broadcast sends a message to all Requests.
func (jw *Jaws) Broadcast(msg *Message) {
select {
case <-jw.Done():
case jw.bcastCh <- msg:
}
}
// SetInner sends an HTML id and new inner HTML to all Requests.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) SetInner(id string, innerHtml string) {
jw.Broadcast(&Message{
Elem: id,
What: "inner",
Data: innerHtml,
})
}
// Remove removes the HTML element with the given ID on all Requests.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) Remove(id string) {
jw.Broadcast(&Message{
Elem: id,
What: "remove",
})
}
// Insert calls the Javascript 'insertBefore()' method on the given element on all Requests.
// The position parameter 'where' may be either a HTML ID, an child index or the text 'null'.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) Insert(parentId, where, html string) {
jw.Broadcast(&Message{
Elem: parentId,
What: "insert",
Data: where + "\n" + html,
})
}
// Append calls the Javascript 'appendChild()' method on the given element on all Requests.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) Append(parentId, html string) {
jw.Broadcast(&Message{
Elem: parentId,
What: "append",
Data: html,
})
}
// Replace calls the Javascript 'replaceChild()' method on the given element on all Requests.
// The position parameter 'where' may be either a HTML ID or an index.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) Replace(id, where, html string) {
jw.Broadcast(&Message{
Elem: id,
What: "replace",
Data: where + "\n" + html,
})
}
// SetAttr sends an HTML id and new attribute value to all Requests.
// If the value is an empty string, a value-less attribute will be added (such as "disabled")
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) SetAttr(id, attr, val string) {
jw.Broadcast(&Message{
Elem: id,
What: "sattr",
Data: attr + "\n" + val,
})
}
// RemoveAttr removes a given attribute from the HTML id for all Requests.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) RemoveAttr(id, attr string) {
jw.Broadcast(&Message{
Elem: id,
What: "rattr",
Data: attr,
})
}
// SetValue sends an HTML id and new input value to all Requests.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) SetValue(id, val string) {
jw.Broadcast(&Message{
Elem: id,
What: "value",
Data: val,
})
}
// Reload requests all Requests to reload their current page.
func (jw *Jaws) Reload() {
jw.Broadcast(&Message{
Elem: " reload",
})
}
// Redirect requests all Requests to navigate to the given URL.
func (jw *Jaws) Redirect(url string) {
jw.Broadcast(&Message{
Elem: " redirect",
What: url,
})
}
// Trigger invokes the event handler for the given ID with a 'trigger' event on all Requests.
func (jw *Jaws) Trigger(id, val string) {
jw.Broadcast(&Message{
Elem: id,
What: "trigger",
Data: val,
})
}
// Alert sends an alert to all Requests. The lvl argument should be one of Bootstraps alert levels:
// primary, secondary, success, danger, warning, info, light or dark.
func (jw *Jaws) Alert(lvl, msg string) {
jw.Broadcast(&Message{
Elem: " alert",
What: lvl,
Data: msg,
})
}
// Count returns the number of requests waiting for their WebSocket callbacks.
func (jw *Jaws) Pending() (n int) {
jw.mu.Lock()
n = len(jw.reqs)
jw.mu.Unlock()
return
}
// ServeWithTimeout begins processing requests with the given timeout.
// It is intended to run on it's own goroutine.
// It returns when the completion channel is closed.
func (jw *Jaws) ServeWithTimeout(requestTimeout time.Duration) {
const minInterval = time.Millisecond * 10
const maxInterval = time.Second
maintenanceInterval := requestTimeout / 2
if maintenanceInterval > maxInterval {
maintenanceInterval = maxInterval
}
if maintenanceInterval < minInterval {
maintenanceInterval = minInterval
}
t := time.NewTicker(maintenanceInterval)
defer t.Stop()
subs := map[chan *Message]struct{}{}
for {
select {
case <-jw.Done():
return
case <-t.C:
jw.maintenance(requestTimeout)
case msgCh := <-jw.subCh:
if msgCh != nil {
subs[msgCh] = struct{}{}
}
case msgCh := <-jw.unsubCh:
if _, ok := subs[msgCh]; ok {
delete(subs, msgCh)
close(msgCh)
}
case msg := <-jw.bcastCh:
if msg != nil {
for msgCh := range subs {
select {
case msgCh <- msg:
default:
// it's critical that we keep the broadcast
// distribution loop running, so any Request
// that fails to process it's messages quickly
// enough must be terminated. the alternative
// would be to drop some messages, but that
// could mean nonreproducible and seemingly
// random failures in processing logic.
close(msgCh)
delete(subs, msgCh)
_ = jw.Log(fmt.Errorf("jaws: broadcast channel full sending %v", msg))
}
}
}
}
}
}
// Serve calls ServeWithTimeout(time.Second * 10).
func (jw *Jaws) Serve() {
jw.ServeWithTimeout(time.Second * 10)
}
func (jw *Jaws) subscribe(size int) chan *Message {
msgCh := make(chan *Message, size)
select {
case <-jw.Done():
close(msgCh)
return nil
case jw.subCh <- msgCh:
}
return msgCh
}
func (jw *Jaws) unsubscribe(msgCh chan *Message) {
select {
case <-jw.Done():
case jw.unsubCh <- msgCh:
}
}
func (jw *Jaws) maintenance(requestTimeout time.Duration) {
var toCycle []*Request
deadline := time.Now().Add(-requestTimeout)
jw.mu.Lock()
for k, rq := range jw.reqs {
if rq.Created.Before(deadline) {
delete(jw.reqs, k)
toCycle = append(toCycle, rq)
}
}
jw.mu.Unlock()
for _, rq := range toCycle {
rq.recycle()
}
}
func parseIP(remoteAddr string) (ip net.IP) {
if remoteAddr != "" {
if host, _, err := net.SplitHostPort(remoteAddr); err == nil {
ip = net.ParseIP(host)
} else {
ip = net.ParseIP(remoteAddr)
}
}
return
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}