Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement reload signal debouncing #1705

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
18 changes: 15 additions & 3 deletions child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os/exec"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"

Expand Down Expand Up @@ -52,6 +53,7 @@ type Child struct {
timeout time.Duration

reloadSignal os.Signal
reloading uint32

killSignal os.Signal
killTimeout time.Duration
Expand Down Expand Up @@ -400,7 +402,7 @@ func (c *Child) signal(s os.Signal) error {
// kill takes negative pid to indicate that you want to use gpid
pid = -(pid)
}
// cross platform way to signal process/process group
// cross-platform way to signal process/process group
if p, err := os.FindProcess(pid); err != nil {
return err
} else {
Expand All @@ -409,6 +411,15 @@ func (c *Child) signal(s os.Signal) error {
}

func (c *Child) reload() error {
if !atomic.CompareAndSwapUint32(&c.reloading, 0, 1) {
c.logger.Printf("[INFO] (child) reload already in progress")
return nil
}
defer func() {
c.logger.Printf("[INFO] (child) reload completed")
atomic.StoreUint32(&c.reloading, 0)
}()

select {
case <-c.stopCh:
case <-c.randomSplay():
Expand Down Expand Up @@ -480,8 +491,9 @@ func (c *Child) randomSplay() <-chan time.Time {
return time.After(0)
}

ns := c.splay.Nanoseconds()
offset := rand.Int63n(ns)
max := c.splay.Nanoseconds()
min := c.splay.Nanoseconds() / 2
offset := rand.Int63n(max-min+1) + min
t := time.Duration(offset)

c.logger.Printf("[DEBUG] (child) waiting %.2fs for random splay", t.Seconds())
Expand Down
4 changes: 2 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ func (cli *CLI) Run(args []string) int {
return ExitCodeInterrupt
default:
// Propagate the signal to the child process
runner.Signal(s)
go runner.Signal(s)
}
case <-cli.stopCh:
return ExitCodeOK
}
}
}

// stop is used internally to shutdown a running CLI
// stop is used internally to shut down a running CLI
func (cli *CLI) stop() {
cli.Lock()
defer cli.Unlock()
Expand Down