cloudflare / tableflip

Graceful process restarts in Go
BSD 3-Clause "New" or "Revised" License
2.91k stars 148 forks source link

Expose parent presence #23

Closed nolith closed 5 years ago

nolith commented 5 years ago

There are situations in which is useful to detect the first invocation, i.e. you may want to cleanup dangling unix sockets, but not during an upgrade.

We already have WaitForParent and it can be exploited to get this information, but it looks so hacky...

func isUpgrade(u *tableflip.Upgrader) bool {
    ctx, cancel := context.WithCancel(context.Background())
    // we use a canceled context because WaitForParent returns immediately, without errors, only on the parent process
    // an already expired context ensure us an immediate failure also inside the children process
    cancel()

    return u.WaitForParent(ctx) != nil
}

This can be easily implemented in Upgrader with 1 line of code + tests.