This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
349 lines (310 loc) · 7.53 KB
/
server.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
package synapse
import (
"crypto/tls"
"math"
"net"
"sync"
"unsafe"
"github.com/philhofer/fwd"
"github.com/tinylib/msgp/msgp"
)
const (
// used to compute pad sizes
sizeofPtr = unsafe.Sizeof((*byte)(nil))
// leadSize is the size of a "lead frame"
leadSize = 11
// maxMessageSize is the maximum size of a message
maxMessageSize = math.MaxUint16
)
// All writes (on either side) need to be atomic; conn.Write() is called exactly once and
// should contain the entirety of the request (client-side) or response (Server-side).
//
// In principle, the client can operate on any net.Conn, and the
// Server can operate on any net.Listener.
// Serve starts a Server on 'l' that serves
// the supplied handler. It blocks until the
// listener closes.
func Serve(l net.Listener, h Handler) error {
for {
c, err := l.Accept()
if err != nil {
return err
}
go ServeConn(c, h)
}
}
// ListenAndServeTLS acts identically to ListenAndServe, except that
// it expects connections over TLS1.2 (see crypto/tls). Additionally,
// files containing a certificate and matching private key for the
// Server must be provided. If the certificate is signed by a
// certificate authority, the certFile should be the concatenation of
// the server's certificate followed by the CA's certificate.
func ListenAndServeTLS(network, laddr string, certFile, keyFile string, h Handler) error {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return err
}
l, err := tls.Listen(network, laddr, &tls.Config{
Certificates: []tls.Certificate{cert},
})
if err != nil {
return err
}
return Serve(l, h)
}
// ListenAndServe opens up a network listener
// on the provided network and local address
// and begins serving with the provided handler.
// ListenAndServe blocks until there is a fatal
// listener error.
func ListenAndServe(network string, laddr string, h Handler) error {
l, err := net.Listen(network, laddr)
if err != nil {
return err
}
return Serve(l, h)
}
// ServeConn serves an individual network
// connection. It blocks until the connection
// is closed or it encounters a fatal error.
func ServeConn(c net.Conn, h Handler) {
ch := connHandler{
conn: c,
h: h,
remote: c.RemoteAddr(),
writing: make(chan *connWrapper, 32),
}
go ch.writeLoop()
ch.connLoop() // returns on connection close
ch.wg.Wait() // wait for handlers to return
close(ch.writing) // close output queue
}
func readFrame(lead [leadSize]byte) (seq uint64, ft fType, sz int) {
seq = (uint64(lead[0]) << 56) | (uint64(lead[1]) << 48) |
(uint64(lead[2]) << 40) | (uint64(lead[3]) << 32) |
(uint64(lead[4]) << 24) | (uint64(lead[5]) << 16) |
(uint64(lead[6]) << 8) | (uint64(lead[7]))
ft = fType(lead[8])
sz = int((uint16(lead[9]) << 8) | uint16(lead[10]))
return
}
func putFrame(bts []byte, seq uint64, ft fType, sz int) {
bts[0] = byte(seq >> 56)
bts[1] = byte(seq >> 48)
bts[2] = byte(seq >> 40)
bts[3] = byte(seq >> 32)
bts[4] = byte(seq >> 24)
bts[5] = byte(seq >> 16)
bts[6] = byte(seq >> 8)
bts[7] = byte(seq)
bts[8] = byte(ft)
usz := uint16(sz)
bts[9] = byte(usz >> 8)
bts[10] = byte(usz)
}
// connHandler handles network
// connections and multiplexes requests
// to connWrappers
type connHandler struct {
h Handler
conn net.Conn
remote net.Addr
wg sync.WaitGroup // outstanding handlers
writing chan *connWrapper // write queue
}
func (c *connHandler) writeLoop() error {
bwr := fwd.NewWriterSize(c.conn, 4096)
// this works the same way
// as (*client).writeLoop()
//
// the goroutine wakes up when
// there are pending messages
// to write. it writes the first
// one into the buffered writer,
// and continues to fill the
// buffered writer until
// no more messages are pending,
// and then flushes whatever is
// left. (*connHandler).writing
// is closed when there are no
// more pending handlers.
var err error
for {
cw, ok := <-c.writing
if !ok {
// this is the "normal"
// exit point for this
// goroutine.
return nil
}
if !c.do(bwr.Write(cw.res.out)) {
goto flush
}
wrappers.push(cw)
more:
select {
case another, ok := <-c.writing:
if ok {
f := c.do(bwr.Write(another.res.out))
wrappers.push(another)
if !f {
goto flush
}
goto more
} else {
bwr.Flush()
return nil
}
default:
if !c.do(0, bwr.Flush()) {
goto flush
}
}
}
flush:
for w := range c.writing {
wrappers.push(w)
}
return err
}
func (c *connHandler) do(i int, err error) bool {
if err != nil {
c.conn.Close()
return false
}
return true
}
// connLoop continuously polls the connection.
// requests are read synchronously; the responses
// are written in a spawned goroutine
func (c *connHandler) connLoop() {
brd := fwd.NewReaderSize(c.conn, 4096)
var (
lead [leadSize]byte
seq uint64
sz int
frame fType
err error
)
for {
// loop:
// - read seq, type, sz
// - call handler asynchronously
if !c.do(brd.ReadFull(lead[:])) {
return
}
seq, frame, sz = readFrame(lead)
// handle commands
if frame == fCMD {
var body []byte // command body; may be nil
var cmd command // command byte
// the 1-byte body case
// is pretty common for fCMD
if sz == 1 {
var bt byte
bt, err = brd.ReadByte()
cmd = command(bt)
} else {
body = make([]byte, sz)
_, err = brd.ReadFull(body)
cmd = command(body[0])
body = body[1:]
}
if err != nil {
c.conn.Close()
return
}
c.wg.Add(1)
go handleCmd(c, seq, cmd, body)
continue
}
// the only valid frame
// type left is fREQ
if frame != fREQ {
if !c.do(brd.Skip(sz)) {
return
}
continue
}
w := wrappers.pop()
if cap(w.in) >= sz {
w.in = w.in[0:sz]
} else {
w.in = make([]byte, sz)
}
if !c.do(brd.ReadFull(w.in)) {
return
}
// trigger handler
w.seq = seq
c.wg.Add(1)
go c.handleReq(w)
}
}
// connWrapper contains all the resources
// necessary to execute a Handler on a request
type connWrapper struct {
next *connWrapper // only used by slab
seq uint64 // sequence number
req request // (8w)
res response // (4w)
in []byte // incoming message
}
// handleconn sets up the Request and ResponseWriter
// interfaces and calls the handler.
func (c *connHandler) handleReq(cw *connWrapper) {
// clear/reset everything
cw.req.addr = c.remote
cw.res.wrote = false
var err error
// split request into 'name' and body
cw.req.mtd, cw.req.in, err = msgp.ReadUint32Bytes(cw.in)
if err != nil {
cw.res.Error(StatusBadRequest, "malformed request method")
} else {
c.h.ServeCall(&cw.req, &cw.res)
// if the handler didn't write a body,
// write 'nil'
if !cw.res.wrote {
cw.res.Send(nil)
}
}
blen := len(cw.res.out) - leadSize // length minus frame length
putFrame(cw.res.out, cw.seq, fRES, blen)
c.writing <- cw
c.wg.Done()
}
func handleCmd(c *connHandler, seq uint64, cmd command, body []byte) {
if cmd == cmdInvalid || cmd >= _maxcommand {
return
}
act := cmdDirectory[cmd]
resbyte := byte(cmd)
var res []byte
var err error
if act == nil {
resbyte = byte(cmdInvalid)
} else {
res, err = act.Server(c, body)
if err != nil {
resbyte = byte(cmdInvalid)
}
}
// for now, we'll use one of the
// connection wrappers
wr := wrappers.pop()
sz := len(res) + 1
need := sz + leadSize
if cap(wr.res.out) < need {
wr.res.out = make([]byte, need)
} else {
wr.res.out = wr.res.out[:need]
}
putFrame(wr.res.out[:], seq, fCMD, sz)
wr.res.out[leadSize] = resbyte
if res != nil {
copy(wr.res.out[leadSize+1:], res)
}
c.writing <- wr
c.wg.Done()
}