-
Notifications
You must be signed in to change notification settings - Fork 110
feat: add ETag/caching support in OFREP #1854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
46db024
d6b50a5
bfa8a17
f1b017f
567fcbb
73f07a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package ofrep | |
|
|
||
| import ( | ||
| "context" | ||
| "crypto/md5" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
|
|
@@ -19,12 +20,17 @@ import ( | |
| "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/trace" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| const ( | ||
| key = "key" | ||
| singleEvaluation = "/ofrep/v1/evaluate/flags/{key}" | ||
| bulkEvaluation = "/ofrep/v1/evaluate/{path:flags\\/|flags}" | ||
| key = "key" | ||
| singleEvaluation = "/ofrep/v1/evaluate/flags/{key}" | ||
| bulkEvaluation = "/ofrep/v1/evaluate/{path:flags\\/|flags}" | ||
| headerETag = "ETag" | ||
| headerIfNoneMatch = "If-None-Match" | ||
| headerContentType = "Content-Type" | ||
| contentTypeJSON = "application/json" | ||
| ) | ||
|
|
||
| type handler struct { | ||
|
|
@@ -128,7 +134,37 @@ func (h *handler) HandleBulkEvaluation(w http.ResponseWriter, r *http.Request) { | |
| fmt.Sprintf("Bulk evaluation failed. Tracking ID: %s", requestID)) | ||
| h.writeJSONToResponse(http.StatusInternalServerError, res, w) | ||
| } else { | ||
| h.writeJSONToResponse(http.StatusOK, ofrep.BulkEvaluationResponseFrom(evaluations, metadata), w) | ||
| response := ofrep.BulkEvaluationResponseFrom(evaluations, metadata) | ||
| h.writeBulkEvaluationResponse(w, r, response) | ||
| } | ||
| } | ||
|
|
||
| // writes the bulk evaluation response with ETag support | ||
| func (h *handler) writeBulkEvaluationResponse(w http.ResponseWriter, r *http.Request, response ofrep.BulkEvaluationResponse) { | ||
| // calculate ETag and marshal response in one operation | ||
| eTag, body, err := calculateETag(response) | ||
| if err != nil { | ||
| h.Logger.Warn("error calculating ETag", zap.Error(err)) | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| // check If-None-Match header for cache validation | ||
| ifNoneMatch := r.Header.Get(headerIfNoneMatch) | ||
| if ifNoneMatch == eTag { | ||
| // ETag matches, return 304 Not Modified | ||
| w.Header().Add(headerETag, eTag) | ||
| w.WriteHeader(http.StatusNotModified) | ||
| return | ||
| } | ||
|
|
||
| // ETag doesn't match or missing, return the response with the new ETag | ||
| w.Header().Add(headerContentType, contentTypeJSON) | ||
| w.Header().Add(headerETag, eTag) | ||
| w.WriteHeader(http.StatusOK) | ||
| _, err = w.Write(body) | ||
| if err != nil { | ||
| h.Logger.Warn("error while writing response", zap.Error(err)) | ||
| } | ||
| } | ||
|
toddbaert marked this conversation as resolved.
|
||
|
|
||
|
|
@@ -137,17 +173,29 @@ func (h *handler) writeJSONToResponse(status int, payload interface{}, w http.Re | |
| marshal, err := json.Marshal(payload) | ||
| if err != nil { | ||
| // always a 500 | ||
| h.Logger.Warn(fmt.Sprintf("error marshelling the response: %v", err)) | ||
| h.Logger.Warn("error marshalling the response", zap.Error(err)) | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Add("Content-Type", "application/json") | ||
| w.Header().Add(headerContentType, contentTypeJSON) | ||
| w.WriteHeader(status) | ||
| _, err = w.Write(marshal) | ||
| if err != nil { | ||
| h.Logger.Warn(fmt.Sprintf("error while writing response: %v", err)) | ||
| h.Logger.Warn("error while writing response", zap.Error(err)) | ||
| } | ||
| } | ||
|
|
||
| // calculateETag generates an ETag from the bulk evaluation response | ||
| func calculateETag(response ofrep.BulkEvaluationResponse) (string, []byte, error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: For the ETag calculation the flags must be in the same order every time, is this guaranteed?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great point! Ordering in maps in Go is intentionally non-deterministic so that devs cannot rely on it, but the JSON marshalling in Go does sort them: https://github.com/golang/go/blob/master/src/encoding/json/encode.go#L183-L189 So we can rely on that.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay good to know, thank you!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL as well (the json marshall ordering bit). I know maps were purposely random in Go, so I was quite scared for a second. |
||
| // marshal the response to JSON | ||
| data, err := json.Marshal(response) | ||
| if err != nil { | ||
| return "", nil, fmt.Errorf("failed to marshal response for ETag calculation: %w", err) | ||
| } | ||
|
|
||
| hash := md5.Sum(data) | ||
| return fmt.Sprintf("\"%x\"", hash), data, nil | ||
| } | ||
|
|
||
| func extractOfrepRequest(req *http.Request) (ofrep.Request, error) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.