Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix proxy retry loop #635

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions runner/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package runner

import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -106,18 +104,20 @@ func (p *Proxy) proxyHandler(w http.ResponseWriter, r *http.Request) {
}
req.Header.Set("X-Forwarded-For", r.RemoteAddr)

// retry on connection refused error since after a file change air will restart the server and it may take a few milliseconds for the server to be up-and-running.
// air will restart the server. it may take a few milliseconds for it to start back up.
// therefore, we retry until the server becomes available or this retry loop exits with an error.
var resp *http.Response
resp, err = p.client.Do(req)
for i := 0; i < 10; i++ {
resp, err = p.client.Do(req)
if err == nil {
break
}
if !errors.Is(err, syscall.ECONNREFUSED) {
http.Error(w, "proxy handler: unable to reach app", http.StatusInternalServerError)
return
}
time.Sleep(100 * time.Millisecond)
resp, err = p.client.Do(req)
}
if err != nil {
http.Error(w, "proxy handler: unable to reach app", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

Expand Down
Loading