Skip to content

Commit 61d9f83

Browse files
committed
refactor redis tests and add asyncRevalidate
1 parent cae4736 commit 61d9f83

File tree

7 files changed

+4794
-75
lines changed

7 files changed

+4794
-75
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ jobs:
2121
# - name: Run linter
2222
# run: make lint
2323
- name: Run unit tests
24-
env:
25-
REDIS_CONNECTION_STRING: ${{ secrets.REDIS_CONNECTION_STRING }}
2624
run: |
2725
go test -race -v -covermode=atomic --coverprofile=coverage.txt ./...
2826
go test -json ./... > test-report.json

pkg/http/cache.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"fmt"
99
"io"
1010
"io/ioutil"
11+
"net"
1112
"net/http"
1213
"net/http/httputil"
1314
"strings"
15+
"time"
1416

1517
"github.com/rs/zerolog/log"
1618

@@ -96,6 +98,43 @@ func newCacheHandler(c Cacher, m MetricsCollector, cfg config.CacheConfig) handl
9698
return handler{
9799
cacher: c,
98100
metricsCollector: m,
101+
cfg: cfg,
102+
}
103+
}
104+
105+
func (h handler) asyncCacheRevalidate(hashKey string, res http.ResponseWriter, req *http.Request) {
106+
ctx := context.Background()
107+
newReq := req.WithContext(ctx)
108+
109+
netTransport := &http.Transport{
110+
MaxIdleConnsPerHost: 1000,
111+
DisableKeepAlives: false,
112+
IdleConnTimeout: time.Hour * 1,
113+
Dial: (&net.Dialer{
114+
Timeout: 10 * time.Second,
115+
KeepAlive: 30 * time.Second,
116+
}).Dial,
117+
TLSHandshakeTimeout: 10 * time.Second,
118+
ResponseHeaderTimeout: 10 * time.Second,
119+
}
120+
client := &http.Client{
121+
Timeout: time.Second * 10,
122+
Transport: netTransport,
123+
}
124+
125+
newReq.URL.Host = h.cfg.DownstreamHost.Host
126+
newReq.URL.Scheme = h.cfg.DownstreamHost.Scheme
127+
newReq.RequestURI = ""
128+
resp, err := client.Do(newReq)
129+
if err != nil {
130+
log.Ctx(ctx).Error().Err(err).Msg("Errored when sending request to the server")
131+
132+
return
133+
}
134+
err = h.cacheResponse(ctx, hashKey)(resp)
135+
136+
if err != nil {
137+
log.Print("Error occurred caching response")
99138
}
100139
}
101140

@@ -138,7 +177,9 @@ func (h handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
138177
logger.Info().Msg("serving request from memory")
139178
h.metricsCollector.CacheHit(req.Method, result.Status)
140179

141-
// if result.IsStale() => async fetch from downstream
180+
if result.IsStale() {
181+
go h.asyncCacheRevalidate(hashKey, res, req)
182+
}
142183
serveResponseFromMemory(res, result)
143184
}
144185

pkg/http/cache_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func mustURL(t *testing.T, downstreamURL string) *url.URL {
6565

6666
func TestCacheHandler(t *testing.T) {
6767
ctrl := gomock.NewController(t)
68+
defer ctrl.Finish()
6869

6970
proxied := http.StatusUseProxy
7071
endpoint := "/status/200?q=1"

pkg/http/server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ func RunServer(opts ServerOpts) {
3232

3333
mux.HandleFunc("/probes/readiness", readinessHandler(opts.ReadinessChecker))
3434

35-
server := &h.Server{Addr: fmt.Sprintf("0.0.0.0:%s", opts.Config.ServerConfig.Port), Handler: mux}
35+
server := &h.Server{
36+
Addr: fmt.Sprintf("0.0.0.0:%s", opts.Config.ServerConfig.Port),
37+
Handler: mux,
38+
ReadTimeout: 5 * time.Second,
39+
WriteTimeout: 10 * time.Second,
40+
}
3641
serverCtx, serverStopCtx := context.WithCancel(context.Background())
3742

3843
log.Info().Msgf("Starting server on port %s", opts.Config.ServerConfig.Port)

0 commit comments

Comments
 (0)