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

Fix server gracefull shutdown #42

Merged
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
1 change: 1 addition & 0 deletions docker-compose-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ services:
depends_on:
kafka:
condition: service_healthy
stop_grace_period: 30s
healthcheck:
test: [ "CMD", "nc", "-z", "localhost", "5000" ]
interval: 5s
Expand Down
42 changes: 28 additions & 14 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
"context"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -75,7 +78,7 @@ func NewApp(host string, port int, log logger.Logger, config *viper.Viper) (*App
return a, err
}

func (a *App) loadConfigurationDefaults() {
func (a *App) configure() error {
a.config.SetDefault("otlp.enabled", false)
a.config.SetDefault("kafka.producer.net.maxOpenRequests", 10)
a.config.SetDefault("kafka.producer.net.dialTimeout", "500ms")
Expand All @@ -98,10 +101,6 @@ func (a *App) loadConfigurationDefaults() {
a.config.SetDefault("server.Timeout", "500ms")
a.config.SetDefault("prometheus.enabled", "true") // always true on the API side
a.config.SetDefault("prometheus.port", ":9091")
}

func (a *App) configure() error {
a.loadConfigurationDefaults()

if a.config.GetBool("otlp.enabled") {
if err := a.configureOTel(); err != nil {
Expand Down Expand Up @@ -222,12 +221,12 @@ func (a *App) metricsReporterInterceptor(

// Run runs the app
func (a *App) Run() {
log := a.log

listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", a.host, a.port))
if err != nil {
log.Panic(err.Error())
a.log.Panic(err.Error())
}
log.Infof("events gateway listening on %s:%d", a.host, a.port)
a.log.Infof("events gateway listening on %s:%d", a.host, a.port)

metrics.StartServer(a.config)
var opts []grpc.ServerOption
Expand All @@ -250,11 +249,26 @@ func (a *App) Run() {
a.grpcServer = grpc.NewServer(opts...)

pb.RegisterGRPCForwarderServer(a.grpcServer, a.Server)
if err := a.grpcServer.Serve(listener); err != nil {
log.Panic(err.Error())
}
}
var stopChan = make(chan os.Signal, 2)
helder-junior marked this conversation as resolved.
Show resolved Hide resolved

signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM)
var errChan = make(chan error)

func (a *App) Stop() {
a.grpcServer.GracefulStop()
go func() {
if err := a.grpcServer.Serve(listener); err != nil {
errChan <- err
}
}()

defer func() {
a.log.Info("Calling GRPC Gracefull stop...")
a.grpcServer.GracefulStop()
a.log.Info("Finished GRPC Gracefull stop...")
}()
select {
case err := <-errChan:
a.log.Panicf("Server failed with error: %s", err.Error())
case sig := <-stopChan:
a.log.Infof("Got signal %s from OS. Stopping...", sig)
}
}
3 changes: 1 addition & 2 deletions server/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ ADD .. /app

RUN apk add make build-base

RUN go install github.com/wadey/[email protected] && \
go install github.com/onsi/ginkgo/v2/[email protected] && \
RUN go install github.com/onsi/ginkgo/v2/[email protected] && \
go mod tidy