/
route.go
180 lines (152 loc) · 3.04 KB
/
route.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
package goa
import (
"math"
"math/rand"
"net"
"sync"
"time"
"github.com/jimenezrick/goa/log"
)
const (
routeConnectRetries = 5
routeQueueLen = 128
minBatchSize = 1024
maxBatchLen = routeQueueLen / 8
)
type Route struct {
addr string
conn *conn
seq uint64
queue chan *Request
closed chan struct{}
exited chan struct{}
mtx sync.Mutex
pending map[uint64]chan<- []byte
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func newRoute(addr string) *Route {
return &Route{
addr: addr,
queue: make(chan *Request, routeQueueLen),
closed: make(chan struct{}),
exited: make(chan struct{}),
pending: make(map[uint64]chan<- []byte),
}
}
// TODO: connectAsync()
func (ro *Route) connect() error {
var netconn net.Conn
var err error
for i := 0; i < routeConnectRetries; i++ {
netconn, err = net.Dial("tcp", ro.addr)
if err == nil {
break
}
log.Debug(err)
waitBackoff(i)
}
if err != nil {
// TODO: Cancel all queue if we give up reconnecting
return err
}
log.Debug("Connected to", netconn.RemoteAddr())
ro.conn = newConn(netconn)
ro.closed = make(chan struct{})
ro.exited = make(chan struct{})
go ro.send()
go ro.recv()
return nil
}
func waitBackoff(i int) {
s := rand.Float64() * (math.Pow(2, float64(i)) - 1)
time.Sleep(time.Duration(s * float64(time.Second)))
}
func (ro *Route) send() {
defer ro.conn.close()
var reqsBatch = make([]*Request, 0, maxBatchLen)
var batchSize = 0
for {
reqsBatch = reqsBatch[:0:maxBatchLen]
batchSize = 0
batch:
for {
if len(ro.queue) == 0 && len(reqsBatch) > 0 {
break batch
}
select {
//
// TODO: Add a cancel channel to remove timeouted replies from the client
//
case req := <-ro.queue:
reqsBatch = append(reqsBatch, req)
batchSize += len(req.payld)
if len(reqsBatch) == maxBatchLen || batchSize >= minBatchSize {
break batch
}
case <-ro.closed:
close(ro.exited)
cancelBatch(reqsBatch)
return
}
}
for _, req := range reqsBatch {
req.seq = ro.seq
ro.seq++
}
ro.setPending(reqsBatch)
if err := ro.conn.sendBatch(reqsBatch); err != nil {
close(ro.exited)
cancelBatch(reqsBatch)
return
}
}
}
func cancelBatch(reqs []*Request) {
for _, r := range reqs {
r.cancel()
}
}
func (ro *Route) recv() {
defer ro.conn.close()
for {
payld, seq, err := ro.conn.recv()
if err != nil {
close(ro.closed)
<-ro.exited
ro.cancelPending()
ro.connect()
return
}
rsp := ro.getPending(seq)
if rsp == nil {
log.Infof("Unknown sequence number %d, dropping frame", seq)
}
rsp <- payld
}
}
func (ro *Route) setPending(reqs []*Request) {
ro.mtx.Lock()
for _, r := range reqs {
if r.rsp != nil {
ro.pending[r.seq] = r.rsp
}
}
ro.mtx.Unlock()
}
func (ro *Route) getPending(seq uint64) chan<- []byte {
ro.mtx.Lock()
rsp := ro.pending[seq]
delete(ro.pending, seq)
ro.mtx.Unlock()
return rsp
}
func (ro *Route) cancelPending() {
ro.mtx.Lock()
for seq, rsp := range ro.pending {
close(rsp)
delete(ro.pending, seq)
}
ro.mtx.Unlock()
}