oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux || darwin
2
3package containment
4
5import (
6 "time"
7
8 "golang.org/x/sys/unix"
9)
10
11// zeroTimeoutTries caps the calls a zero-timeout poll makes when every one
12// is interrupted. Such a poll takes microseconds, so a few tries ride out an
13// odd signal, while a steady stream of signals cannot keep the caller (for
14// example recovery) spinning; it gets the EINTR and fails closed.
15const zeroTimeoutTries = 8
16
17// pollFD is unix.Poll that does not report a signal as a failure.
18func pollFD(fds []unix.PollFd, timeout time.Duration) (int, error) {
19 return pollRetry(fds, timeout, unix.Poll)
20}
21
22// pollRetry polls again when a signal interrupts the call. A timed poll
23// retries only for the time left of the original timeout and then reports a
24// timeout (0, nil). A zero timeout has no time to measure, so it makes at
25// most zeroTimeoutTries calls and then returns the EINTR.
26func pollRetry(fds []unix.PollFd, timeout time.Duration, poll func([]unix.PollFd, int) (int, error)) (int, error) {
27 deadline := time.Now().Add(timeout)
28 for tries := 1; ; tries++ {
29 n, e := poll(fds, int(timeout.Milliseconds()))
30 if e != unix.EINTR {
31 return n, e
32 }
33 if timeout > 0 {
34 if timeout = time.Until(deadline); timeout <= 0 {
35 return 0, nil
36 }
37 } else if tries >= zeroTimeoutTries {
38 return 0, e
39 }
40 }
41}
42