Skip to content

Commit

Permalink
fix: in retry I have to read the body again
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda committed Feb 20, 2023
1 parent e4d1ff1 commit b18cea7
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"bytes"
"errors"
"fmt"
"github.com/interfacerproject/interfacer-gateway/config"
Expand Down Expand Up @@ -140,25 +141,40 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
}

func (p *ProxiedHost) proxyRequest(w http.ResponseWriter, r *http.Request) {
reqUrl := p.buildUrl(r.URL).String()
req, err := http.NewRequest(r.Method, reqUrl, r.Body)
// Can't really fail due to method, url, and the body are provided by the std lib.
var err error
saveBody, err := io.ReadAll(r.Body)
if err != nil {
logger.Log.WithFields(logrus.Fields{
"app": p.name,
"host": r.RemoteAddr,
"error": err.Error(),
}).Errorf("client: could not create request: %s", err.Error())
}).Errorf("client: could not read body of request: %s", err.Error())

w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "client: could not create request\n")
fmt.Fprintf(w, "client: could not read body of request\n")
return
}
req.Header = r.Header
maxRetry := 3
var res *http.Response = nil

reqUrl := p.buildUrl(r.URL).String()

for i := 0; ; i = i + 1 {
var err error

req, err := http.NewRequest(r.Method, reqUrl, bytes.NewReader(saveBody))
// Can't really fail due to method, url, and the body are provided by the std lib.
if err != nil {
logger.Log.WithFields(logrus.Fields{
"app": p.name,
"host": r.RemoteAddr,
"error": err.Error(),
}).Errorf("client: could not create request: %s", err.Error())

w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "client: could not create request\n")
return
}
req.Header = r.Header
res, err = client.Do(req)
if err == nil {
break
Expand Down

0 comments on commit b18cea7

Please sign in to comment.