forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retryable_garden_connection.go
117 lines (93 loc) · 2.27 KB
/
retryable_garden_connection.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
package worker
import (
"io"
"strings"
"time"
"code.cloudfoundry.org/garden"
gconn "code.cloudfoundry.org/garden/client/connection"
)
//go:generate counterfeiter . Sleeper
type Sleeper interface {
Sleep(time.Duration)
}
type RetryableConnection struct {
gconn.Connection
}
func NewRetryableConnection(connection gconn.Connection) *RetryableConnection {
return &RetryableConnection{
Connection: connection,
}
}
func (conn *RetryableConnection) Run(handle string, processSpec garden.ProcessSpec, processIO garden.ProcessIO) (garden.Process, error) {
innerProcess, err := conn.Connection.Run(handle, processSpec, processIO)
if err != nil {
return nil, err
}
return &retryableProcess{
Process: innerProcess,
rehydrate: func() (garden.Process, error) {
return conn.Attach(handle, innerProcess.ID(), processIO)
},
}, nil
}
func (conn *RetryableConnection) Attach(handle string, processID string, processIO garden.ProcessIO) (garden.Process, error) {
innerProcess, err := conn.Connection.Attach(handle, processID, processIO)
if err != nil {
return nil, err
}
return &retryableProcess{
Process: innerProcess,
rehydrate: func() (garden.Process, error) {
return conn.Attach(handle, processID, processIO)
},
}, nil
}
type retryableProcess struct {
garden.Process
rehydrate func() (garden.Process, error)
}
func (process *retryableProcess) Wait() (int, error) {
for {
status, err := process.Process.Wait()
if err == nil {
return status, nil
}
if !strings.HasSuffix(err.Error(), io.EOF.Error()) {
return 0, err
}
process.Process, err = process.rehydrate()
if err != nil {
return 0, err
}
}
}
func (process *retryableProcess) Signal(sig garden.Signal) error {
for {
err := process.Process.Signal(sig)
if err == nil {
return nil
}
if strings.Contains(err.Error(), "use of closed network connection") {
return err
}
process.Process, err = process.rehydrate()
if err != nil {
return err
}
}
}
func (process *retryableProcess) SetTTY(tty garden.TTYSpec) error {
for {
err := process.Process.SetTTY(tty)
if err == nil {
return nil
}
if strings.Contains(err.Error(), "use of closed network connection") {
return err
}
process.Process, err = process.rehydrate()
if err != nil {
return err
}
}
}