diff options
| author | Paul Buetow <paul@buetow.org> | 2023-04-18 23:00:48 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-04-18 23:00:48 +0300 |
| commit | 3658638e3bb3edb0f266c65fe0e42a1cd53a5f83 (patch) | |
| tree | 53735108cc161bec79ebafaecc12803d636e1f3a /execute.go | |
| parent | 7ea496151c0336414c9563613d7c1ce87e28f4ba (diff) | |
use nagiosCode and other refactorings
Diffstat (limited to 'execute.go')
| -rw-r--r-- | execute.go | 45 |
1 files changed, 20 insertions, 25 deletions
@@ -7,58 +7,53 @@ import ( "time" ) -type executionUnit struct { - name string - check check -} - -func execute(config config, state state) state { +func execute(state state, config config) state { limiterCh := make(chan struct{}, config.CheckConcurrency) - executionCh := make(chan executionUnit) - resultCh := make(chan checkResult) + inputCh := make(chan namedCheck) + outputCh := make(chan checkResult) go func() { for name, check := range config.Checks { - executionCh <- executionUnit{name, check} + inputCh <- namedCheck{check, name} } - close(executionCh) + close(inputCh) }() - var resultWg sync.WaitGroup - resultWg.Add(1) + var outputWg sync.WaitGroup + outputWg.Add(1) go func() { - for checkResult := range resultCh { + for checkResult := range outputCh { state.update(checkResult) } - resultWg.Done() + outputWg.Done() }() - var executionWg sync.WaitGroup - executionWg.Add(len(config.Checks)) + var inputWg sync.WaitGroup + inputWg.Add(len(config.Checks)) - for executionUnit := range executionCh { - go func(name string, check check) { + for check := range inputCh { + go func(check namedCheck) { limiterCh <- struct{}{} defer func() { <-limiterCh - executionWg.Done() + inputWg.Done() }() ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.CheckTimeoutS)*time.Second) defer cancel() - resultCh <- check.execute(ctx, name) - }(executionUnit.name, executionUnit.check) + outputCh <- check.execute(ctx) + }(check) } - executionWg.Wait() + inputWg.Wait() log.Println("All checks completed!") - close(resultCh) + close(outputCh) - resultWg.Wait() - log.Println("All results collected!") + outputWg.Wait() + log.Println("All outputs collected!") return state } |
