Skip to content

Commit 072fe79

Browse files
committed
fix: timeout for open
1 parent de31cc7 commit 072fe79

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

pkg/devspace/devpod/devpod.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,8 @@ func (d *devPod) start(ctx *devspacecontext.Context, devPodConfig *latest.DevPod
271271
case <-ctx.Context.Done():
272272
return nil
273273
case <-time.After(time.Second):
274-
resp, _ := http.Get(url)
275-
if resp != nil && resp.StatusCode != http.StatusBadGateway && resp.StatusCode != http.StatusServiceUnavailable {
276-
time.Sleep(time.Second * 1)
277-
_ = open.Start(url)
278-
ctx.Log.Donef("Successfully opened %s", url)
274+
err := tryOpen(ctx.Context, url, ctx.Log)
275+
if err == nil {
279276
return nil
280277
}
281278
}
@@ -308,6 +305,34 @@ func (d *devPod) start(ctx *devspacecontext.Context, devPodConfig *latest.DevPod
308305
return d.startLogs(ctx, devPodConfig, selectedPod, parent)
309306
}
310307

308+
func tryOpen(ctx context.Context, url string, log logpkg.Logger) error {
309+
timeoutCtx, cancel := context.WithTimeout(ctx, time.Second)
310+
defer cancel()
311+
312+
req, err := http.NewRequestWithContext(timeoutCtx, "GET", url, nil)
313+
if err != nil {
314+
return err
315+
}
316+
317+
resp, err := http.DefaultClient.Do(req)
318+
if err != nil {
319+
return err
320+
}
321+
defer resp.Body.Close()
322+
if resp != nil && resp.StatusCode != http.StatusBadGateway && resp.StatusCode != http.StatusServiceUnavailable {
323+
select {
324+
case <-ctx.Done():
325+
return nil
326+
case <-time.After(time.Second):
327+
}
328+
_ = open.Start(url)
329+
log.Donef("Successfully opened %s", url)
330+
return nil
331+
}
332+
333+
return fmt.Errorf("not reachable")
334+
}
335+
311336
func (d *devPod) startLogs(ctx *devspacecontext.Context, devPodConfig *latest.DevPod, selectedPod *selector.SelectedPodContainer, parent *tomb.Tomb) error {
312337
ctx = ctx.WithLogger(ctx.Log.WithPrefixColor("logs ", "yellow+b"))
313338
loader.EachDevContainer(devPodConfig, func(devContainer *latest.DevContainer) bool {

0 commit comments

Comments
 (0)