-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
169 lines (146 loc) · 4.58 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
package xds
import (
"context"
"log"
"net"
"google.golang.org/grpc"
alsv3 "github.com/envoyproxy/go-control-plane/envoy/service/accesslog/v3"
clusterservicev3 "github.com/envoyproxy/go-control-plane/envoy/service/cluster/v3"
endpointservicev3 "github.com/envoyproxy/go-control-plane/envoy/service/endpoint/v3"
listenerservicev3 "github.com/envoyproxy/go-control-plane/envoy/service/listener/v3"
routeservicev3 "github.com/envoyproxy/go-control-plane/envoy/service/route/v3"
cachev3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
serverv3 "github.com/envoyproxy/go-control-plane/pkg/server/v3"
// to be
discoverygrpcv3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
runtimeservicev3 "github.com/envoyproxy/go-control-plane/envoy/service/runtime/v3"
secretservicev3 "github.com/envoyproxy/go-control-plane/envoy/service/secret/v3"
)
const (
defaultXdsListenAddr string = "[0.0.0.0]:8000"
defaultAlsListenAddr string = "[0.0.0.0]:8001"
defaultGrpcConcurrentStreams uint32 = 1000000
)
type serverOptFunc func(*serverOpt)
type serverOpt struct {
xdsListenAddr string
alsListenAddr string
maxConcurrentStreams uint32
}
func XdsListenAddr(addr string) serverOptFunc {
return func(opt *serverOpt) {
opt.xdsListenAddr = addr
}
}
func AlsListenAddr(addr string) serverOptFunc {
return func(opt *serverOpt) {
opt.alsListenAddr = addr
}
}
func MaxConcurrentStreams(n uint32) serverOptFunc {
return func(opt *serverOpt) {
opt.maxConcurrentStreams = n
}
}
func initOpt(opt *serverOpt) {
if len(opt.xdsListenAddr) < 1 {
opt.xdsListenAddr = defaultXdsListenAddr
}
if len(opt.alsListenAddr) < 1 {
opt.alsListenAddr = defaultAlsListenAddr
}
if opt.maxConcurrentStreams < 1 {
opt.maxConcurrentStreams = defaultGrpcConcurrentStreams
}
}
type server struct {
opt *serverOpt
xdsSvr *grpc.Server
alsSvr *grpc.Server
xdsHandler serverv3.Server
alsHandler *accesslogServiceHandler
}
func (s *server) registerXdsService() {
clusterservicev3.RegisterClusterDiscoveryServiceServer(s.xdsSvr, s.xdsHandler)
endpointservicev3.RegisterEndpointDiscoveryServiceServer(s.xdsSvr, s.xdsHandler)
listenerservicev3.RegisterListenerDiscoveryServiceServer(s.xdsSvr, s.xdsHandler)
routeservicev3.RegisterRouteDiscoveryServiceServer(s.xdsSvr, s.xdsHandler)
// to be
discoverygrpcv3.RegisterAggregatedDiscoveryServiceServer(s.xdsSvr, s.xdsHandler)
runtimeservicev3.RegisterRuntimeDiscoveryServiceServer(s.xdsSvr, s.xdsHandler)
secretservicev3.RegisterSecretDiscoveryServiceServer(s.xdsSvr, s.xdsHandler)
}
func (s *server) registerAlsService() {
// https://godoc.org/github.com/envoyproxy/go-control-plane/envoy/service/accesslog/v3#AccessLogServiceServer
alsv3.RegisterAccessLogServiceServer(s.alsSvr, s.alsHandler)
}
func (s *server) listenXds() (net.Listener, error) {
log.Printf("info: xds server listen: %s", s.opt.xdsListenAddr)
listener, err := net.Listen("tcp", s.opt.xdsListenAddr)
if err != nil {
log.Printf("error: addr '%s' listen error: %s", s.opt.xdsListenAddr, err.Error())
return nil, err
}
return listener, nil
}
func (s *server) listenAls() (net.Listener, error) {
log.Printf("info: als server listen: %s", s.opt.alsListenAddr)
listener, err := net.Listen("tcp", s.opt.alsListenAddr)
if err != nil {
log.Printf("error: addr '%s' listen error: %s", s.opt.alsListenAddr, err.Error())
return nil, err
}
return listener, nil
}
func (s *server) Start() error {
xdsListen, err := s.listenXds()
if err != nil {
return err
}
alsListen, err := s.listenAls()
if err != nil {
return err
}
s.registerXdsService()
s.registerAlsService()
errors := make(chan error, 0)
go func() {
if err := s.xdsSvr.Serve(xdsListen); err != nil {
log.Printf("error: xds serve error: %s", err.Error())
errors <- err
}
}()
go func() {
if err := s.alsSvr.Serve(alsListen); err != nil {
log.Printf("error: als serve error: %s", err.Error())
errors <- err
}
}()
return <-errors
}
func (s *server) Stop() error {
log.Printf("info: stop grpc server")
s.xdsSvr.Stop()
s.alsSvr.Stop()
return nil
}
func NewServer(ctx context.Context, cache cachev3.Cache, funcs ...serverOptFunc) *server {
opt := new(serverOpt)
for _, fn := range funcs {
fn(opt)
}
initOpt(opt)
xdsSvr := grpc.NewServer(
grpc.MaxConcurrentStreams(opt.maxConcurrentStreams),
)
alsSvr := grpc.NewServer(
grpc.MaxConcurrentStreams(opt.maxConcurrentStreams),
)
return &server{
opt: opt,
xdsSvr: xdsSvr,
alsSvr: alsSvr,
xdsHandler: serverv3.NewServer(ctx, cache, nil),
alsHandler: newAccesslogServiceHandler(newLoggerAccessLog()),
}
}