-
Notifications
You must be signed in to change notification settings - Fork 672
/
server.go
106 lines (91 loc) · 3.54 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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package grpcutils
import (
"math"
"net"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
const (
// MinTime is the minimum amount of time a client should wait before sending
// a keepalive ping. grpc-go default 5 mins
defaultServerKeepAliveMinTime = 5 * time.Second
// After a duration of this time if the server doesn't see any activity it
// pings the client to see if the transport is still alive.
// If set below 1s, a minimum value of 1s will be used instead.
// grpc-go default 2h
defaultServerKeepAliveInterval = 2 * time.Hour
// After having pinged for keepalive check, the server waits for a duration
// of Timeout and if no activity is seen even after that the connection is
// closed. grpc-go default 20s
defaultServerKeepAliveTimeout = 20 * time.Second
)
var DefaultServerOptions = []grpc.ServerOption{
grpc.MaxRecvMsgSize(math.MaxInt),
grpc.MaxSendMsgSize(math.MaxInt),
grpc.MaxConcurrentStreams(math.MaxUint32),
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
MinTime: defaultServerKeepAliveMinTime,
PermitWithoutStream: defaultPermitWithoutStream,
}),
grpc.KeepaliveParams(keepalive.ServerParameters{
Time: defaultServerKeepAliveInterval,
Timeout: defaultServerKeepAliveTimeout,
}),
}
// NewServer will return a gRPC server with server options as defined by
// DefaultServerOptions. ServerOption can also optionally be passed.
func NewServer(opts ...ServerOption) *grpc.Server {
return grpc.NewServer(newServerOpts(opts)...)
}
type ServerOptions struct {
opts []grpc.ServerOption
}
// append(DefaultServerOptions, ...) will always allocate a new slice and will
// not overwrite any potential data that may have previously been appended to
// DefaultServerOptions https://go.dev/ref/spec#Composite_literals
func newServerOpts(opts []ServerOption) []grpc.ServerOption {
s := &ServerOptions{opts: DefaultServerOptions}
s.applyOpts(opts)
return s.opts
}
func (s *ServerOptions) applyOpts(opts []ServerOption) {
for _, opt := range opts {
opt(s)
}
}
// ServerOption are options which can be applied to a gRPC server in addition to
// the defaults set by DefaultServerOPtions.
type ServerOption func(*ServerOptions)
// WithUnaryInterceptor adds a single unary interceptor to the gRPC server
// options.
func WithUnaryInterceptor(unaryInterceptor grpc.UnaryServerInterceptor) ServerOption {
return func(s *ServerOptions) {
s.opts = append(s.opts, grpc.UnaryInterceptor(unaryInterceptor))
}
}
// WithStreamInterceptor adds a single stream interceptor to the gRPC server
// options.
func WithStreamInterceptor(streamInterceptor grpc.StreamServerInterceptor) ServerOption {
return func(s *ServerOptions) {
s.opts = append(s.opts, grpc.StreamInterceptor(streamInterceptor))
}
}
// NewListener returns a TCP listener listening against the next available port
// on the system bound to localhost.
func NewListener() (net.Listener, error) {
return net.Listen("tcp", "127.0.0.1:")
}
// Serve will start a gRPC server and block until it errors or is shutdown.
func Serve(listener net.Listener, grpcServer *grpc.Server) {
// TODO: While errors will be reported later, it could be useful to somehow
// log this if it is the primary error.
//
// There is nothing to with the error returned by serve here. Later requests
// will propegate their error if they occur.
_ = grpcServer.Serve(listener)
// Similarly, there is nothing to with an error when the listener is closed.
_ = listener.Close()
}