Skip to content

Commit

Permalink
Code review changes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
flotter committed Jun 28, 2023
1 parent d36ce7f commit c853715
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 7 additions & 10 deletions internals/overlord/checkstate/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// CheckManager starts and manages the health checks.
type CheckManager struct {
mutex sync.Mutex
group sync.WaitGroup
wg sync.WaitGroup
checks map[string]*checkData
failureHandlers []FailureFunc
}
Expand Down Expand Up @@ -61,25 +61,27 @@ func (m *CheckManager) PlanChanged(p *plan.Plan) {
}
// Wait for all context cancellations to propagate and allow
// each goroutine to cleanly exit.
m.group.Wait()
m.wg.Wait()

// Set the size of the next wait group
m.group.Add(len(p.Checks))
m.wg.Add(len(p.Checks))

// Then configure and start new checks.
checks := make(map[string]*checkData, len(p.Checks))
for name, config := range p.Checks {
ctx, cancel := context.WithCancel(context.Background())
check := &checkData{
config: config,
group: &m.group,
checker: newChecker(config),
ctx: ctx,
cancel: cancel,
action: m.callFailureHandlers,
}
checks[name] = check
go check.loop()
go func() {
defer m.wg.Done()
check.loop()
}()
}
m.checks = checks
}
Expand Down Expand Up @@ -163,7 +165,6 @@ const (
// checkData holds state for an active health check.
type checkData struct {
config *plan.Check
group *sync.WaitGroup
checker checker
ctx context.Context
cancel context.CancelFunc
Expand All @@ -180,10 +181,6 @@ type checker interface {
}

func (c *checkData) loop() {
// Schedule a notification on exit to indicate another
// checker in the group is complete.
defer c.group.Done()

logger.Debugf("Check %q starting with period %v", c.config.Name, c.config.Period.Value)

ticker := time.NewTicker(c.config.Period.Value)
Expand Down
4 changes: 2 additions & 2 deletions internals/overlord/checkstate/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ func (s *ManagerSuite) TestFailures(c *C) {

// waitCheck is a time based approach to wait for a checker run to complete.
// The timeout value does not impact the general time it takes for tests to
// complete, but determines a worse case waiting period before giving up.
// complete, but determines a worst case waiting period before giving up.
// The timeout value must take into account single core or very busy machines
// so it makes sense to pick a conservative number here as failing a test
// due to a busy test resource is more extensive than waiting a few more
// seconds.
func waitCheck(c *C, mgr *CheckManager, name string, f func(check *CheckInfo) bool) *CheckInfo {
// Worse case waiting time for checker run(s) to complete. This
// Worst case waiting time for checker run(s) to complete. This
// period should be much longer (10x is good) than the longest
// check timeout value.
timeout := time.Second * 10
Expand Down

0 comments on commit c853715

Please sign in to comment.