From 919cdfd15447846ee4d3acffb7570463897a7722 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 Apr 2023 20:45:10 +0300 Subject: add global timeout --- cmd/gogios/main.go | 10 +++++++++- internal/execute.go | 4 ++-- internal/run.go | 6 ++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/gogios/main.go b/cmd/gogios/main.go index f9ec195..e125bd2 100644 --- a/cmd/gogios/main.go +++ b/cmd/gogios/main.go @@ -1,13 +1,21 @@ package main import ( + "context" "flag" + "time" "codeberg.org/snonux/gogios/internal" ) func main() { configFile := flag.String("cfg", "/etc/gogios.json", "The config file") + timeout := flag.Int("timeout", 5, "Global timeout in minutes") flag.Parse() - internal.Run(*configFile) + + ctx, cancel := context.WithTimeout(context.Background(), + time.Duration(*timeout)*time.Minute) + defer cancel() + + internal.Run(ctx, *configFile) } diff --git a/internal/execute.go b/internal/execute.go index d8f426f..1029ff7 100644 --- a/internal/execute.go +++ b/internal/execute.go @@ -7,7 +7,7 @@ import ( "time" ) -func execute(state state, config config) state { +func execute(globalCtx context.Context, state state, config config) state { limiterCh := make(chan struct{}, config.CheckConcurrency) inputCh := make(chan namedCheck) outputCh := make(chan checkResult) @@ -40,7 +40,7 @@ func execute(state state, config config) state { inputWg.Done() }() - ctx, cancel := context.WithTimeout(context.Background(), + ctx, cancel := context.WithTimeout(globalCtx, time.Duration(config.CheckTimeoutS)*time.Second) defer cancel() diff --git a/internal/run.go b/internal/run.go index 417b81b..ef9919d 100644 --- a/internal/run.go +++ b/internal/run.go @@ -1,6 +1,8 @@ package internal -func Run(configFile string) { +import "context" + +func Run(cts context.Context, configFile string) { config, err := newConfig(configFile) if err != nil { panic(err) @@ -11,7 +13,7 @@ func Run(configFile string) { notifyError(config, err) } - state = execute(state, config) + state = execute(ctx, state, config) if err := state.persist(); err != nil { notifyError(config, err) -- cgit v1.2.3