-
Notifications
You must be signed in to change notification settings - Fork 4
/
timeout.go
192 lines (168 loc) · 4.05 KB
/
timeout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Package timeout is for handling timeout invocation of external command
package timeout
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"syscall"
"time"
"github.com/Songmu/wrapcommander"
)
// exit statuses are same with GNU timeout
const (
exitNormal = 0
exitTimedOut = 124
exitUnknownErr = 125
exitKilled = 137
)
// overwritten with syscall.SIGTERM on unix environment (see timeout_unix.go)
var defaultSignal = os.Interrupt
// Error is error of timeout
type Error struct {
ExitCode int
Err error
}
func (err *Error) Error() string {
return fmt.Sprintf("exit code: %d, %s", err.ExitCode, err.Err.Error())
}
// Timeout is main struct of timeout package
type Timeout struct {
Duration time.Duration
KillAfter time.Duration
Signal os.Signal
Foreground bool
Cmd *exec.Cmd
KillAfterCancel time.Duration
}
func (tio *Timeout) signal() os.Signal {
if tio.Signal == nil {
return defaultSignal
}
return tio.Signal
}
// Run is synchronous interface of executing command and returning information
func (tio *Timeout) Run() (*ExitStatus, string, string, error) {
cmd := tio.getCmd()
var outBuffer, errBuffer bytes.Buffer
cmd.Stdout = &outBuffer
cmd.Stderr = &errBuffer
ch, err := tio.RunCommand()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, string(outBuffer.Bytes()), string(errBuffer.Bytes()), err
}
exitSt := <-ch
return exitSt, string(outBuffer.Bytes()), string(errBuffer.Bytes()), nil
}
// RunSimple executes command and only returns integer as exit code. It is mainly for go-timeout command
func (tio *Timeout) RunSimple(preserveStatus bool) int {
cmd := tio.getCmd()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
ch, err := tio.RunCommand()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return getExitCodeFromErr(err)
}
exitSt := <-ch
if preserveStatus {
return exitSt.GetChildExitCode()
}
return exitSt.GetExitCode()
}
func getExitCodeFromErr(err error) int {
if err != nil {
if tmerr, ok := err.(*Error); ok {
return tmerr.ExitCode
}
return -1
}
return exitNormal
}
// RunContext runs command with context
func (tio *Timeout) RunContext(ctx context.Context) (*ExitStatus, error) {
if err := tio.start(); err != nil {
return nil, err
}
return tio.wait(ctx), nil
}
// RunCommand is executing the command and handling timeout. This is primitive interface of Timeout
func (tio *Timeout) RunCommand() (<-chan *ExitStatus, error) {
if err := tio.start(); err != nil {
return nil, err
}
exitChan := make(chan *ExitStatus)
go func() {
exitChan <- tio.wait(context.Background())
}()
return exitChan, nil
}
func (tio *Timeout) start() error {
if err := tio.getCmd().Start(); err != nil {
return &Error{
ExitCode: wrapcommander.ResolveExitCode(err),
Err: err,
}
}
return nil
}
func (tio *Timeout) wait(ctx context.Context) *ExitStatus {
ex := &ExitStatus{}
cmd := tio.getCmd()
exitChan := getExitChan(cmd)
killCh := make(chan struct{}, 2)
done := make(chan struct{})
defer close(done)
delayedKill := func(dur time.Duration) {
select {
case <-done:
return
case <-time.After(dur):
killCh <- struct{}{}
}
}
if tio.KillAfter > 0 {
go delayedKill(tio.Duration + tio.KillAfter)
}
for {
select {
case st := <-exitChan:
ex.Code = wrapcommander.WaitStatusToExitCode(st)
ex.Signaled = st.Signaled()
return ex
case <-time.After(tio.Duration):
tio.terminate()
ex.typ = exitTypeTimedOut
case <-killCh:
tio.killall()
// just to make sure
cmd.Process.Kill()
ex.killed = true
if ex.typ != exitTypeCanceled {
ex.typ = exitTypeKilled
}
case <-ctx.Done():
// XXX handling etx.Err()?
tio.terminate()
ex.typ = exitTypeCanceled
go delayedKill(tio.getKillAfterCancel())
}
}
}
func (tio *Timeout) getKillAfterCancel() time.Duration {
if tio.KillAfterCancel == 0 {
return 3 * time.Second
}
return tio.KillAfterCancel
}
func getExitChan(cmd *exec.Cmd) chan syscall.WaitStatus {
ch := make(chan syscall.WaitStatus)
go func() {
err := cmd.Wait()
st, _ := wrapcommander.ErrorToWaitStatus(err)
ch <- st
}()
return ch
}