mirror of
				https://github.com/3ybactuk/marketplace-go-service-project.git
				synced 2025-10-31 06:23:44 +03:00 
			
		
		
		
	[hw-5] concurrency, graceful shutdown, concurrent tests
This commit is contained in:
		| @@ -30,6 +30,10 @@ import ( | ||||
| type App struct { | ||||
| 	config     *config.Config | ||||
| 	controller *server.Server | ||||
|  | ||||
| 	grpcServer *grpc.Server | ||||
| 	httpServer *http.Server | ||||
| 	gwConn     *grpc.ClientConn | ||||
| } | ||||
|  | ||||
| func NewApp(configPath string) (*App, error) { | ||||
| @@ -72,26 +76,65 @@ func NewApp(configPath string) (*App, error) { | ||||
| 	return app, nil | ||||
| } | ||||
|  | ||||
| func (app *App) ListenAndServe() error { | ||||
| func (app *App) Shutdown(ctx context.Context) (err error) { | ||||
| 	if app.httpServer != nil { | ||||
| 		err = app.httpServer.Shutdown(ctx) | ||||
|  | ||||
| 		if err != nil { | ||||
| 			log.Error().Err(err).Msgf("failed http gateway server shutdown") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	done := make(chan struct{}) | ||||
| 	if app.grpcServer != nil { | ||||
| 		go func() { | ||||
| 			app.grpcServer.GracefulStop() | ||||
|  | ||||
| 			close(done) | ||||
| 		}() | ||||
| 	} | ||||
|  | ||||
| 	select { | ||||
| 	case <-done: | ||||
| 	case <-ctx.Done(): | ||||
| 		if app.grpcServer != nil { | ||||
| 			app.grpcServer.Stop() | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if app.gwConn != nil { | ||||
| 		err2 := app.gwConn.Close() | ||||
|  | ||||
| 		if err2 != nil { | ||||
| 			err = err2 | ||||
|  | ||||
| 			log.Error().Err(err).Msgf("failed gateway connection close") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (app *App) ListenAndServe(ctx context.Context) error { | ||||
| 	grpcAddr := fmt.Sprintf("%s:%s", app.config.Service.Host, app.config.Service.GRPCPort) | ||||
| 	l, err := net.Listen("tcp", grpcAddr) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	grpcServer := grpc.NewServer( | ||||
| 	app.grpcServer = grpc.NewServer( | ||||
| 		grpc.ChainUnaryInterceptor( | ||||
| 			mw.Logging, | ||||
| 			mw.Validate, | ||||
| 		), | ||||
| 	) | ||||
| 	reflection.Register(grpcServer) | ||||
| 	reflection.Register(app.grpcServer) | ||||
|  | ||||
| 	pb.RegisterLOMSServer(grpcServer, app.controller) | ||||
| 	pb.RegisterLOMSServer(app.grpcServer, app.controller) | ||||
|  | ||||
| 	go func() { | ||||
| 		if err = grpcServer.Serve(l); err != nil { | ||||
| 			log.Fatal().Msgf("failed to serve: %v", err) | ||||
| 		if err = app.grpcServer.Serve(l); err != nil { | ||||
| 			log.Fatal().Err(err).Msg("failed to serve") | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| @@ -106,21 +149,22 @@ func (app *App) ListenAndServe() error { | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("grpc.NewClient: %w", err) | ||||
| 	} | ||||
| 	app.gwConn = conn | ||||
|  | ||||
| 	gwmux := runtime.NewServeMux() | ||||
| 	if err = pb.RegisterLOMSHandler(context.Background(), gwmux, conn); err != nil { | ||||
| 	if err = pb.RegisterLOMSHandler(ctx, gwmux, conn); err != nil { | ||||
| 		return fmt.Errorf("pb.RegisterLOMSHandler: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	gwServer := &http.Server{ | ||||
| 	app.httpServer = &http.Server{ | ||||
| 		Addr:        fmt.Sprintf("%s:%s", app.config.Service.Host, app.config.Service.HTTPPort), | ||||
| 		Handler:     gwmux, | ||||
| 		ReadTimeout: 10 * time.Second, | ||||
| 	} | ||||
|  | ||||
| 	log.Info().Msgf("Serving http loms at http://%s", gwServer.Addr) | ||||
| 	log.Info().Msgf("Serving http loms at http://%s", app.httpServer.Addr) | ||||
|  | ||||
| 	return gwServer.ListenAndServe() | ||||
| 	return app.httpServer.ListenAndServe() | ||||
| } | ||||
|  | ||||
| func getPostgresPools(c *config.Config) (masterPool, replicaPool *pgxpool.Pool, err error) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Никита Шубин
					Никита Шубин