-
Notifications
You must be signed in to change notification settings - Fork 14
[Handler] Integrate the watcher's cache into request handler #78
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
Merged
TomerShor
merged 10 commits into
v3io:development
from
weilerN:nuc-523-integrate-cache-into-request-handler-clean
Jul 21, 2025
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
05a5266
Add IngressHostCacheReader into the handler's init
weilerN c39e431
Implement new logic resolving before BC logic
weilerN 13112a5
Unit tests
weilerN 9f812b6
Minor code improvements- avoid shadow declarations and remove unused …
weilerN 7877368
CR comment - reuse existing ingressValue struct
weilerN 42e20f5
CR comment - align namings
weilerN a118f1a
CR comment - unit test comments
weilerN dfc8f38
CR comment - keep the parseURl as is
weilerN 7e8c261
CR comments - Unit tests - add request header flow coverage
weilerN 6527727
CR comments - handler req.URL is nil
weilerN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -30,6 +30,7 @@ import ( | |||||
| "sync" | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/v3io/scaler/pkg/ingresscache" | ||||||
| "github.com/v3io/scaler/pkg/scalertypes" | ||||||
|
|
||||||
| "github.com/nuclio/errors" | ||||||
|
|
@@ -49,6 +50,7 @@ type Handler struct { | |||||
| targetURLCache *cache.LRUExpireCache | ||||||
| proxyLock sync.Locker | ||||||
| lastProxyErrorTime time.Time | ||||||
| ingressCache ingresscache.IngressHostCacheReader | ||||||
| } | ||||||
|
|
||||||
| func NewHandler(parentLogger logger.Logger, | ||||||
|
|
@@ -57,7 +59,8 @@ func NewHandler(parentLogger logger.Logger, | |||||
| targetNameHeader string, | ||||||
| targetPathHeader string, | ||||||
| targetPort int, | ||||||
| multiTargetStrategy scalertypes.MultiTargetStrategy) (Handler, error) { | ||||||
| multiTargetStrategy scalertypes.MultiTargetStrategy, | ||||||
| ingressCache ingresscache.IngressHostCacheReader) (Handler, error) { | ||||||
| h := Handler{ | ||||||
| logger: parentLogger.GetChild("handler"), | ||||||
| resourceStarter: resourceStarter, | ||||||
|
|
@@ -69,17 +72,17 @@ func NewHandler(parentLogger logger.Logger, | |||||
| targetURLCache: cache.NewLRUExpireCache(100), | ||||||
| proxyLock: &sync.Mutex{}, | ||||||
| lastProxyErrorTime: time.Now(), | ||||||
| ingressCache: ingressCache, | ||||||
| } | ||||||
| h.HandleFunc = h.handleRequest | ||||||
| return h, nil | ||||||
| } | ||||||
|
|
||||||
| func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) { | ||||||
| var path string | ||||||
| var err error | ||||||
| var resourceNames []string | ||||||
|
|
||||||
| responseChannel := make(chan ResourceStatusResult, 1) | ||||||
| defer close(responseChannel) | ||||||
|
|
||||||
| // first try to see if our request came from ingress controller | ||||||
| forwardedHost := req.Header.Get("X-Forwarded-Host") | ||||||
| forwardedPort := req.Header.Get("X-Forwarded-Port") | ||||||
|
|
@@ -97,15 +100,15 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) { | |||||
| resourceNames = append(resourceNames, resourceName) | ||||||
| resourceTargetURLMap[resourceName] = targetURL | ||||||
| } else { | ||||||
| targetNameHeaderValue := req.Header.Get(h.targetNameHeader) | ||||||
| path := req.Header.Get(h.targetPathHeader) | ||||||
| if targetNameHeaderValue == "" { | ||||||
| h.logger.WarnWith("When ingress not set, must pass header value", | ||||||
| "missingHeader", h.targetNameHeader) | ||||||
| path, resourceNames, err = h.getPathAndResourceNames(req) | ||||||
| if err != nil { | ||||||
| h.logger.WarnWith("Failed to get resource names and path from request", | ||||||
| "error", err.Error(), | ||||||
| "host", req.Host, | ||||||
| "path", req.URL.Path) | ||||||
| res.WriteHeader(http.StatusBadRequest) | ||||||
| return | ||||||
| } | ||||||
| resourceNames = strings.Split(targetNameHeaderValue, ",") | ||||||
| for _, resourceName := range resourceNames { | ||||||
| targetURL, status := h.parseTargetURL(resourceName, path) | ||||||
| if targetURL == nil { | ||||||
|
|
@@ -163,6 +166,43 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) { | |||||
| proxy.ServeHTTP(res, req) | ||||||
| } | ||||||
|
|
||||||
| func (h *Handler) getPathAndResourceNames(req *http.Request) (string, []string, error) { | ||||||
| // first try to get the resource names and path from the ingress cache | ||||||
| path, resourceNames, err := h.getValuesFromCache(req) | ||||||
| if err == nil { | ||||||
| return path, resourceNames, nil | ||||||
| } | ||||||
|
|
||||||
| h.logger.DebugWith("Failed to get resource names from ingress cache, trying to extract from the request headers", | ||||||
| "host", req.Host, | ||||||
| "path", req.URL.Path, | ||||||
| "error", err.Error()) | ||||||
|
|
||||||
| // old implementation for backward compatibility | ||||||
| targetNameHeaderValue := req.Header.Get(h.targetNameHeader) | ||||||
| path = req.Header.Get(h.targetPathHeader) | ||||||
| if targetNameHeaderValue == "" { | ||||||
| return "", nil, errors.New("No target name header found") | ||||||
| } | ||||||
| resourceNames = strings.Split(targetNameHeaderValue, ",") | ||||||
| return path, resourceNames, nil | ||||||
| } | ||||||
|
|
||||||
| func (h *Handler) getValuesFromCache(req *http.Request) (string, []string, error) { | ||||||
| host := req.Host | ||||||
| path := req.URL.Path | ||||||
| resourceNames, err := h.ingressCache.Get(host, path) | ||||||
| if err != nil { | ||||||
| return "", nil, errors.New("Failed to get resourceNames from ingress cache") | ||||||
|
||||||
| return "", nil, errors.New("Failed to get resourceNames from ingress cache") | |
| return "", nil, errors.New("Failed to get resource names from ingress cache") |
Collaborator
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that req.URL is pointer, so might technically be nil, do we check it anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I will cover it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comments - handler req.URL is nil