From bbb94b5d8b81a73a138121a96b567295af0bdbbc Mon Sep 17 00:00:00 2001 From: Michael Kirpichev Date: Sun, 4 Aug 2024 23:15:46 +0200 Subject: [PATCH] Parametrize server timeout (#3) * Add server timeout from cli * Change timeout behavior --- app/main.go | 7 ++++--- app/server/server.go | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/main.go b/app/main.go index 621e697..fee4170 100644 --- a/app/main.go +++ b/app/main.go @@ -24,9 +24,9 @@ var opts struct { Listen string `short:"l" long:"listen" env:"LISTEN" default:"localhost:8080" description:"listen on host:port"` SecretKey string `short:"k" long:"key" env:"KEY" required:"true" description:"secret key"` Batch bool `short:"b" long:"batch" description:"batch mode for multi-line scripts"` - Limit int `long:"limit" default:"10" description:"limit how many concurrent update can be running"` - TimeOut time.Duration `long:"timeout" default:"1m" description:"for how long update task can be running"` - UpdateDelay time.Duration `long:"update-delay" default:"1s" description:"delay between updates"` + Limit int `long:"limit" default:"10" description:"limit how many concurrent update can be running"` + TimeOut time.Duration `long:"timeout" default:"1m" description:"for how long batch update task can be running"` + UpdateDelay time.Duration `long:"update-delay" default:"1s" description:"delay between updates"` Dbg bool `long:"dbg" env:"DEBUG" description:"show debug info"` } @@ -72,6 +72,7 @@ func main() { Config: conf, Runner: runner, UpdateDelay: opts.UpdateDelay, + Timeout: opts.TimeOut, } if err := srv.Run(ctx); err != nil { diff --git a/app/server/server.go b/app/server/server.go index 1d6adb0..8045fe9 100644 --- a/app/server/server.go +++ b/app/server/server.go @@ -26,6 +26,7 @@ type Rest struct { Config Config Runner Runner UpdateDelay time.Duration + Timeout time.Duration } // Config declares command loader from config for given tasks @@ -46,7 +47,7 @@ func (s *Rest) Run(ctx context.Context) error { Addr: s.Listen, Handler: s.router(), ReadHeaderTimeout: time.Second, - WriteTimeout: 30 * time.Second, + WriteTimeout: s.Timeout + 10*time.Second, // Give enough time to finish the task and respond IdleTimeout: time.Second, ErrorLog: log.ToStdLogger(log.Default(), "WARN"), }