-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Multi-Proxy and Load Balancer Strategy Support
This commit introduces significant updates to enhance the handling of proxies within the server configuration and adds support for load balancing strategies. Changes: - Updated API tests to reflect changes from a single `Proxy` to a list of `Proxies`. - Adjusted initialization and configuration of proxies in `run.go` to support multiple proxies and load balancer strategies. - Updated configuration files to include new fields for multiple proxies and load balancer strategies. - Enhanced global configuration validation for clients, pools, and proxies. - Added new `loadBalancer` section in `gatewayd.yaml` for rules and strategies. - Implemented load balancing strategy selection and Round Robin strategy. - Added tests for load balancer strategies. - Added new error type `ErrorCodeLoadBalancerStrategyNotFound`. - Improved proxy connection handling and added informative comments. Configuration Example: - Updated `gatewayd.yaml` to reflect new support for multiple proxies and load balancer strategies. - Ensure to update your configuration files accordingly. Testing: - Updated existing tests and added new tests for multi-proxy and load balancing functionality. - Verified configuration validation for proxies and load balancers. Impact: - Improved flexibility and scalability of server configuration. - Enabled robust proxy management and efficient load distribution.
- Loading branch information
1 parent
18ae198
commit 391ee7d
Showing
16 changed files
with
395 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package network | ||
|
||
// Load balancing strategies. | ||
const ( | ||
RoundRobinStrategy = "ROUND_ROBIN" | ||
) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package network | ||
|
||
import ( | ||
gerr "github.com/gatewayd-io/gatewayd/errors" | ||
) | ||
|
||
type LoadBalancerStrategy interface { | ||
GetNextProxy() IProxy | ||
} | ||
|
||
func NewLoadBalancerStrategy(server *Server) (LoadBalancerStrategy, *gerr.GatewayDError) { | ||
switch server.LoadbalancerStrategyName { | ||
case RoundRobinStrategy: | ||
return NewRoundRobin(server), nil | ||
default: | ||
return nil, gerr.ErrLoadBalancerStrategyNotFound | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package network | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
gerr "github.com/gatewayd-io/gatewayd/errors" | ||
) | ||
|
||
func TestNewLoadBalancerStrategy(t *testing.T) { | ||
serverValid := &Server{ | ||
LoadbalancerStrategyName: RoundRobinStrategy, | ||
Proxies: []IProxy{MockProxy{}}, | ||
} | ||
|
||
// Test case 1: Valid strategy name | ||
strategy, err := NewLoadBalancerStrategy(serverValid) | ||
if err != nil { | ||
t.Errorf("Expected no error, got %v", err) | ||
} | ||
|
||
_, ok := strategy.(*RoundRobin) | ||
if !ok { | ||
t.Errorf("Expected strategy to be of type RoundRobin") | ||
} | ||
|
||
// Test case 2: InValid strategy name | ||
serverInvalid := &Server{ | ||
LoadbalancerStrategyName: "InvalidStrategy", | ||
Proxies: []IProxy{MockProxy{}}, | ||
} | ||
|
||
strategy, err = NewLoadBalancerStrategy(serverInvalid) | ||
if !errors.Is(err, gerr.ErrLoadBalancerStrategyNotFound) { | ||
t.Errorf("Expected ErrLoadBalancerStrategyNotFound, got %v", err) | ||
} | ||
if strategy != nil { | ||
t.Errorf("Expected strategy to be nil for invalid strategy name") | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package network | ||
|
||
import "sync/atomic" | ||
|
||
type RoundRobin struct { | ||
proxies []IProxy | ||
next atomic.Uint32 | ||
} | ||
|
||
func NewRoundRobin(server *Server) *RoundRobin { | ||
return &RoundRobin{proxies: server.Proxies} | ||
} | ||
|
||
func (r *RoundRobin) GetNextProxy() IProxy { | ||
proxiesLen := uint32(len(r.proxies)) | ||
|
||
nextIndex := r.next.Add(1) | ||
|
||
return r.proxies[nextIndex%proxiesLen] | ||
} |
Oops, something went wrong.