1
0
mirror of https://github.com/Luzifer/elb-instance-status.git synced 2024-09-16 13:48:34 +00:00

Fix: Cleanup context after checks have run

in order not to leak context resources

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-04-20 14:04:57 +02:00
parent 879faa0a36
commit 99391db514
Signed by: luzifer
SSH Key Fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E

View File

@ -8,6 +8,7 @@ import (
"net/url"
"os"
"os/exec"
"sync"
"time"
"github.com/sirupsen/logrus"
@ -143,9 +144,22 @@ func loadChecks() error {
}
func spawnChecks() {
ctx, _ := context.WithTimeout(context.Background(), cfg.CheckInterval-time.Second)
var wg sync.WaitGroup
ctx, cancel := context.WithTimeout(context.Background(), cfg.CheckInterval-time.Second)
wg.Add(len(checks))
go func() {
// Do not block the execution function but cleanup the context after
// all checks are done (or cancelled)
wg.Wait()
cancel()
}()
for id := range checks {
go executeAndRegisterCheck(ctx, id)
go func(ctx context.Context, id string) {
defer wg.Done()
executeAndRegisterCheck(ctx, id)
}(ctx, id)
}
}