Skip to content

Commit

Permalink
Support cookie hash (#1132)
Browse files Browse the repository at this point in the history
* Support cookie hash

* Use header Cookie instead of Set-Cookie
  • Loading branch information
xxx7xxxx authored Nov 9, 2023
1 parent 1c0e586 commit bb9e904
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
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 @@ -1564,7 +1564,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: "Cookie"}}
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

0 comments on commit bb9e904

Please sign in to comment.