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

Feature: enable multi pool client connections #577

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type API struct {
Options *Options
Config *config.Config
PluginRegistry *plugin.Registry
Pools map[string]*pool.Pool
Proxies map[string]*network.Proxy
Pools map[string]map[string]*pool.Pool
Proxies map[string]map[string]*network.Proxy
Servers map[string]*network.Server
}

Expand Down Expand Up @@ -201,16 +201,21 @@ func (a *API) GetPlugins(context.Context, *emptypb.Empty) (*v1.PluginConfigs, er
}

// GetPools returns the pool configuration of the GatewayD.
func (a *API) GetPools(context.Context, *emptypb.Empty) (*structpb.Struct, error) {
_, span := otel.Tracer(config.TracerName).Start(a.ctx, "Get Pools")
func (a *API) GetPools(ctx context.Context, _ *emptypb.Empty) (*structpb.Struct, error) {
_, span := otel.Tracer(config.TracerName).Start(ctx, "Get Pools")
sinadarbouy marked this conversation as resolved.
Show resolved Hide resolved
defer span.End()

pools := make(map[string]interface{})
for name, p := range a.Pools {
pools[name] = map[string]interface{}{
"cap": p.Cap(),
"size": p.Size(),
pools := make(map[string]any)
Copy link
Member

Choose a reason for hiding this comment

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

Note to self: plugins should be updated as well, as they rely on this API.


for configGroupName, configGroupPools := range a.Pools {
groupPools := make(map[string]any)
for name, p := range configGroupPools {
groupPools[name] = map[string]any{
"cap": p.Cap(),
"size": p.Size(),
}
}
pools[configGroupName] = groupPools
}

poolsConfig, err := structpb.NewStruct(pools)
Expand All @@ -231,23 +236,31 @@ func (a *API) GetProxies(context.Context, *emptypb.Empty) (*structpb.Struct, err
_, span := otel.Tracer(config.TracerName).Start(a.ctx, "Get Proxies")
defer span.End()

proxies := make(map[string]interface{})
for name, proxy := range a.Proxies {
available := make([]interface{}, 0)
for _, c := range proxy.AvailableConnectionsString() {
available = append(available, c)
}
// Create a new map to hold the flattened proxies data
proxies := make(map[string]any)

busy := make([]interface{}, 0)
for _, conn := range proxy.BusyConnectionsString() {
busy = append(busy, conn)
}
for configGroupName, configGroupProxies := range a.Proxies {
// Create a map for each configuration group
groupProxies := make(map[string]any)
for name, proxy := range configGroupProxies {
available := make([]any, 0)
for _, c := range proxy.AvailableConnectionsString() {
available = append(available, c)
}

proxies[name] = map[string]interface{}{
"available": available,
"busy": busy,
"total": len(available) + len(busy),
busy := make([]any, 0)
for _, conn := range proxy.BusyConnectionsString() {
busy = append(busy, conn)
}

groupProxies[name] = map[string]any{
"available": available,
"busy": busy,
"total": len(available) + len(busy),
}
}

proxies[configGroupName] = groupProxies
}

proxiesConfig, err := structpb.NewStruct(proxies)
Expand Down
10 changes: 5 additions & 5 deletions api/api_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getAPIConfig() *API {
context.Background(),
network.Server{
Logger: logger,
Proxy: defaultProxy,
Proxies: []network.IProxy{defaultProxy},
PluginRegistry: pluginReg,
PluginTimeout: config.DefaultPluginTimeout,
Network: "tcp",
Expand All @@ -73,11 +73,11 @@ func getAPIConfig() *API {
},
),
PluginRegistry: pluginReg,
Pools: map[string]*pool.Pool{
config.Default: defaultPool,
Pools: map[string]map[string]*pool.Pool{
config.Default: {config.DefaultConfigurationBlock: defaultPool},
},
Proxies: map[string]*network.Proxy{
config.Default: defaultProxy,
Proxies: map[string]map[string]*network.Proxy{
config.Default: {config.DefaultConfigurationBlock: defaultProxy},
},
Servers: servers,
}
Expand Down
39 changes: 24 additions & 15 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,26 @@ func TestGetPluginsWithEmptyPluginRegistry(t *testing.T) {

func TestPools(t *testing.T) {
api := API{
Pools: map[string]*pool.Pool{
config.Default: pool.NewPool(context.TODO(), config.EmptyPoolCapacity),
Pools: map[string]map[string]*pool.Pool{
config.Default: {config.DefaultConfigurationBlock: pool.NewPool(context.TODO(), config.EmptyPoolCapacity)},
},
ctx: context.Background(),
}
pools, err := api.GetPools(context.Background(), &emptypb.Empty{})
require.NoError(t, err)
assert.NotEmpty(t, pools)
assert.NotEmpty(t, pools.AsMap())
assert.Equal(t, pools.AsMap()[config.Default], map[string]interface{}{"cap": 0.0, "size": 0.0})

assert.Equal(t,
map[string]interface{}{
mostafa marked this conversation as resolved.
Show resolved Hide resolved
config.DefaultConfigurationBlock: map[string]interface{}{"cap": 0.0, "size": 0.0},
},
pools.AsMap()[config.Default])
}

func TestPoolsWithEmptyPools(t *testing.T) {
api := API{
Pools: map[string]*pool.Pool{},
Pools: map[string]map[string]*pool.Pool{},
ctx: context.Background(),
}
pools, err := api.GetPools(context.Background(), &emptypb.Empty{})
Expand Down Expand Up @@ -258,8 +263,8 @@ func TestGetProxies(t *testing.T) {
)

api := API{
Proxies: map[string]*network.Proxy{
config.Default: proxy,
Proxies: map[string]map[string]*network.Proxy{
config.Default: {config.DefaultConfigurationBlock: proxy},
},
ctx: context.Background(),
}
Expand All @@ -268,10 +273,14 @@ func TestGetProxies(t *testing.T) {
assert.NotEmpty(t, proxies)
assert.NotEmpty(t, proxies.AsMap())

if defaultProxy, ok := proxies.AsMap()[config.Default].(map[string]interface{}); ok {
assert.Equal(t, 1.0, defaultProxy["total"])
assert.NotEmpty(t, defaultProxy["available"])
assert.Empty(t, defaultProxy["busy"])
if defaultProxies, ok := proxies.AsMap()[config.Default].(map[string]interface{}); ok {
if defaultProxy, ok := defaultProxies[config.DefaultConfigurationBlock].(map[string]interface{}); ok {
assert.Equal(t, 1.0, defaultProxy["total"])
assert.NotEmpty(t, defaultProxy["available"])
assert.Empty(t, defaultProxy["busy"])
} else {
t.Errorf("proxies.default.%s is not found or not a map", config.DefaultConfigurationBlock)
}
} else {
t.Errorf("proxies.default is not found or not a map")
}
Expand Down Expand Up @@ -333,7 +342,7 @@ func TestGetServers(t *testing.T) {
Options: network.Option{
EnableTicker: false,
},
Proxy: proxy,
Proxies: []network.IProxy{proxy},
Logger: zerolog.Logger{},
PluginRegistry: pluginRegistry,
PluginTimeout: config.DefaultPluginTimeout,
Expand All @@ -342,11 +351,11 @@ func TestGetServers(t *testing.T) {
)

api := API{
Pools: map[string]*pool.Pool{
config.Default: newPool,
Pools: map[string]map[string]*pool.Pool{
config.Default: {config.DefaultConfigurationBlock: newPool},
},
Proxies: map[string]*network.Proxy{
config.Default: proxy,
Proxies: map[string]map[string]*network.Proxy{
config.Default: {config.DefaultConfigurationBlock: proxy},
},
Servers: map[string]*network.Server{
config.Default: server,
Expand Down
2 changes: 1 addition & 1 deletion api/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Test_Healthchecker(t *testing.T) {
Options: network.Option{
EnableTicker: false,
},
Proxy: proxy,
Proxies: []network.IProxy{proxy},
Logger: zerolog.Logger{},
PluginRegistry: pluginRegistry,
PluginTimeout: config.DefaultPluginTimeout,
Expand Down
Loading
Loading