Skip to content

Commit

Permalink
Upgrade EaseMesh to 2.0 (#696)
Browse files Browse the repository at this point in the history
* Upgrade EaseMesh to 2.0

* Add timeout in server pool
  • Loading branch information
xxx7xxxx committed Jul 21, 2022
1 parent 60d7230 commit d9ad9d6
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 210 deletions.
25 changes: 19 additions & 6 deletions pkg/filters/proxy/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,42 @@ import (
"github.com/megaease/easegress/pkg/protocols/httpprot"
)

const (
// LoadBalancePolicyRoundRobin is the load balance policy of round robin.
LoadBalancePolicyRoundRobin = "roundRobin"
// LoadBalancePolicyRandom is the load balance policy of random.
LoadBalancePolicyRandom = "random"
// LoadBalancePolicyWeightedRandom is the load balance policy of weighted random.
LoadBalancePolicyWeightedRandom = "weightedRandom"
// LoadBalancePolicyIPHash is the load balance policy of IP hash.
LoadBalancePolicyIPHash = "ipHash"
// LoadBalancePolicyHeaderHash is the load balance policy of HTTP header hash.
LoadBalancePolicyHeaderHash = "headerHash"
)

// LoadBalancer is the interface of an HTTP load balancer.
type LoadBalancer interface {
ChooseServer(req *httpprot.Request) *Server
}

// LoadBalanceSpec is the spec to create a load balancer.
type LoadBalanceSpec struct {
Policy string `yaml:"policy" jsonschema:"enum=roundRobin,enum=random,enum=weightedRandom,enum=ipHash,enum=headerHash"`
Policy string `yaml:"policy" jsonschema:"omitempty,enum=,enum=roundRobin,enum=random,enum=weightedRandom,enum=ipHash,enum=headerHash"`
HeaderHashKey string `yaml:"headerHashKey" jsonschema:"omitempty"`
}

// NewLoadBalancer creates a load balancer for servers according to spec.
func NewLoadBalancer(spec *LoadBalanceSpec, servers []*Server) LoadBalancer {
switch spec.Policy {
case "roundRobin", "":
case LoadBalancePolicyRoundRobin, "":
return newRoundRobinLoadBalancer(servers)
case "random":
case LoadBalancePolicyRandom:
return newRandomLoadBalancer(servers)
case "weightedRandom":
case LoadBalancePolicyWeightedRandom:
return newWeightedRandomLoadBalancer(servers)
case "ipHash":
case LoadBalancePolicyIPHash:
return newIPHashLoadBalancer(servers)
case "headerHash":
case LoadBalancePolicyHeaderHash:
return newHeaderHashLoadBalancer(servers, spec.HeaderHashKey)
default:
logger.Errorf("unsupported load balancing policy: %s", spec.Policy)
Expand Down
Loading

0 comments on commit d9ad9d6

Please sign in to comment.