Skip to content

Commit

Permalink
Shutdown timeout in config
Browse files Browse the repository at this point in the history
  • Loading branch information
massivemadness committed Oct 4, 2024
1 parent a9a55b5 commit 15997a9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions cmd/articles-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/go-playground/validator/v10"
"github.com/massivemadness/articles-server/internal/api"
Expand Down Expand Up @@ -77,12 +76,16 @@ func main() {
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan

shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), cfg.HttpServer.ShutdownTimeout)
defer shutdownRelease()

if err := httpServer.Shutdown(shutdownCtx); err != nil {
zapLogger.Error("HTTP shutdown error", zap.Error(err))
}

if err := httpPrivateServer.Shutdown(shutdownCtx); err != nil {
zapLogger.Error("HTTP shutdown error private", zap.Error(err))
}

zapLogger.Info("Server stopped")
}
1 change: 1 addition & 0 deletions config/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ http_server:
private_port: 8081
timeout: 4s
idle_timeout: 60s
shutdown_timeout: 10s

database:
host: "postgres"
Expand Down
1 change: 1 addition & 0 deletions config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ http_server:
private_port: 8081
timeout: 4s
idle_timeout: 60s
shutdown_timeout: 10s

database:
host: "postgres" # service name from docker compose
Expand Down
1 change: 1 addition & 0 deletions config/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ http_server:
private_port: 8081
timeout: 4s
idle_timeout: 60s
shutdown_timeout: 10s

database:
host: "postgres"
Expand Down
11 changes: 6 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ type Application struct {
}

type HTTPServer struct {
Address string `yaml:"address" env-default:"localhost"`
PublicPort int `yaml:"public_port" env-default:"8080"`
PrivatePort int `yaml:"private_port" env-default:"8081"`
Timeout time.Duration `yaml:"timeout" env-default:"4s"`
IdleTimeout time.Duration `yaml:"idle_timeout" env-default:"60s"`
Address string `yaml:"address" env-default:"localhost"`
PublicPort int `yaml:"public_port" env-default:"8080"`
PrivatePort int `yaml:"private_port" env-default:"8081"`
Timeout time.Duration `yaml:"timeout" env-default:"4s"`
IdleTimeout time.Duration `yaml:"idle_timeout" env-default:"60s"`
ShutdownTimeout time.Duration `yaml:"shutdown_timeout" env-default:"10s"`
}

type Database struct {
Expand Down

0 comments on commit 15997a9

Please sign in to comment.