Add retry and non-blocking send for exit events

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby
2019-08-15 18:53:59 +00:00
parent 0d27d8f4f2
commit bee4c1a8a2
2 changed files with 41 additions and 10 deletions

View File

@@ -36,19 +36,25 @@ const bufferSize = 2048
// Reap should be called when the process receives an SIGCHLD. Reap will reap
// all exited processes and close their wait channels
func Reap() error {
now := time.Now()
var (
now = time.Now()
current []chan runc.Exit
)
exits, err := sys.Reap(false)
Default.Lock()
for c := range Default.subscribers {
for _, e := range exits {
c <- runc.Exit{
Timestamp: now,
Pid: e.Pid,
Status: e.Status,
}
}
current = append(current, c)
}
Default.Unlock()
for _, e := range exits {
go notify(runc.Exit{
Timestamp: now,
Pid: e.Pid,
Status: e.Status,
}, current)
}
return err
}
@@ -107,3 +113,28 @@ func (m *Monitor) Unsubscribe(c chan runc.Exit) {
close(c)
m.Unlock()
}
func notify(e runc.Exit, subscribers []chan runc.Exit) {
const timeout = 10 * time.Millisecond
timer := time.NewTimer(timeout)
timer.Stop()
for i := 0; i < 50; i++ {
var failed []chan runc.Exit
for _, s := range subscribers {
timer.Reset(timeout)
select {
case s <- e:
case <-timer.C:
failed = append(failed, s)
}
timer.Stop()
}
// all subscribers received the message
if len(failed) == 0 {
return
}
subscribers = failed
}
}