Skip to content

Commit

Permalink
feat: add http server write timeout (#250)
Browse files Browse the repository at this point in the history
Refs: closes #242 

## Summary

Add 1 minute as the maximum duration before timing out writes of the HTTP response.
  • Loading branch information
Goncalo-Marques authored Jul 6, 2024
1 parent 1b6d49d commit e9ba1ec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
16 changes: 13 additions & 3 deletions server/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/goncalo-marques/ecomap/server/internal/authn"
"github.com/goncalo-marques/ecomap/server/internal/authz"
Expand Down Expand Up @@ -99,10 +100,19 @@ func main() {

handlerHTTP := transporthttp.New(authzService, service)

var writeTimeoutHTTP time.Duration
if len(serviceConfig.ServerHTTP.WriteTimeout) != 0 {
writeTimeoutHTTP, err = time.ParseDuration(serviceConfig.ServerHTTP.WriteTimeout)
if err != nil {
logging.Logger.ErrorContext(ctx, "main: failed to parse server http write timeout configuration", logging.Error(err))
}
}

serverHTTP := &http.Server{
Addr: addressHTTP,
Handler: handlerHTTP,
ErrorLog: slog.NewLogLogger(logging.Logger.Handler(), slog.LevelError),
Addr: addressHTTP,
Handler: handlerHTTP,
WriteTimeout: writeTimeoutHTTP,
ErrorLog: slog.NewLogLogger(logging.Logger.Handler(), slog.LevelError),
}

go func() {
Expand Down
2 changes: 2 additions & 0 deletions server/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
serverHTTP:
writeTimeout: 60s
database:
url: postgres://postgres:postgres@database:5432/ecomap?sslmode=disable
migrations:
Expand Down
2 changes: 2 additions & 0 deletions server/deployments/prod/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
serverHTTP:
writeTimeout: 60s
database:
url: postgres://DB_USER:DB_PASSWORD@DB_HOST:DB_PORT/DB_NAME?sslmode=disable
migrations:
Expand Down
3 changes: 2 additions & 1 deletion server/internal/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ type Service struct {

// ServerHTTP defines the http server configuration structure.
type ServerHTTP struct {
Address string `yaml:"address"`
Address string `yaml:"address"`
WriteTimeout string `yaml:"writeTimeout"`
}

// DatabaseMigrations defines the database migrations configuration structure.
Expand Down

0 comments on commit e9ba1ec

Please sign in to comment.