generated from bep/golibtemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
497 lines (421 loc) · 12.3 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package execrpc
import (
"encoding/hex"
"fmt"
"hash"
"io"
"os"
"sync"
"time"
"github.com/bep/execrpc/codecs"
"golang.org/x/sync/errgroup"
)
const (
// MessageStatusOK is the status code for a successful and complete message exchange.
MessageStatusOK = iota
// MessageStatusContinue is the status code for a message that should continue the conversation.
MessageStatusContinue
// MessageStatusInitServer is the status code for a message used to initialize/configure the server.
MessageStatusInitServer
// MessageStatusErrDecodeFailed is the status code for a message that failed to decode.
MessageStatusErrDecodeFailed
// MessageStatusErrEncodeFailed is the status code for a message that failed to encode.
MessageStatusErrEncodeFailed
// MessageStatusErrInitServerFailed is the status code for a message that failed to initialize the server.
MessageStatusErrInitServerFailed
// MessageStatusSystemReservedMax is the maximum value for a system reserved status code.
MessageStatusSystemReservedMax = 99
)
// NewServerRaw creates a new Server using the given options.
func NewServerRaw(opts ServerRawOptions) (*ServerRaw, error) {
if opts.Call == nil {
return nil, fmt.Errorf("opts: Call function is required")
}
s := &ServerRaw{
call: opts.Call,
}
s.dispatcher = &messageDispatcher{
s: s,
}
return s, nil
}
// NewServer creates a new Server. using the given options.
func NewServer[C, Q, M, R any](opts ServerOptions[C, Q, M, R]) (*Server[C, Q, M, R], error) {
if opts.Handle == nil {
return nil, fmt.Errorf("opts: Handle function is required")
}
if opts.Codec == nil {
codecName := os.Getenv(envClientCodec)
var err error
opts.Codec, err = codecs.ForName(codecName)
if err != nil {
return nil, fmt.Errorf("failed to resolve codec from env variable %s with value %q (set by client); it can optionally be set in ServerOptions", envClientCodec, codecName)
}
}
var (
rawServer *ServerRaw
messagesRaw = make(chan Message, 10)
)
callRaw := func(message Message, d Dispatcher) error {
if message.Header.Status == MessageStatusInitServer {
if opts.Init == nil {
m := createErrorMessage(fmt.Errorf("opts: Init function is required"), message.Header, MessageStatusErrInitServerFailed)
d.SendMessage(m)
return nil
}
var cfg C
err := opts.Codec.Decode(message.Body, &cfg)
if err != nil {
m := createErrorMessage(err, message.Header, MessageStatusErrDecodeFailed)
d.SendMessage(m)
return nil
}
if err := opts.Init(cfg); err != nil {
m := createErrorMessage(err, message.Header, MessageStatusErrInitServerFailed)
d.SendMessage(m)
return nil
}
// OK.
var receipt Message
receipt.Header = message.Header
receipt.Header.Status = MessageStatusOK
d.SendMessage(receipt)
return nil
}
var q Q
err := opts.Codec.Decode(message.Body, &q)
if err != nil {
m := createErrorMessage(err, message.Header, MessageStatusErrDecodeFailed)
d.SendMessage(m)
return nil
}
call := &Call[Q, M, R]{
Request: q,
messagesRaw: messagesRaw,
messages: make(chan M, 10),
receiptToServer: make(chan R, 1),
receiptFromServer: make(chan R, 1),
}
go func() {
opts.Handle(call)
if !call.closed1 {
// The server returned without fetching the Receipt.
call.closeMessages()
}
if !call.closed2 {
// The server did not call Close,
// just send an empty receipt.
var r R
call.Close(false, r)
}
}()
var size uint32
var hasher hash.Hash
if opts.GetHasher != nil {
hasher = opts.GetHasher()
}
var shouldHash bool
if hasher != nil {
// Avoid hashing if the receipt does not implement Sum64Provider.
var r *R
_, shouldHash = any(r).(TagProvider)
}
var (
checksum string
messageBuff []Message
)
defer func() {
receipt := <-call.receiptFromServer
// Send any buffered message before the receipt.
if opts.DelayDelivery && !call.drop {
for _, m := range messageBuff {
d.SendMessage(m)
}
}
b, err := opts.Codec.Encode(receipt)
h := message.Header
h.Status = MessageStatusOK
d.SendMessage(createMessage(b, err, h, MessageStatusErrEncodeFailed))
}()
for m := range call.messages {
b, err := opts.Codec.Encode(m)
h := message.Header
h.Status = MessageStatusContinue
if h.ID == 0 {
panic("message ID must not be 0 for request/response messages")
}
m := createMessage(b, err, h, MessageStatusErrEncodeFailed)
if opts.DelayDelivery {
messageBuff = append(messageBuff, m)
} else {
d.SendMessage(m)
}
if shouldHash {
hasher.Write(m.Body)
}
size += uint32(len(m.Body))
}
if shouldHash {
checksum = hex.EncodeToString(hasher.Sum(nil))
}
var receipt R
setReceiptValuesIfNotSet(size, checksum, &receipt)
call.receiptToServer <- receipt
return nil
}
var err error
rawServer, err = NewServerRaw(
ServerRawOptions{
Call: callRaw,
},
)
if err != nil {
return nil, err
}
s := &Server[C, Q, M, R]{
messagesRaw: messagesRaw,
ServerRaw: rawServer,
}
// Handle standalone messages in its own goroutine.
go func() {
for message := range s.messagesRaw {
rawServer.dispatcher.SendMessage(message)
}
}()
return s, nil
}
func setReceiptValuesIfNotSet(size uint32, checksum string, r any) {
if m, ok := any(r).(LastModifiedProvider); ok && m.GetELastModified() == 0 {
m.SetELastModified(time.Now().Unix())
}
if size != 0 {
if m, ok := any(r).(SizeProvider); ok && m.GetESize() == 0 {
m.SetESize(size)
}
}
if checksum != "" {
if m, ok := any(r).(TagProvider); ok && m.GetETag() == "" {
m.SetETag(checksum)
}
}
}
func createMessage(b []byte, err error, h Header, failureStatus uint16) Message {
var m Message
if err != nil {
return createErrorMessage(err, h, failureStatus)
} else {
m = Message{
Header: h,
Body: b,
}
}
return m
}
func createErrorMessage(err error, h Header, failureStatus uint16) Message {
var additionalMsg string
if failureStatus == MessageStatusErrDecodeFailed || failureStatus == MessageStatusErrEncodeFailed {
additionalMsg = " Check that client and server uses the same codec."
}
m := Message{
Header: h,
Body: []byte(fmt.Sprintf("failed create message (error code %d): %s.%s", failureStatus, err, additionalMsg)),
}
m.Header.Status = failureStatus
return m
}
// ServerOptions is the options for a server.
type ServerOptions[C, Q, M, R any] struct {
// Init is the function that will be called when the server is started.
// It can be used to initialize the server with the given configuration.
// If an error is returned, the server will stop.
Init func(C) error
// Handle is the function that will be called when a request is received.
Handle func(*Call[Q, M, R])
// Codec is the codec that will be used to encode and decode requests, messages and receipts.
// The client will tell the server what codec is in use, so in most cases you should just leave this unset.
Codec codecs.Codec
// GetHasher returns the hash instance to be used for the response body
// If it's not set or it returns nil, no hash will be calculated.
GetHasher func() hash.Hash
// Delay delivery of messages to the client until Close is called.
// Close takes a drop parameter that will drop any buffered messages.
// This can be useful if you want to check the server generated ETag,
// maybe the client already has this data.
DelayDelivery bool
}
// Server is a stringly typed server for requests of type Q and responses of tye R.
type Server[C, Q, M, R any] struct {
messagesRaw chan Message
*ServerRaw
}
func (s *Server[C, Q, M, R]) Start() error {
err := s.ServerRaw.Start()
// Close the standalone message channel.
close(s.messagesRaw)
if err == io.EOF {
return nil
}
return err
}
// ServerRaw is a RPC server handling raw messages with a header and []byte body.
// See Server for a generic, typed version.
type ServerRaw struct {
call func(Message, Dispatcher) error
dispatcher *messageDispatcher
started bool
onStop func()
in io.Reader
out io.Writer
g *errgroup.Group
}
// Written by server to os.Stdout to signal it's ready for reading.
var serverStarted = []byte("_server_started")
// Start sets upt the server communication and starts the server loop.
func (s *ServerRaw) Start() error {
if s.started {
panic("server already started")
}
s.started = true
// os.Stdout is where the client will listen for a specific byte stream,
// and any writes to stdout outside of this protocol (e.g. fmt.Println("hello world!") will
// freeze the server.
//
// To prevent that, we preserve the original stdout for the server and redirect user output to stderr.
origStdout := os.Stdout
done := make(chan bool)
r, w, err := os.Pipe()
if err != nil {
return err
}
os.Stdout = w
go func() {
// Copy all output from the pipe to stderr.
_, _ = io.Copy(os.Stderr, r)
// Done when the pipe is closed.
done <- true
}()
s.in = os.Stdin
s.out = origStdout
s.onStop = func() {
// Close one side of the pipe.
_ = w.Close()
<-done
}
s.g = &errgroup.Group{}
// Signal to client that the server is ready.
fmt.Fprint(s.out, string(serverStarted)+"\n")
s.g.Go(func() error {
return s.inputOutput()
})
err = s.g.Wait()
if s.onStop != nil {
s.onStop()
}
return err
}
// inputOutput reads messages from the stdin and calls the server's call function.
// The response is written to stdout.
func (s *ServerRaw) inputOutput() error {
// We currently treat all errors in here as stop signals.
// This means that the server will stop taking requests and
// needs to be restarted.
// Server implementations should communicate client error situations
// via the messages.
var err error
for err == nil {
var header Header
if err = header.Read(s.in); err != nil {
break
}
body := make([]byte, header.Size)
_, err = io.ReadFull(s.in, body)
if err != nil {
break
}
err = s.call(
Message{
Header: header,
Body: body,
},
s.dispatcher,
)
if err != nil {
break
}
}
return err
}
// ServerRawOptions is the options for a raw portion of the server.
type ServerRawOptions struct {
// Call is the message exhcange between the client and server.
// Note that any error returned by this function will be treated as a fatal error and the server is stopped.
// Validation errors etc. should be returned in the response message.
// Message passed to the Dispatcher as part of the request/response must
// use the same ID as the request.
// ID 0 is reserved for standalone messages (e.g. log messages).
Call func(Message, Dispatcher) error
}
type messageDispatcher struct {
mu sync.Mutex
s *ServerRaw
}
// Call is the request/response exchange between the client and server.
// Note that the stream parameter S is optional, set it to any if not used.
type Call[Q, M, R any] struct {
Request Q
messagesRaw chan Message
messages chan M
receiptFromServer chan R
receiptToServer chan R
closed1 bool // No more messages.
closed2 bool // Receipt set.
drop bool // Drop buffered messages.
}
// SendRaw sends one or more messages back to the client
// that is not part of the request/response exchange.
// These messages must have ID 0.
func (c *Call[Q, M, R]) SendRaw(ms ...Message) {
for _, m := range ms {
if m.Header.ID != 0 {
panic("message ID must be 0 for standalone messages")
}
c.messagesRaw <- m
}
}
// Enqueue enqueues one or more messages to be sent back to the client.
func (c *Call[Q, M, R]) Enqueue(rr ...M) {
for _, r := range rr {
c.messages <- r
}
}
func (c *Call[Q, M, R]) Receipt() <-chan R {
c.closeMessages()
return c.receiptToServer
}
// Close closes the call and sends andy buffered messages and the receipt back to the client.
// If drop is true, the buffered messages are dropped.
// Note that drop is only relevant if the server is configured with DelayDelivery set to true.
func (c *Call[Q, M, R]) Close(drop bool, r R) {
c.drop = drop
c.closed2 = true
c.receiptFromServer <- r
}
func (c *Call[Q, M, R]) closeMessages() {
c.closed1 = true
close(c.messages)
}
// Dispatcher is the interface for dispatching messages to the client.
type Dispatcher interface {
// SendMessage sends one or more message back to the client.
SendMessage(...Message)
}
func (s *messageDispatcher) SendMessage(ms ...Message) {
s.mu.Lock()
defer s.mu.Unlock()
for _, m := range ms {
m.Header.Size = uint32(len(m.Body))
if err := m.Write(s.s.out); err != nil {
panic(err)
}
}
}