Skip to content

Commit

Permalink
fix proxy retry loop (#635)
Browse files Browse the repository at this point in the history
This addresses two bugs:
1) The loop could exit with an error condition present and fail to
   inform the user.
2) On Windows 11, syscall.ECONNREFUSED is not returned. Instead we
   receive net.OpError with a message similar to:

"unable to reach app: Get "http://localhost:7000/": dial tcp [::1]:7000: connectex: No connection could be made because the target machine actively refused it."

The Fix:
Since the retry loop is short; just 1 second maximum, we retry until no
error happens. If an error does occur, we inform the user.
  • Loading branch information
farmergreg authored Sep 21, 2024
1 parent 90c8f9e commit f4f7c3d
Showing 1 changed file with 8 additions and 8 deletions.
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

0 comments on commit f4f7c3d

Please sign in to comment.