diff options
| author | Paul Buetow <paul@buetow.org> | 2023-04-19 20:45:10 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-04-19 20:45:10 +0300 |
| commit | 919cdfd15447846ee4d3acffb7570463897a7722 (patch) | |
| tree | dd822801a58873b63e342ad510cb13d7696d98f7 | |
| parent | e023a59312c3fa5e768dfea6b73c7647242a9f5a (diff) | |
add global timeout
| -rw-r--r-- | cmd/gogios/main.go | 10 | ||||
| -rw-r--r-- | internal/execute.go | 4 | ||||
| -rw-r--r-- | 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) |
