-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgrpcserver.go
101 lines (89 loc) · 3.01 KB
/
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
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
/*
* Copyright © 2017 Xiao Zhang <[email protected]>.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file.
*/
package turbo
import (
"net"
"net/http"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
type GrpcServer struct {
*Server
gClient *grpcClient
grpcServer *grpc.Server
}
func NewGrpcServer(initializer Initializable, configFilePath string) *GrpcServer {
if initializer == nil {
initializer = &defaultInitializer{}
}
s := &GrpcServer{
Server: &Server{
Config: NewConfig("grpc", configFilePath),
Components: new(Components),
reloadConfig: make(chan bool),
Initializer: initializer,
},
gClient: new(grpcClient),
}
s.initChans()
initLogger(s.Config)
return s
}
type grpcClientCreator func(conn *grpc.ClientConn) map[string]interface{}
// Start starts both HTTP server and GRPC service
func (s *GrpcServer) Start(clientCreator grpcClientCreator, sw switcher, registerServer func(s *grpc.Server)) {
log.Info("Starting Turbo...")
s.Initializer.InitService(s)
s.grpcServer = s.startGrpcServiceInternal(registerServer, false)
s.httpServer = s.startGrpcHTTPServerInternal(clientCreator, sw)
watchConfigReload(s)
}
// StartHTTPServer starts a HTTP server which sends requests via grpc
func (s *GrpcServer) StartHTTPServer(clientCreator grpcClientCreator, sw switcher) {
s.Initializer.InitService(s)
s.httpServer = s.startGrpcHTTPServerInternal(clientCreator, sw)
watchConfigReload(s)
}
// StartGrpcService starts a GRPC service
func (s *GrpcServer) StartGrpcService(registerServer func(s *grpc.Server)) {
s.Initializer.InitService(s)
s.grpcServer = s.startGrpcServiceInternal(registerServer, true)
}
func (s *GrpcServer) startGrpcHTTPServerInternal(clientCreator grpcClientCreator, sw switcher) *http.Server {
log.Info("Starting HTTP Server...")
switcherFunc = sw
//TODO register multi gClients
s.gClient.init(s.Config.GrpcServiceHost()+":"+s.Config.GrpcServicePort(), clientCreator)
return startHTTPServer(s)
}
func (s *GrpcServer) startGrpcServiceInternal(registerServer func(s *grpc.Server), alone bool) *grpc.Server {
log.Info("Starting GRPC Service...")
lis, err := net.Listen("tcp", ":"+s.Config.GrpcServicePort())
logPanicIf(err)
grpcServer := grpc.NewServer()
registerServer(grpcServer)
reflection.Register(grpcServer)
go func() {
if err := grpcServer.Serve(lis); err != nil {
log.Printf("GRPC Service failed to serve: %v", err)
}
}()
log.Info("GRPC Service started")
return grpcServer
}
// GrpcService returns a grpc client instance,
// example: client := turbo.GrpcService().(proto.YourServiceClient)
func (s *GrpcServer) Service(serviceName string) interface{} {
if s == nil || s.gClient == nil || s.gClient.grpcServiceMap == nil {
log.Panic("grpc connection not initiated!")
}
return s.gClient.grpcServiceMap[serviceName]
}
func (s *GrpcServer) ServerField() *Server { return s.Server }
func (s *GrpcServer) Stop() {
log.Info("Stop() invoked, Service is stopping...")
stop(s, s.httpServer, s.grpcServer, nil)
}