Skip to content

Commit

Permalink
Do not send body/headers to groupcache.
Browse files Browse the repository at this point in the history
  • Loading branch information
udhos committed Feb 17, 2024
1 parent 1e0d1ac commit 2e4ddb4
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 128 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ go env -w CGO_ENABLED=1

go test -race ./...

#go test -bench=BenchmarkController ./cmd/gateboard
#go test -bench=BenchmarkController ./cmd/kubecache

go env -w CGO_ENABLED=0

Expand Down
13 changes: 3 additions & 10 deletions cmd/kubecache/fetch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"context"
"io"
"net/http"
Expand All @@ -10,24 +9,18 @@ import (
)

func fetch(c context.Context, client *http.Client, tracer trace.Tracer,
method, uri string, reqBody []byte, h http.Header) ([]byte, http.Header, int, error) {
method, uri string) ([]byte, http.Header, int, error) {

const me = "fetch"
ctx, span := tracer.Start(c, me)
defer span.End()

req, errReq := http.NewRequestWithContext(ctx, method, uri,
bytes.NewBuffer(reqBody))
req, errReq := http.NewRequestWithContext(ctx, method, uri, nil)
if errReq != nil {
return nil, nil, 500, errReq
}

// copy headers
for k, v := range h {
for _, vv := range v {
req.Header.Add(k, vv)
}
}
//req.Header.Add("key", "value")

resp, errDo := client.Do(req)
if errDo != nil {
Expand Down
14 changes: 1 addition & 13 deletions cmd/kubecache/groupcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,13 @@ func startGroupcache(app *application) func() {
return fmt.Errorf("getter: bad key: '%s'", key)
}

//
// retrieve request body/headers from context
//
v := ctx.Value(reqKey)
if v == nil {
return fmt.Errorf("getter: context key not found: %s", reqKey)
}
reqVal, ok := v.(*reqContextValue)
if !ok {
return fmt.Errorf("getter: bad context value for key: %s", reqKey)
}

u, errURL := url.JoinPath(app.cfg.backendURL, uri)
if errURL != nil {
return fmt.Errorf("getter: bad URL: %v", errURL)
}

body, respHeaders, status, errFetch := fetch(ctx, httpClient, app.tracer,
method, u, reqVal.body, reqVal.header)
method, u)

traceID := span.SpanContext().TraceID().String()
log.Info().Str("traceID", traceID).Msgf("getter: traceID=%s key='%s' url=%s status=%d error:%v",
Expand Down
32 changes: 3 additions & 29 deletions cmd/kubecache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"os/signal"
"strconv"
"syscall"
Expand Down Expand Up @@ -231,7 +230,7 @@ func (app *application) ServeHTTP(w http.ResponseWriter, r *http.Request) {

key := r.Method + " " + uri

resp, errFetch := app.query(ctx, key, r.Body, r.Header.Clone())
resp, errFetch := app.query(ctx, key)

elap := time.Since(begin)

Expand Down Expand Up @@ -262,32 +261,16 @@ func (app *application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (app *application) query(c context.Context, key string, body io.ReadCloser, h http.Header) (response, error) {
func (app *application) query(c context.Context, key string) (response, error) {

const me = "app.query"
ctx, span := app.tracer.Start(c, me)
defer span.End()

var resp response

reqBody, errBody := io.ReadAll(body)
if errBody != nil {
log.Error().Msgf("key='%s' body error:%v", key, errBody)
resp.Status = 500
return resp, errBody
}

//
// create context with request headers/body
//
reqVal := reqContextValue{
header: h,
body: reqBody,
}
ctxReq := context.WithValue(ctx, reqKey, &reqVal)

var data []byte
errGet := app.cache.Get(ctxReq, key, groupcache.AllocatingByteSliceSink(&data))
errGet := app.cache.Get(ctx, key, groupcache.AllocatingByteSliceSink(&data))

if errGet != nil {
log.Error().Msgf("key='%s' cache error:%v", key, errGet)
Expand All @@ -309,12 +292,3 @@ type response struct {
Status int `json:"status"`
Header http.Header `json:"header"`
}

type reqContextKey string

type reqContextValue struct {
header http.Header
body []byte
}

var reqKey = reqContextKey("reqContextKey")
Loading

0 comments on commit 2e4ddb4

Please sign in to comment.