Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
fix: run gunzip only otherwise pass through
Browse files Browse the repository at this point in the history
  • Loading branch information
eitu5ami committed Dec 14, 2023
1 parent bb44fd7 commit b4f7ce1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 119 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/golang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
run: docker-compose up -d
- name: Run tests
run: go test -race -shuffle=on -v ./internal/...
env:
RPC_GATEWAY_NODE_URL_1: ${{ secrets.RPC_GATEWAY_NODE_URL_1 }}
RPC_GATEWAY_NODE_URL_2: ${{ secrets.RPC_GATEWAY_NODE_URL_2 }}
- name: Print out docker containers' logs
if: always()
run: docker-compose logs
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21-alpine3.18 AS builder
FROM golang:1.21-alpine3.19 AS builder

RUN apk add --update-cache \
git \
Expand Down
43 changes: 0 additions & 43 deletions internal/middleware/gzip.go

This file was deleted.

65 changes: 0 additions & 65 deletions internal/middleware/gzip_test.go

This file was deleted.

22 changes: 19 additions & 3 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"io"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"

"github.com/0xProject/rpc-gateway/internal/middleware"
"github.com/go-http-utils/headers"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand Down Expand Up @@ -100,6 +103,16 @@ func (h *Proxy) HasNodeProviderFailed(statusCode int) bool {
return statusCode >= http.StatusInternalServerError || statusCode == http.StatusTooManyRequests
}

func (h *Proxy) copyHeaders(dst http.ResponseWriter, src http.ResponseWriter) {
for k, v := range src.Header() {
if len(v) == 0 {
continue
}

dst.Header().Set(k, v[0])
}
}

func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body := &bytes.Buffer{}

Expand All @@ -113,22 +126,25 @@ func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pw := NewResponseWriter()
r.Body = io.NopCloser(bytes.NewBuffer(body.Bytes()))

if target.Config.Connection.HTTP.Compression {
middleware.Gzip(target.Proxy).ServeHTTP(pw, r)
} else {
if !target.Config.Connection.HTTP.Compression && strings.Contains(r.Header.Get(headers.ContentEncoding), "gzip") {
middleware.Gunzip(target.Proxy).ServeHTTP(pw, r)
} else {
target.Proxy.ServeHTTP(pw, r)
}

if h.HasNodeProviderFailed(pw.statusCode) {
h.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds())
h.metricResponseStatus.WithLabelValues(target.Config.Name, strconv.Itoa(pw.statusCode)).Inc()
h.metricRequestErrors.WithLabelValues(target.Config.Name, "rerouted").Inc()

continue
}
h.copyHeaders(w, pw)

w.WriteHeader(pw.statusCode)
w.Write(pw.body.Bytes()) // nolint:errcheck

h.metricResponseStatus.WithLabelValues(target.Config.Name, strconv.Itoa(pw.statusCode)).Inc()
h.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds())

return
Expand Down
10 changes: 3 additions & 7 deletions internal/rpcgateway/rpcgateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ func TestRpcGatewayFailover(t *testing.T) {
MaxConnsPerHost: 1,
}

t.Logf("gateway serving from: %s", gs.URL)

req, _ := http.NewRequest("POST", gs.URL, bytes.NewBufferString(``))
req, _ := http.NewRequest("POST", gs.URL, bytes.NewBufferString(rpcRequestBody))
req.Header.Set("Content-Type", "application/json")
req.ContentLength = int64(len(rpcRequestBody))

Expand All @@ -131,10 +129,8 @@ func TestRpcGatewayFailover(t *testing.T) {

assert.Equal(t, http.StatusOK, res.StatusCode)

bodyContent, _ := io.ReadAll(res.Body)

t.Log("Response from RPC gateway:")
t.Logf("%s", bodyContent)
_, err = io.ReadAll(res.Body)
assert.Nil(t, err)

err = gateway.Stop(context.TODO())
assert.Nil(t, err)
Expand Down

0 comments on commit b4f7ce1

Please sign in to comment.