Skip to content

Commit

Permalink
rename method, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdoxsey committed Apr 26, 2024
1 parent 5a8a71c commit a8c7134
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions internal/enabler/enabler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ func New(name string, handler Handler, enabled bool) Enabler {
// Run calls RunEnabled if enabled, otherwise it waits until enabled.
func (d *enabler) Run(ctx context.Context) error {
for {
err := d.runOnce(ctx)
if errors.Is(err, errCauseEnabler) {
continue
err := d.runOrWaitForEnabled(ctx)
// if we received any error but our own, exit with that error
if !errors.Is(err, errCauseEnabler) {
return err
}
return err
}
}

func (d *enabler) runOnce(ctx context.Context) error {
func (d *enabler) runOrWaitForEnabled(ctx context.Context) error {
d.mu.Lock()
enabled := d.enabled
ctx, d.cancel = context.WithCancelCause(ctx)
d.mu.Unlock()

// we're enabled so call RunEnabled. If Disabled is called it will cancel ctx.
if enabled {
log.Ctx(ctx).Info().Msgf("enabled %s", d.name)
err := d.handler.RunEnabled(ctx)
Expand All @@ -83,7 +84,7 @@ func (d *enabler) runOnce(ctx context.Context) error {
return err
}

// wait for a transition from disabled -> enabled
// wait until Enabled is called
<-ctx.Done()
return context.Cause(ctx)
}
Expand Down

0 comments on commit a8c7134

Please sign in to comment.