Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize manager startup process #562

Merged
merged 2 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 45 additions & 38 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type Server struct {
// Server configuration
config *config.Config

// GRPC service
service *service.GRPC
// GRPC server
grpcServer *grpc.Server

// REST server
restServer *http.Server
Expand Down Expand Up @@ -74,74 +74,81 @@ func New(cfg *config.Config) (*Server, error) {
return nil, err
}

// Initialize REST service
restService := service.NewREST(db, cache, job, enforcer)

// Initialize GRPC service
grpcService := service.NewGRPC(db, cache, searcher)

// Initialize Proxy service
// Initialize Proxy server
proxyServer := proxy.New(cfg.Database.Redis)

// Initialize router
// Initialize REST server
restService := service.NewREST(db, cache, job, enforcer)
router, err := router.Init(cfg.Verbose, cfg.Server.PublicPath, restService, enforcer)
if err != nil {
return nil, err
}
restServer := &http.Server{
Addr: cfg.Server.REST.Addr,
Handler: router,
}

// Initialize GRPC server
grpcService := service.NewGRPC(db, cache, searcher)
grpcServer := grpc.NewServer()
manager.RegisterManagerServer(grpcServer, grpcService)

return &Server{
config: cfg,
service: grpcService,
restServer: &http.Server{
Addr: cfg.Server.REST.Addr,
Handler: router,
},
config: cfg,
grpcServer: grpcServer,
restServer: restServer,
proxyServer: proxyServer,
}, nil
}

func (s *Server) Serve() error {
// GRPC listener
lis, _, err := rpc.ListenWithPortRange(s.config.Server.GRPC.Listen, s.config.Server.GRPC.PortRange.Start, s.config.Server.GRPC.PortRange.End)
if err != nil {
logger.Fatalf("failed to net listen: %+v", err)
}
defer lis.Close()

// Serve Proxy
// Started Proxy server
go func() {
logger.Info("serve proxy")
logger.Info("started proxy")
if err := s.proxyServer.Serve(); err != nil {
logger.Fatalf("failed to start manager proxy server: %+v", err)
logger.Fatalf("proxy server closed unexpect: %+v", err)
}
}()

// Serve REST
// Started REST server
go func() {
logger.Infof("serve rest at %s", s.restServer.Addr)
logger.Infof("started rest server at %s", s.restServer.Addr)
if err := s.restServer.ListenAndServe(); err != nil {
logger.Fatalf("failed to start manager rest server: %+v", err)
if err == http.ErrServerClosed {
logger.Info("rest server closed under request")
return
}
logger.Fatalf("rest server closed unexpect: %+v", err)
}
}()

// Serve GRPC
grpcServer := grpc.NewServer()
manager.RegisterManagerServer(grpcServer, s.service)
logger.Infof("serve grpc at %s://%s", lis.Addr().Network(), lis.Addr().String())
if err := grpcServer.Serve(lis); err != nil {
logger.Errorf("failed to start manager grpc server: %+v", err)
// Generate GRPC listener
lis, _, err := rpc.ListenWithPortRange(s.config.Server.GRPC.Listen, s.config.Server.GRPC.PortRange.Start, s.config.Server.GRPC.PortRange.End)
if err != nil {
logger.Fatalf("net listener failed to start: %+v", err)
}
defer lis.Close()

// Started GRPC server
logger.Infof("started grpc server at %s://%s", lis.Addr().Network(), lis.Addr().String())
if err := s.grpcServer.Serve(lis); err != nil {
logger.Errorf("stoped grpc server: %+v", err)
return err
}

return nil
}

func (s *Server) Stop() {
// Stop Proxy
// Stop Proxy server
s.proxyServer.Stop()

// Stop REST
// Stop REST server
if err := s.restServer.Shutdown(context.TODO()); err != nil {
logger.Errorf("failed to stop manager rest server: %+v", err)
logger.Errorf("rest server failed to stop: %+v", err)
}

// Stop GRPC server
s.grpcServer.GracefulStop()
logger.Info("grpc server closed under request")
}
1 change: 1 addition & 0 deletions manager/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (p *proxy) Serve() error {
if err != nil {
return err
}
defer listener.Close()

for {
select {
Expand Down