-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpcserver.go
61 lines (47 loc) · 1015 Bytes
/
grpcserver.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
package grpcserver
import (
"context"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
type (
Server struct {
cfg *Config
server *grpc.Server
}
)
func newGRpcServer(cfg *Config) *Server {
serverOpts := append(
cfg.serverOptions,
grpc.UnaryInterceptor(ChainUnaryServer(cfg.unaryServerInterceptor...)),
grpc.StreamInterceptor(ChainStreamServer(cfg.streamServerInterceptor...)),
)
server := grpc.NewServer(serverOpts...)
cfg.wrap(server)
if cfg.Reflection {
reflection.Register(server)
}
return &Server{
cfg: cfg,
server: server,
}
}
func (server *Server) Name() string {
return "gRPC-server"
}
func (server *Server) Serve(ctx context.Context) error {
lis, err := net.Listen(server.cfg.Network, server.cfg.Addr)
if err != nil {
return err
}
err = server.server.Serve(lis)
if err != grpc.ErrServerStopped {
return nil
}
return err
}
func (server *Server) Shutdown(ctx context.Context) error {
server.server.GracefulStop()
return nil
}