代码拉取完成,页面将自动刷新
同步操作将从 Gitee 极速下载/RPC-Turbo 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* Copyright © 2017 Xiao Zhang <zzxx513@gmail.com>.
* 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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。