Skip to content

Commit

Permalink
Fix server gracefull shutdown (#42)
Browse files Browse the repository at this point in the history
* Fix gracefull shutdown

* Add gracefull shutdown docker compose

* Fix signal handler

* Add log after gracefull shutdown finishes
  • Loading branch information
helder-junior authored Nov 21, 2024
1 parent aad63d8 commit b099f51
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
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)

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

0 comments on commit b099f51

Please sign in to comment.