File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,20 @@ var bufPool = sync.Pool{
44
44
},
45
45
}
46
46
47
+ // sleep returns nil after the specified duration or error if interrupted.
48
+ // Modified slightly from
49
+ // from https://stackoverflow.com/questions/55135239/how-can-i-sleep-with-responsive-context-cancelation
50
+ func sleep (ctx context.Context , d time.Duration ) error {
51
+ t := time .NewTimer (d )
52
+ select {
53
+ case <- ctx .Done ():
54
+ t .Stop ()
55
+ return fmt .Errorf ("Interrupted: %w" , ctx .Err ())
56
+ case <- t .C :
57
+ }
58
+ return nil
59
+ }
60
+
47
61
func receive (conn net.Conn , out chan * bytes.Buffer ) {
48
62
log .Printf ("New connection from %s" , conn .RemoteAddr ())
49
63
defer conn .Close ()
@@ -132,7 +146,7 @@ func (w *Worker) ConnectWithRetries(ctx context.Context) error {
132
146
if ctx .Err () != nil {
133
147
return err
134
148
}
135
- time . Sleep ( delay )
149
+ sleep ( ctx , delay )
136
150
delay *= 2
137
151
if delay > 30 * time .Second {
138
152
delay = 30 * time .Second
You can’t perform that action at this time.
0 commit comments