/
sync.go
69 lines (63 loc) · 2.18 KB
/
sync.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
package velox
import (
"fmt"
"log"
"net/http"
"strconv"
"sync/atomic"
)
//NOTE(@jpillora): always assume v1, include v2 in checks when we get there...
const proto = "v1"
type syncer interface {
sync(gostruct interface{}) (*State, error)
}
//SyncHandler is a small wrapper around Sync which simply synchronises
//all incoming connections. Use Sync if you wish to implement user authentication
//or any other request-time checks.
func SyncHandler(gostruct interface{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if conn, err := Sync(gostruct, w, r); err != nil {
log.Printf("[velox] sync handler error: %s", err)
} else {
conn.Wait()
}
})
}
var connectionID int64
//Sync upgrades a given HTTP connection into a velox connection and synchronises
//the provided struct with the client. velox takes responsibility for writing the response
//in the event of failure. Default handlers close the TCP connection on return so when
//manually using this method, you'll most likely want to block using Conn.Wait().
func Sync(gostruct interface{}, w http.ResponseWriter, r *http.Request) (Conn, error) {
//access gostruct.State via interfaces:
gosyncable, ok := gostruct.(syncer)
if !ok {
return nil, fmt.Errorf("velox sync failed: struct does not embed velox.State")
}
//extract internal state from gostruct
state, err := gosyncable.sync(gostruct)
if err != nil {
return nil, fmt.Errorf("velox sync failed: %s", err)
}
version := int64(0)
//matching id, allow user to pick version
if id := r.URL.Query().Get("id"); id != "" && id == state.data.id {
if v, err := strconv.ParseInt(r.URL.Query().Get("v"), 10, 64); err == nil && v > 0 {
version = v
}
}
//set initial connection state
conn := newConn(atomic.AddInt64(&connectionID, 1), r.RemoteAddr, state, version)
//attempt connection over transport
//(negotiate websockets / start eventsource emitter)
//return when connected
if err := conn.connect(w, r); err != nil {
return nil, fmt.Errorf("velox connection failed: %s", err)
}
//hand over to state to keep in sync
state.subscribe(conn)
//do an initial push only to this client
conn.Push()
//pass connection to user
return conn, nil
}