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

Support cookie hash #1132

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/07.Reference/7.02.Filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ Rules to revise request header.

| Name | Type | Description | Required |
| ------------- | ------ | ----------------------------------------------------------------------------------------------------------- | -------- |
| policy | string | Load balance policy, valid values are `roundRobin`, `random`, `weightedRandom`, `ipHash`, `headerHash` and `forward`, the last one is only used in `GRPCProxy` | Yes |
| policy | string | Load balance policy, valid values are `roundRobin`, `random`, `weightedRandom`, `ipHash`, `headerHash`, `cookieHash` and `forward`, the last one is only used in `GRPCProxy` | Yes |
| headerHashKey | string | When `policy` is `headerHash`, this option is the name of a header whose value is used for hash calculation | No |
| stickySession | [proxy.StickySession](#proxyStickySessionSpec) | Sticky session spec | No |
| healthCheck | [proxy.HealthCheck](#proxyHealthCheckSpec) | Health check spec, note that healthCheck is not needed if you are using service registry | No |
Expand Down
14 changes: 8 additions & 6 deletions pkg/filters/proxies/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const (
LoadBalancePolicyIPHash = "ipHash"
// LoadBalancePolicyHeaderHash is the load balance policy of HTTP header hash.
LoadBalancePolicyHeaderHash = "headerHash"
// LoadBalancePolicyCookieHash is the load balance policy of HTTP cookie hash,
// which is the shorthand of headerHash with hash key Set-Cookie.
LoadBalancePolicyCookieHash = "cookieHash"
)

// LoadBalancer is the interface of a load balancer.
Expand Down Expand Up @@ -108,6 +111,8 @@ func (glb *GeneralLoadBalancer) Init(
lbp = &IPHashLoadBalancePolicy{}
case LoadBalancePolicyHeaderHash:
lbp = &HeaderHashLoadBalancePolicy{spec: glb.spec}
case LoadBalancePolicyCookieHash:
lbp = &HeaderHashLoadBalancePolicy{spec: &LoadBalanceSpec{HeaderHashKey: "Set-Cookie"}}
Copy link
Contributor

@suchen-sci suchen-sci Nov 8, 2023

Choose a reason for hiding this comment

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

Set-Cookie is in response header that server send to client. In this place, client sends requests to server. I think maybe Cookie header is more appropriate.

As show in https://docs.oracle.com/en-us/iaas/Content/Balance/Reference/sessionpersistence.htm#cook__HowItWorks

The Load Balancer service calculates a hash of the configured cookie and other request parameters, 
and sends that value to the client in a cookie. 
The value stored in the cookie enables the service to route subsequent client requests to the correct backend server. 
If your backend servers change any of the defined cookies, 
the service recomputes the cookie's value and resends it to the client.

My understand for this is that load balancer use Set-Cookie header to set some information to client. And in later requests, get this information from Cookie header and use this information to route to correct server?

And for cookie hash, do we support to route based on a specific cookie or route based on all cookies? When use header.Get(), it is always return first cookie...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

default:
logger.Errorf("unsupported load balancing policy: %s", glb.spec.Policy)
lbp = &RoundRobinLoadBalancePolicy{}
Expand Down Expand Up @@ -235,8 +240,7 @@ func (glb *GeneralLoadBalancer) Close() {
}

// RandomLoadBalancePolicy is a load balance policy that chooses a server randomly.
type RandomLoadBalancePolicy struct {
}
type RandomLoadBalancePolicy struct{}

// ChooseServer chooses a server randomly.
func (lbp *RandomLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server {
Expand All @@ -255,8 +259,7 @@ func (lbp *RoundRobinLoadBalancePolicy) ChooseServer(req protocols.Request, sg *
}

// WeightedRandomLoadBalancePolicy is a load balance policy that chooses a server randomly by weight.
type WeightedRandomLoadBalancePolicy struct {
}
type WeightedRandomLoadBalancePolicy struct{}

// ChooseServer chooses a server randomly by weight.
func (lbp *WeightedRandomLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server {
Expand All @@ -272,8 +275,7 @@ func (lbp *WeightedRandomLoadBalancePolicy) ChooseServer(req protocols.Request,
}

// IPHashLoadBalancePolicy is a load balance policy that chooses a server by ip hash.
type IPHashLoadBalancePolicy struct {
}
type IPHashLoadBalancePolicy struct{}

// ChooseServer chooses a server by ip hash.
func (lbp *IPHashLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server {
Expand Down