forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrequest_forwarder.go
96 lines (86 loc) · 2.54 KB
/
request_forwarder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/go-logr/logr"
)
func forwardRequest(
lggr logr.Logger,
w http.ResponseWriter,
r *http.Request,
roundTripper http.RoundTripper,
fwdSvcURL *url.URL,
maxRetries int,
) {
proxy := httputil.NewSingleHostReverseProxy(fwdSvcURL)
proxy.Transport = roundTripper
proxy.Director = func(req *http.Request) {
req.URL = fwdSvcURL
req.Host = fwdSvcURL.Host
req.URL.Path = r.URL.Path
req.URL.RawQuery = r.URL.RawQuery
req.Header.Del("X-Forwarded-For")
}
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(502)
errMsg := fmt.Errorf("error on backend (%w)", err).Error()
if _, err := w.Write([]byte(errMsg)); err != nil {
lggr.Error(
err,
"could not write error response to client",
)
}
}
proxy.ModifyResponse = func(resp *http.Response) error {
if resp.StatusCode == http.StatusServiceUnavailable {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(resp.Body)
if err != nil {
return err
}
resp.Body.Close()
resp.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
// Check if the response body starts with "upstream connect error or disconnect/reset before headers"
if bytes.HasPrefix(buf.Bytes(), []byte("upstream connect error or disconnect/reset before headers")) {
updatedResp, err := retryRequest(lggr, resp, maxRetries, 0)
if err != nil {
return err
}
*resp = *updatedResp
}
}
return nil
}
proxy.ServeHTTP(w, r)
}
func retryRequest(lggr logr.Logger, resp *http.Response, maxRetries, attempt int) (*http.Response, error) {
if attempt >= maxRetries {
lggr.Error(nil, "Max retries reached, returning last response")
return resp, nil
}
lggr.Info("Service unavailable, retrying", "attempt", attempt+1)
time.Sleep(time.Second * time.Duration(2*(attempt+1)))
newResp, err := http.DefaultTransport.RoundTrip(resp.Request)
if err != nil {
return retryRequest(lggr, resp, maxRetries, attempt+1)
}
if newResp.StatusCode == http.StatusServiceUnavailable {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(newResp.Body)
if err != nil {
return nil, err
}
newResp.Body.Close()
newResp.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
// Check if the response body starts with "upstream connect error or disconnect/reset before headers"
if bytes.HasPrefix(buf.Bytes(), []byte("upstream connect error or disconnect/reset before headers")) {
return retryRequest(lggr, newResp, maxRetries, attempt+1)
}
}
return newResp, nil
}