-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-sync.go
138 lines (128 loc) · 2.84 KB
/
web-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
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
package main
import (
"bytes"
"github.com/google/btree"
"golang.org/x/net/websocket"
"log"
"strconv"
)
type syncNotifyReq struct {
key string
data string
}
type kdVersion struct {
seq int64
key string
data string
}
func (this kdVersion) Less(than btree.Item) bool {
return this.seq < than.(kdVersion).seq
}
type syncRefreshReq struct {
last_seq int64
rsp_ch chan syncRefreshRsp
}
type syncRefreshRsp struct {
seq int64
update map[string]string
wchan chan struct{}
}
var syncChan = make(chan interface{}, 16)
func syncGo() {
seq_next := int64(1)
dseq := map[string]int64{}
dlog := btree.New(8)
cur_wchan := make(chan struct{}, 0)
for {
req := <-syncChan
switch req := req.(type) {
case syncNotifyReq:
item_seq := dseq[req.key]
if item_seq > 0 {
i := dlog.Get(kdVersion{seq: item_seq})
cur := i.(kdVersion)
if cur.data == req.data {
continue
}
dlog.Delete(kdVersion{seq: item_seq})
}
dseq[req.key] = seq_next
dlog.ReplaceOrInsert(kdVersion{seq: seq_next, key: req.key, data: req.data})
seq_next++
close(cur_wchan)
cur_wchan = make(chan struct{}, 0)
case syncRefreshReq:
update := map[string]string{}
dlog.AscendGreaterOrEqual(kdVersion{seq: req.last_seq}, func(i btree.Item) bool {
kd := i.(kdVersion)
update[kd.key] = kd.data
return true
})
req.rsp_ch <- syncRefreshRsp{
seq: seq_next,
update: update,
wchan: cur_wchan,
}
}
}
}
func wsSync(ws *websocket.Conn) {
log.Printf("sync: started")
// Start go routine that reads ok's.
ws_ok_ch := make(chan struct{}, 1)
go func() {
defer close(ws_ok_ch)
for {
var ok_rsp []byte
err := websocket.Message.Receive(ws, &ok_rsp)
if err != nil || !bytes.Equal(ok_rsp, []byte("ok")) {
log.Printf("sync: ended %v", err)
return
}
ws_ok_ch <- struct{}{}
}
}()
last_seq := int64(0)
for {
req := syncRefreshReq{last_seq: last_seq, rsp_ch: make(chan syncRefreshRsp, 1)}
syncChan <- req
rsp := <-req.rsp_ch
// Generate response to client.
var payload bytes.Buffer
payload.Write([]byte("{"))
first := true
for key, value := range rsp.update {
if first {
first = false
} else {
payload.Write([]byte(",\n"))
}
payload.Write([]byte(strconv.Quote(key)))
payload.Write([]byte(": "))
payload.Write([]byte(value))
}
payload.Write([]byte("}"))
err := websocket.Message.Send(ws, payload.String())
if err != nil {
return
}
// Wait for ok to throttle writes.
if _, ok := <-ws_ok_ch; !ok {
// Websocket was closed.
return
}
// Wait for update.
// TODO: Cancel wait if websocket is killed.
last_seq = rsp.seq
select {
case <-ws_ok_ch:
// Websocket was closed or got invalid ok.
return
case <-rsp.wchan:
// Updates are available.
}
}
}
func syncNotify(key string, data string) {
syncChan <- syncNotifyReq{key: key, data: data}
}