Skip to content

Commit

Permalink
fixup! agent/http: refactor proxy for simpler error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
roobre committed Aug 4, 2023
1 parent 4c63bff commit 5acc9cd
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pkg/agent/protocol/http/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func (h *httpHandler) isExcluded(r *http.Request) bool {
return false
}

// proxy proxies a request to the upstream URL.
// Request is performed immediately, but response won't be sent until sendAt.
func (h *httpHandler) proxy(rw http.ResponseWriter, req *http.Request, delay time.Duration) {
// forward forwards a request to the upstream URL.
// Request is performed immediately, but response won't be sent before the duration specified in delay.
func (h *httpHandler) forward(rw http.ResponseWriter, req *http.Request, delay time.Duration) {
timer := time.After(delay)

upstreamReq := req.Clone(context.Background())
Expand Down Expand Up @@ -137,8 +137,8 @@ func (h *httpHandler) proxy(rw http.ResponseWriter, req *http.Request, delay tim
_, _ = io.Copy(rw, response.Body)
}

// fault sleeps until sendAt and then writes the configured error downstream.
func (h *httpHandler) fault(rw http.ResponseWriter, delay time.Duration) {
// injectError waits sleeps the duration specified in delay and then writes the configured error downstream.
func (h *httpHandler) injectError(rw http.ResponseWriter, delay time.Duration) {
time.Sleep(delay)

rw.WriteHeader(int(h.disruption.ErrorCode))
Expand All @@ -148,7 +148,7 @@ func (h *httpHandler) fault(rw http.ResponseWriter, delay time.Duration) {
func (h *httpHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if h.isExcluded(req) {
//nolint:contextcheck // Unclear which context the linter requires us to propagate here.
h.proxy(rw, req, 0)
h.forward(rw, req, 0)
return
}

Expand All @@ -159,12 +159,12 @@ func (h *httpHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

if h.disruption.ErrorRate > 0 && rand.Float32() <= h.disruption.ErrorRate {
h.fault(rw, delay)
h.injectError(rw, delay)
return
}

//nolint:contextcheck // Unclear which context the linter requires us to propagate here.
h.proxy(rw, req, delay)
h.forward(rw, req, delay)
}

// Start starts the execution of the proxy
Expand Down

0 comments on commit 5acc9cd

Please sign in to comment.