-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
317 lines (270 loc) · 9.15 KB
/
server.go
File metadata and controls
317 lines (270 loc) · 9.15 KB
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
//go:generate mockgen -destination=server_mock.go -package=server -source=server.go
package server
import (
"context"
"fmt"
"net"
"os"
"sync"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
"github.com/LumeraProtocol/supernode/pkg/errors"
"github.com/LumeraProtocol/supernode/pkg/log"
)
const (
_ = iota
KB int = 1 << (10 * iota) // 1024
MB // 1048576
GB // 1073741824
)
const (
defaultGracefulShutdownTimeout = 30 * time.Second
)
type grpcServer interface {
GetServiceInfo() map[string]grpc.ServiceInfo
GracefulStop()
RegisterService(*grpc.ServiceDesc, interface{})
Serve(net.Listener) error
Stop()
}
type ServerOptionBuilder interface {
buildKeepAliveParams(opts *ServerOptions) keepalive.ServerParameters
buildKeepAlivePolicy(opts *ServerOptions) keepalive.EnforcementPolicy
}
// Server represents a gRPC server with Lumera ALTS credentials
type Server struct {
name string
creds credentials.TransportCredentials
server *grpc.Server
services []ServiceDesc
listeners []net.Listener
mu sync.RWMutex
done chan struct{}
builder ServerOptionBuilder
}
// ServiceDesc wraps a gRPC service description and its implementation
type ServiceDesc struct {
Desc *grpc.ServiceDesc
Service interface{}
}
// ServerOptions contains options for creating a new server
type ServerOptions struct {
// Connection parameters
MaxRecvMsgSize int // Maximum message size the server can receive (in bytes)
MaxSendMsgSize int // Maximum message size the server can send (in bytes)
InitialWindowSize int32 // Initial window size for stream flow control
InitialConnWindowSize int32 // Initial window size for connection flow control
// Server parameters
MaxConcurrentStreams uint32 // Maximum number of concurrent streams per connection
GracefulShutdownTime time.Duration // Time to wait for graceful shutdown
// Keepalive parameters
MaxConnectionIdle time.Duration // Maximum time a connection can be idle
MaxConnectionAge time.Duration // Maximum time a connection can exist
MaxConnectionAgeGrace time.Duration // Additional time to wait before forcefully closing
Time time.Duration // Time after which server pings client if there's no activity
Timeout time.Duration // Time to wait for ping ack before considering the connection dead
MinTime time.Duration // Minimum time client should wait before sending pings
PermitWithoutStream bool // Allow pings even when there are no active streams
// Additional options
NumServerWorkers uint32 // Number of server workers (0 means default)
WriteBufferSize int // Size of write buffer
ReadBufferSize int // Size of read buffer
}
// DefaultServerOptions returns default server options
func DefaultServerOptions() *ServerOptions {
return &ServerOptions{
MaxRecvMsgSize: 100 * MB,
MaxSendMsgSize: 100 * MB,
InitialWindowSize: (int32)(1 * MB),
InitialConnWindowSize: (int32)(1 * MB),
MaxConcurrentStreams: 1000,
GracefulShutdownTime: defaultGracefulShutdownTimeout,
MaxConnectionIdle: 2 * time.Hour,
MaxConnectionAge: 2 * time.Hour,
MaxConnectionAgeGrace: 1 * time.Hour,
Time: 1 * time.Hour,
Timeout: 30 * time.Minute,
MinTime: 5 * time.Minute,
PermitWithoutStream: true,
WriteBufferSize: 32 * KB,
ReadBufferSize: 32 * KB,
}
}
// defaultServerOptionBuilder is the default implementation of ServerOptionBuilder
type defaultServerOptionBuilder struct{}
// buildKeepAlivePolicy creates the keepalive enforcement policy
func (b *defaultServerOptionBuilder) buildKeepAliveParams(opts *ServerOptions) keepalive.ServerParameters {
return keepalive.ServerParameters{
MaxConnectionIdle: opts.MaxConnectionIdle,
MaxConnectionAge: opts.MaxConnectionAge,
MaxConnectionAgeGrace: opts.MaxConnectionAgeGrace,
Time: opts.Time,
Timeout: opts.Timeout,
}
}
// buildKeepAlivePolicy creates the keepalive enforcement policy
func (b *defaultServerOptionBuilder) buildKeepAlivePolicy(opts *ServerOptions) keepalive.EnforcementPolicy {
return keepalive.EnforcementPolicy{
MinTime: opts.MinTime,
PermitWithoutStream: opts.PermitWithoutStream,
}
}
// NewServer creates a new gRPC server with the given credentials
func NewServer(name string, creds credentials.TransportCredentials) *Server {
return &Server{
name: name,
creds: creds,
services: make([]ServiceDesc, 0),
listeners: make([]net.Listener, 0),
done: make(chan struct{}),
builder: &defaultServerOptionBuilder{},
}
}
// NewServerWithBuilder creates a new gRPC server with the given credentials and option builder
func NewServerWithBuilder(name string, creds credentials.TransportCredentials, builder ServerOptionBuilder) *Server {
return &Server{
name: name,
creds: creds,
services: make([]ServiceDesc, 0),
listeners: make([]net.Listener, 0),
done: make(chan struct{}),
builder: builder,
}
}
// buildServerOptions creates all server options including credentials
func (s *Server) buildServerOptions(opts *ServerOptions) []grpc.ServerOption {
serverOpts := []grpc.ServerOption{
grpc.MaxRecvMsgSize(opts.MaxRecvMsgSize),
grpc.MaxSendMsgSize(opts.MaxSendMsgSize),
grpc.InitialWindowSize(opts.InitialWindowSize),
grpc.InitialConnWindowSize(opts.InitialConnWindowSize),
grpc.MaxConcurrentStreams(opts.MaxConcurrentStreams),
grpc.KeepaliveParams(s.builder.buildKeepAliveParams(opts)),
grpc.KeepaliveEnforcementPolicy(s.builder.buildKeepAlivePolicy(opts)),
grpc.WriteBufferSize(opts.WriteBufferSize),
grpc.ReadBufferSize(opts.ReadBufferSize),
}
if opts.NumServerWorkers > 0 {
serverOpts = append(serverOpts, grpc.NumStreamWorkers(opts.NumServerWorkers))
}
// Add credentials based on environment
if os.Getenv("INTEGRATION_TEST_ENV") == "true" {
serverOpts = append(serverOpts, grpc.Creds(insecure.NewCredentials()))
} else {
serverOpts = append(serverOpts, grpc.Creds(s.creds))
}
return serverOpts
}
// RegisterService registers a gRPC service with the server
func (s *Server) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
s.services = append(s.services, ServiceDesc{
Desc: desc,
Service: impl,
})
}
// createListener creates a TCP listener for the given address
func (s *Server) createListener(ctx context.Context, address string) (net.Listener, error) {
lis, err := net.Listen("tcp", address)
if err != nil {
return nil, errors.Errorf("failed to create listener: %w", err).WithField("address", address)
}
log.WithContext(ctx).Infof("gRPC server listening on %q", address)
return lis, nil
}
// Serve starts the gRPC server on the given address
func (s *Server) Serve(ctx context.Context, address string, opts *ServerOptions) error {
if ctx == nil {
return fmt.Errorf("context cannot be nil")
}
if opts == nil {
opts = DefaultServerOptions()
}
grpclog.SetLoggerV2(log.NewLoggerWithErrorLevel())
ctx = log.ContextWithPrefix(ctx, s.name)
// Create server with options
serverOpts := s.buildServerOptions(opts)
s.server = grpc.NewServer(serverOpts...)
// Register services
s.mu.RLock()
for _, service := range s.services {
s.server.RegisterService(service.Desc, service.Service)
}
s.mu.RUnlock()
// Enable reflection
reflection.Register(s.server)
// Create listener
lis, err := s.createListener(ctx, address)
if err != nil {
return err
}
s.mu.Lock()
s.listeners = append(s.listeners, lis)
s.mu.Unlock()
// Start serving in a goroutine
serveErr := make(chan error, 1)
go func() {
if err := s.server.Serve(lis); err != nil {
serveErr <- errors.Errorf("serve: %w", err).WithField("address", address)
}
close(serveErr)
}()
// Wait for context cancellation or error
select {
case <-ctx.Done():
log.WithContext(ctx).Infof("Shutting down gRPC server at %q", address)
return s.Stop(opts.GracefulShutdownTime)
case err := <-serveErr:
return err
}
}
// Stop gracefully stops the server with a timeout
func (s *Server) Stop(timeout time.Duration) error {
if s.server == nil {
return nil
}
// Create shutdown context with timeout
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// Create channel to signal completion of graceful stop
stopped := make(chan struct{})
go func() {
s.server.GracefulStop()
close(stopped)
}()
// Wait for graceful stop or timeout
select {
case <-ctx.Done():
s.server.Stop()
return fmt.Errorf("server shutdown timed out")
case <-stopped:
return nil
}
}
// Close immediately stops the server and closes all listeners
func (s *Server) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.server != nil {
s.server.Stop()
s.server = nil
}
// Close all listeners
var errs []error
for _, lis := range s.listeners {
if err := lis.Close(); err != nil {
errs = append(errs, err)
}
}
s.listeners = nil
if len(errs) > 0 {
return fmt.Errorf("errors closing listeners: %v", errs)
}
return nil
}