-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver.go
337 lines (286 loc) · 5.87 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
package tarantool
import (
"bufio"
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"net"
"sync"
)
const saltSize = 32
type QueryHandler func(queryContext context.Context, query Query) *Result
type OnShutdownCallback func(err error)
type IprotoServer struct {
sync.Mutex
conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
uuid string
salt []byte // base64-encoded salt
ctx context.Context
cancel context.CancelFunc
handler QueryHandler
onShutdown OnShutdownCallback
output chan *BinaryPacket
closeOnce sync.Once
firstError error
perf PerfCount
schemaID uint64
wg sync.WaitGroup
getPingStatus func(*IprotoServer) uint
}
type IprotoServerOptions struct {
Perf PerfCount
GetPingStatus func(*IprotoServer) uint
}
func NewIprotoServer(uuid string, handler QueryHandler, onShutdown OnShutdownCallback) *IprotoServer {
return &IprotoServer{
conn: nil,
reader: nil,
writer: nil,
handler: handler,
onShutdown: onShutdown,
uuid: uuid,
schemaID: 1,
}
}
func (s *IprotoServer) WithOptions(opts *IprotoServerOptions) *IprotoServer {
if opts == nil {
opts = &IprotoServerOptions{}
}
s.perf = opts.Perf
s.getPingStatus = opts.GetPingStatus
if s.getPingStatus == nil {
s.getPingStatus = func(*IprotoServer) uint { return 0 }
}
return s
}
func (s *IprotoServer) Accept(conn net.Conn) {
var ccr io.Reader
var ccw io.Writer
if s.perf.NetRead != nil {
ccr = NewCountedReader(conn, s.perf.NetRead)
} else {
ccr = conn
}
if s.perf.NetWrite != nil {
ccw = NewCountedWriter(conn, s.perf.NetWrite)
} else {
ccw = conn
}
s.conn = conn
s.reader = bufio.NewReader(ccr)
s.writer = bufio.NewWriter(ccw)
s.ctx, s.cancel = context.WithCancel(context.Background())
s.output = make(chan *BinaryPacket, 1024)
err := s.greet()
if err != nil {
s.Shutdown()
return
}
go s.loop()
}
func (s *IprotoServer) CheckAuth(hash []byte, password string) bool {
scr, err := scramble(s.salt, password)
if err != nil {
return false
}
if len(scr) != len(hash) {
return false
}
for i, v := range hash {
if v != scr[i] {
return false
}
}
return true
}
func (s *IprotoServer) setError(err error) {
if err != nil && err != io.EOF {
s.Lock()
defer s.Unlock()
if s.firstError == nil {
s.firstError = err
}
}
}
func (s *IprotoServer) getError() error {
s.Lock()
defer s.Unlock()
return s.firstError
}
func (s *IprotoServer) Shutdown() error {
err := s.getError()
s.closeOnce.Do(func() {
s.cancel()
if s.onShutdown != nil {
s.onShutdown(err)
}
go func() {
s.wg.Wait()
s.conn.Close()
}()
})
return err
}
func (s *IprotoServer) greet() (err error) {
var line1, line2 string
var format, greeting string
var n int
salt := make([]byte, saltSize)
_, err = rand.Read(salt)
if err != nil {
return
}
s.salt = []byte(base64.StdEncoding.EncodeToString(salt))
line1 = fmt.Sprintf("%s %s", ServerIdent, s.uuid)
line2 = string(s.salt)
format = fmt.Sprintf("%%-%ds\n%%-%ds\n", GreetingSize/2-1, GreetingSize/2-1)
greeting = fmt.Sprintf(format, line1, line2)
// send greeting
n, err = fmt.Fprintf(s.writer, "%s", greeting)
if err != nil || n != GreetingSize {
return
}
return s.writer.Flush()
}
func (s *IprotoServer) loop() {
s.wg.Add(2)
go func() {
defer s.wg.Done()
s.read()
}()
go func() {
defer s.wg.Done()
s.write()
}()
}
func (s *IprotoServer) read() {
var err error
var pp *BinaryPacket
r := s.reader
var wg sync.WaitGroup
READER_LOOP:
for {
select {
case <-s.ctx.Done():
break READER_LOOP
default:
// read raw bytes
pp = packetPool.Get()
_, err = pp.ReadFrom(r)
if err != nil {
break READER_LOOP
}
if s.perf.NetPacketsIn != nil {
s.perf.NetPacketsIn.Add(1)
}
wg.Add(1)
go func(pp *BinaryPacket) {
packet := &pp.packet
defer wg.Done()
err := packet.UnmarshalBinary(pp.body)
if err != nil {
s.setError(fmt.Errorf("Error decoding packet type %d: %s", packet.Cmd, err))
s.Shutdown()
return
}
code := packet.Cmd
if code == PingCommand {
pr := packetPool.GetWithID(packet.requestID)
pr.packet.Cmd = s.getPingStatus(s)
pr.packet.SchemaID = packet.SchemaID
select {
case s.output <- pr:
break
case <-s.ctx.Done():
break
}
} else {
res := s.handler(s.ctx, packet.Request)
if res.ErrorCode != OKCommand && res.Error == nil {
res.Error = ErrUnknownError
}
// reuse the same binary packet object for result marshalling
if err = pp.packMsg(res, nil); err != nil {
s.setError(err)
s.Shutdown()
return
}
pp.packet.SchemaID = s.schemaID
select {
case s.output <- pp:
return
case <-s.ctx.Done():
break
}
}
pp.Release()
}(pp)
}
}
if err != nil {
s.setError(err)
}
wg.Wait()
s.Shutdown()
CLEANUP_LOOP:
for {
select {
case pp = <-s.output:
pp.Release()
default:
break CLEANUP_LOOP
}
}
}
func (s *IprotoServer) write() {
var err error
w := s.writer
wp := func(w io.Writer, packet *BinaryPacket) error {
if s.perf.NetPacketsOut != nil {
s.perf.NetPacketsOut.Add(1)
}
_, err = packet.WriteTo(w)
defer packet.Release()
return err
}
WRITER_LOOP:
for {
select {
case packet, ok := <-s.output:
if !ok {
break WRITER_LOOP
}
if err = wp(w, packet); err != nil {
break WRITER_LOOP
}
case <-s.ctx.Done():
w.Flush()
break WRITER_LOOP
default:
if err = w.Flush(); err != nil {
break WRITER_LOOP
}
// same without flush
select {
case packet, ok := <-s.output:
if !ok {
break WRITER_LOOP
}
if err = wp(w, packet); err != nil {
break WRITER_LOOP
}
case <-s.ctx.Done():
w.Flush()
break WRITER_LOOP
}
}
}
if err != nil {
s.setError(err)
}
s.Shutdown()
}