Skip to content

Commit 76ed9fe

Browse files
committed
implement container start timeout
1 parent 76b8bec commit 76ed9fe

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

pkg/docker/runner.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,43 @@ func (n *Runner) Run(ctx context.Context, f fn.Function, address string, startTi
154154
return nil
155155
}
156156

157+
if startTimeout > 0 {
158+
startCtx, cancel := context.WithTimeout(context.Background(), startTimeout)
159+
defer cancel()
160+
161+
readyCh := make(chan struct{})
162+
go func() {
163+
ticker := time.NewTicker(100 * time.Millisecond)
164+
defer ticker.Stop()
165+
for {
166+
if err := dial(host, port, DefaultDialTimeout); err == nil {
167+
select {
168+
case readyCh <- struct{}{}:
169+
default:
170+
}
171+
return
172+
}
173+
select {
174+
case <-startCtx.Done():
175+
return
176+
case <-ticker.C:
177+
}
178+
}
179+
}()
180+
181+
select {
182+
case <-readyCh:
183+
184+
case err := <-runtimeErrCh:
185+
_ = stop()
186+
return nil, fmt.Errorf("container error before readiness: %w", err)
187+
case <-startCtx.Done():
188+
_ = stop()
189+
return nil, fmt.Errorf("timeout waiting for function to start")
190+
}
191+
192+
}
193+
157194
// Job reporting port, runtime errors and provides a mechanism for stopping.
158195
return fn.NewJob(f, host, port, runtimeErrCh, stop, n.verbose)
159196
}

0 commit comments

Comments
 (0)