Skip to content

Commit f68451d

Browse files
committed
remove gzip handling from cacheHandler
1 parent ba44764 commit f68451d

File tree

3 files changed

+9
-39
lines changed

3 files changed

+9
-39
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ build:
2828
env GOARCH=arm64 GOOS=darwin go build -o bin/${BIN}-mac -ldflags="-s -w" -v ./cmd/${BIN}.go
2929

3030
run: fresh
31-
./bin/${BIN}-mac
31+
go run ./cmd/${BIN}.go
3232

3333
fresh: clean build
3434

app-tests/vercel-commerce-cache-offloader.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ COPY entrypoint.sh .
2828
ENV DOWNSTREAM_HOST=http://localhost:3000
2929
ENV CACHE_SHOULD_HASH_QUERY="true"
3030
ENV CACHE_STRATEGY=LFU
31-
ENV CACHE_SIZE_MB=10
31+
ENV CACHE_SIZE_MB=500
3232
ENV CACHE_STALE_WHILE_REVALIDATE_SEC=60
3333
ENV SERVER_PORT=8000
3434
ENV SERVER_STORAGE=memory

pkg/http/cache.go

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package http
22

33
import (
44
"bytes"
5-
"compress/gzip"
65
"context"
76
"crypto/sha256"
87
"fmt"
@@ -43,12 +42,6 @@ type (
4342
}
4443
)
4544

46-
func handleGzipServeErr(err error) {
47-
if err != nil {
48-
log.Error().Err(err).Msg("Error occurred serving gzip response from memory")
49-
}
50-
}
51-
5245
func (h handler) getCacheKey(req *http.Request) string {
5346
cacheKey := sha256.New()
5447
cacheKey.Write([]byte(req.URL.Path))
@@ -75,21 +68,8 @@ func serveResponseFromMemory(res http.ResponseWriter, result *model.Response) {
7568
}
7669

7770
res.WriteHeader(result.Status)
78-
79-
if res.Header().Get("content-encoding") == "gzip" {
80-
var b bytes.Buffer
81-
gz := gzip.NewWriter(&b)
82-
_, err := gz.Write(result.Body)
83-
handleGzipServeErr(err)
84-
err = gz.Close()
85-
handleGzipServeErr(err)
86-
_, err = res.Write(b.Bytes())
87-
handleGzipServeErr(err)
88-
89-
return
90-
}
91-
9271
_, err := res.Write(result.Body)
72+
9373
if err != nil {
9474
log.Error().Err(err).Msg("Error occurred serving response from memory")
9575
}
@@ -134,15 +114,17 @@ func (h handler) asyncCacheRevalidate(hashKey string, req *http.Request) func()
134114
newReq.URL.Scheme = h.cfg.DownstreamHost.Scheme
135115
newReq.RequestURI = ""
136116
resp, err := client.Do(newReq)
117+
if resp != nil {
118+
defer resp.Body.Close()
119+
}
137120
if err != nil {
138121
log.Ctx(ctx).Error().Err(err).Msg("Errored when sending request to the server")
139122

140123
return
141124
}
142-
err = h.cacheResponse(ctx, hashKey)(resp)
143125

144-
if err != nil {
145-
log.Print("Error occurred caching response")
126+
if err := h.cacheResponse(ctx, hashKey)(resp); err != nil {
127+
log.Ctx(ctx).Error().Err(err).Msg("Errored when caching response")
146128
}
147129
}
148130
}
@@ -207,19 +189,7 @@ func (h handler) cacheResponse(ctx context.Context, hashKey string) func(*http.R
207189
return nil
208190
}
209191

210-
var body []byte
211-
var readErr error
212-
if response.Header.Get("content-encoding") == "gzip" {
213-
reader, err := gzip.NewReader(response.Body)
214-
if err != nil {
215-
logger.Error().Err(err).Msg("error occurred creating gzip reader")
216-
217-
return nil
218-
}
219-
body, readErr = io.ReadAll(reader)
220-
} else {
221-
body, readErr = io.ReadAll(response.Body)
222-
}
192+
body, readErr := io.ReadAll(response.Body)
223193

224194
if readErr != nil {
225195
logger.Error().Err(readErr).Msg("error occurred reading response body")

0 commit comments

Comments
 (0)