more cleanup in server

This commit is contained in:
Christopher Ramey 2021-02-14 14:22:30 -09:00
parent 4c16211e89
commit 8b438b6f3e
2 changed files with 43 additions and 16 deletions

View File

@ -10,12 +10,11 @@ type Server struct {
workers []*worker
cfg *config.Config
debuglvl int
httpsrv http.Server
}
func (srv *Server) Start() {
for _, w := range srv.workers {
go w.start(srv.debuglvl)
go w.start()
}
t := time.NewTicker(srv.cfg.Interval)
@ -26,12 +25,8 @@ func (srv *Server) Start() {
if srv.debuglvl > 0 {
fmt.Printf("interval check at %s\n", r)
}
for _, w := range srv.workers {
select {
case w.wake <- true:
default:
}
w.wake()
}
}
}
@ -40,8 +35,7 @@ func (srv *Server) Start() {
func NewServer(cfg *config.Config, debuglvl int) *Server {
srv := &Server{cfg: cfg, debuglvl: debuglvl}
for _, g := range cfg.Groups {
w := &worker{group: g, wake: make(chan bool)}
srv.workers = append(srv.workers, w)
srv.workers = append(srv.workers, makeworker(g, debuglvl))
}
return srv
}

View File

@ -6,23 +6,25 @@ import (
)
type worker struct {
wake chan bool
group *config.Group
wakec chan bool
shutdownc chan bool
group *config.Group
debuglvl int
}
func (w *worker) start(debuglvl int) {
func (w *worker) start() {
for {
if debuglvl > 2 {
if w.debuglvl > 2 {
fmt.Printf("%s worker waiting.. \n", w.group.Name)
}
<-w.wake
if debuglvl > 2 {
<-w.wakec
if w.debuglvl > 2 {
fmt.Printf("%s worker wake.. \n", w.group.Name)
}
for _, h := range w.group.Hosts {
for _, c := range h.Checks {
err := c.Check(debuglvl)
err := c.Check(w.debuglvl)
if err != nil {
fmt.Printf("check error: %s\n", err)
}
@ -30,3 +32,34 @@ func (w *worker) start(debuglvl int) {
}
}
}
// Wake this worker with a non-blocking push
// into the channel
func (w *worker) wake() {
select {
case w.wakec <- true:
default:
}
}
// Shutdown this worker with a non-blocking push
// into the channel
func (w *worker) shutdown() {
select {
case w.shutdownc <- true:
default:
}
}
func makeworker(g *config.Group, d int) *worker {
return &worker{
group: g,
debuglvl: d,
// This channel is unbuffered so that checks that take
// over the set interval don't backlog
wakec: make(chan bool),
// This channel is buffered because we want it to remember
// an order to shutdown
shutdownc: make(chan bool, 1),
}
}