/
server.go
129 lines (106 loc) · 2.88 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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package drpcserver
import (
"context"
"net"
"time"
"github.com/zeebo/errs"
"storj.io/drpc"
"storj.io/drpc/drpccache"
"storj.io/drpc/drpcctx"
"storj.io/drpc/drpcmanager"
"storj.io/drpc/drpcstream"
)
// Options controls configuration settings for a server.
type Options struct {
// Manager controls the options we pass to the managers this server creates.
Manager drpcmanager.Options
// Log is called when errors happen that can not be returned up, like
// temporary network errors when accepting connections, or errors
// handling individual clients. It is not called if nil.
Log func(error)
}
// Server is an implementation of drpc.Server to serve drpc connections.
type Server struct {
opts Options
handler drpc.Handler
}
// New constructs a new Server.
func New(handler drpc.Handler) *Server {
return NewWithOptions(handler, Options{})
}
// NewWithOptions constructs a new Server using the provided options to tune
// how the drpc connections are handled.
func NewWithOptions(handler drpc.Handler, opts Options) *Server {
return &Server{
opts: opts,
handler: handler,
}
}
// ServeOne serves a single set of rpcs on the provided transport.
func (s *Server) ServeOne(ctx context.Context, tr drpc.Transport) (err error) {
man := drpcmanager.NewWithOptions(tr, s.opts.Manager)
defer func() { err = errs.Combine(err, man.Close()) }()
cache := drpccache.New()
defer cache.Clear()
ctx = drpccache.WithContext(ctx, cache)
for {
stream, rpc, err := man.NewServerStream(ctx)
if err != nil {
return errs.Wrap(err)
}
if err := s.handleRPC(stream, rpc); err != nil {
return errs.Wrap(err)
}
}
}
var temporarySleep = 500 * time.Millisecond
// Serve listens for connections on the listener and serves the drpc request
// on new connections.
func (s *Server) Serve(ctx context.Context, lis net.Listener) (err error) {
tracker := drpcctx.NewTracker(ctx)
defer tracker.Wait()
defer tracker.Cancel()
tracker.Run(func(ctx context.Context) {
<-ctx.Done()
_ = lis.Close()
})
for {
conn, err := lis.Accept()
if err != nil {
if ctx.Err() != nil {
return nil
}
if isTemporary(err) {
if s.opts.Log != nil {
s.opts.Log(err)
}
t := time.NewTimer(temporarySleep)
select {
case <-t.C:
case <-ctx.Done():
t.Stop()
return nil
}
continue
}
return errs.Wrap(err)
}
// TODO(jeff): connection limits?
tracker.Run(func(ctx context.Context) {
err := s.ServeOne(ctx, conn)
if err != nil && s.opts.Log != nil {
s.opts.Log(err)
}
})
}
}
// handleRPC handles the rpc that has been requested by the stream.
func (s *Server) handleRPC(stream *drpcstream.Stream, rpc string) (err error) {
err = s.handler.HandleRPC(stream, rpc)
if err != nil {
return errs.Wrap(stream.SendError(err))
}
return errs.Wrap(stream.CloseSend())
}