Skip to content
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

Added LBEndpoint field in routing.LBContext struct #2648

Merged
merged 1 commit into from
Oct 5, 2023

Conversation

RomanZavodskikh
Copy link
Member

@RomanZavodskikh RomanZavodskikh commented Oct 5, 2023

The routing.Route field of routing.LBContext struct will be eventually removed because loadbalancer in general does not require the full information about route, only about endpoints to balance load between.

You can think about this PR as preliminary one for the #2634

proxy/proxy.go Outdated
@@ -469,6 +469,8 @@ func setRequestURLForDynamicBackend(u *url.URL, stateBag map[string]interface{})
}

func setRequestURLForLoadBalancedBackend(u *url.URL, rt *routing.Route, lbctx *routing.LBContext) *routing.LBEndpoint {
lbctx.LBEndpoints = rt.LBEndpoints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do this either at the place where context is created or maybe better move context creation here.

Copy link
Member Author

@RomanZavodskikh RomanZavodskikh Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do this either at the place where context is

I like this idea, I've done that.

or maybe better move context creation here.

I like this way less, because I need to redeclare a lot variables from func mapRequest(... in func setRequestURLForLoadBalancedBackend(...

diff --git a/proxy/proxy.go b/proxy/proxy.go
index 80b41df3..dddf641f 100644
--- a/proxy/proxy.go
+++ b/proxy/proxy.go
@@ -468,7 +468,13 @@ func setRequestURLForDynamicBackend(u *url.URL, stateBag map[string]interface{})
        }
 }
 
-func setRequestURLForLoadBalancedBackend(u *url.URL, rt *routing.Route, lbctx *routing.LBContext) *routing.LBEndpoint {
+func setRequestURLForLoadBalancedBackend(ctx *context, registry *routing.EndpointRegistry) *routing.LBEndpoint {
+       r := ctx.request
+       rt := ctx.route
+       stateBag := ctx.StateBag()
+       u := r.URL
+       lbctx := &routing.LBContext{Request: r, Route: rt, LBEndpoints: rt.LBEndpoints, Params: stateBag, Registry: registry}
+
        e := rt.LBAlgorithm.Apply(lbctx)
        u.Scheme = e.Scheme
        u.Host = e.Host
@@ -490,7 +496,7 @@ func mapRequest(ctx *context, requestContext stdlibcontext.Context, removeHopHea
                setRequestURLFromRequest(u, r)
                setRequestURLForDynamicBackend(u, stateBag)
        case eskip.LBBackend:
-               endpoint = setRequestURLForLoadBalancedBackend(u, rt, &routing.LBContext{Request: r, Route: rt, LBEndpoints: rt.LBEndpoints, Params: stateBag, Registry: registry})
+               endpoint = setRequestURLForLoadBalancedBackend(ctx, registry)
        default:
                u.Scheme = rt.Scheme
                u.Host = rt.Host

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually better because it nicely contains all endpoint selection logic and creates a place to extend in the future.
Also

	u.Scheme = e.Scheme
	u.Host = e.Host

can be moved out of setRequestURLForLoadBalancedBackend as its not related to endpoint selection,
setRequestURLForLoadBalancedBackend could be renamed to selectLoadBalancedEndpoint (or something alike)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The routing.Route field of routing.LBContext struct will be
eventually removed because loadbalancer in general does not
require the full information about route, only about endpoints
to balance load between.

Signed-off-by: Roman Zavodskikh <[email protected]>
@AlexanderYastrebov
Copy link
Member

👍

1 similar comment
@RomanZavodskikh
Copy link
Member Author

👍

@RomanZavodskikh RomanZavodskikh merged commit eb4de17 into master Oct 5, 2023
11 checks passed
@RomanZavodskikh RomanZavodskikh deleted the addLBEndpointsToCtx branch October 5, 2023 12:16
AlexanderYastrebov added a commit that referenced this pull request Oct 5, 2023
Rename setRequestURLForLoadBalancedBackend to selectEndpoint and move loadbalancing context creation inside

Followup on #2648

Signed-off-by: Alexander Yastrebov <[email protected]>
AlexanderYastrebov added a commit that referenced this pull request Oct 5, 2023
Rename setRequestURLForLoadBalancedBackend to selectEndpoint and move loadbalancing context creation inside

Followup on #2648

Signed-off-by: Alexander Yastrebov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants