summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-04-17 21:43:39 +0300
committerPaul Buetow <paul@buetow.org>2023-04-17 21:43:39 +0300
commit6b5ed019b50fdec4ab91922708390cb12c9a814e (patch)
tree98785e05cb25a6c08bf687f072202fcbbcbdb7ab /main.go
parent41a913d59e4f14b6cc61cb5eabaf0adda5c4b809 (diff)
can execute checks concurrently
Diffstat (limited to 'main.go')
-rw-r--r--main.go50
1 files changed, 41 insertions, 9 deletions
diff --git a/main.go b/main.go
index 5227a1b..6d661d7 100644
--- a/main.go
+++ b/main.go
@@ -4,6 +4,8 @@ import (
"context"
"flag"
"fmt"
+ "log"
+ "sync"
"time"
)
@@ -21,20 +23,50 @@ func main() {
notifyError(config, err)
}
- for name, check := range config.Checks {
- ctx, cancel := context.WithTimeout(context.Background(),
- time.Duration(config.CheckTimeoutS)*time.Second)
- defer cancel()
+ type entry struct {
+ name string
+ check check
+ }
- output, status := check.execute(ctx)
- stateChanged := state.update(name, status)
+ limiterCh := make(chan struct{}, config.CheckConcurrency)
+ checkCh := make(chan entry)
- if status != ok || stateChanged {
- subject := fmt.Sprintf("GOGIOS %s: %s", codeToString(status), name)
- notify(config, subject, output)
+ go func() {
+ for name, check := range config.Checks {
+ checkCh <- entry{name, check}
}
+ close(checkCh)
+ }()
+
+ var wg sync.WaitGroup
+ wg.Add(len(config.Checks))
+
+ for entry := range checkCh {
+ go func(name string, check check) {
+ limiterCh <- struct{}{}
+ defer func() {
+ <-limiterCh
+ wg.Done()
+ }()
+
+ ctx, cancel := context.WithTimeout(context.Background(),
+ time.Duration(config.CheckTimeoutS)*time.Second)
+ defer cancel()
+
+ output, status := check.execute(ctx)
+ stateChanged := state.update(name, status)
+
+ if status != ok || stateChanged {
+ subject := fmt.Sprintf("GOGIOS %s: %s", codeToString(status), name)
+ notify(config, subject, output)
+ }
+
+ }(entry.name, entry.check)
}
+ wg.Wait()
+ log.Println("All checks completed!")
+
if err := state.persist(); err != nil {
notifyError(config, err)
}