changed server to use goroutine for each group, using cond to fire checks

This commit is contained in:
Christopher Ramey 2021-02-06 15:58:58 -09:00
parent aadd46ae1b
commit 534baa3ccb

View File

@ -2,15 +2,16 @@ package main
import ( import (
"fmt" "fmt"
"git.binarythought.com/cdramey/alrm/check"
"git.binarythought.com/cdramey/alrm/config" "git.binarythought.com/cdramey/alrm/config"
"time" "time"
"sync"
) )
func startServer(cfg *config.Config, debuglvl int) error { func startServer(cfg *config.Config, debuglvl int) error {
ch := make(chan check.Check, cfg.Threads) m := sync.Mutex{}
for i := 0; i < cfg.Threads; i++ { c := sync.NewCond(&m)
go worker(ch, debuglvl) for _, g := range cfg.Groups {
go worker(g, c, debuglvl)
} }
t := time.NewTicker(cfg.Interval) t := time.NewTicker(cfg.Interval)
@ -21,24 +22,25 @@ func startServer(cfg *config.Config, debuglvl int) error {
if debuglvl > 0 { if debuglvl > 0 {
fmt.Printf("Interval check at %s\n", r) fmt.Printf("Interval check at %s\n", r)
} }
for _, g := range cfg.Groups { c.Broadcast()
for _, h := range g.Hosts {
for _, c := range h.Checks {
ch <- c
}
}
}
} }
} }
return nil return nil
} }
func worker(ch chan check.Check, debuglvl int) { func worker(g *config.Group, c *sync.Cond, debuglvl int) {
for { for {
chk := <-ch c.L.Lock()
err := chk.Check(debuglvl) c.Wait()
c.L.Unlock()
for _, h := range g.Hosts {
for _, c := range h.Checks {
err := c.Check(debuglvl)
if err != nil { if err != nil {
fmt.Printf("Check error: %s\n", err) fmt.Printf("Check error: %s\n", err)
} }
} }
} }
}
}