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 #5

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
60 changes: 37 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 @@ -205,12 +205,17 @@ func (a *API) GetPools(context.Context, *emptypb.Empty) (*structpb.Struct, error
_, span := otel.Tracer(config.TracerName).Start(a.ctx, "Get Pools")
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)

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 All @@ -268,13 +281,14 @@ func (a *API) GetServers(context.Context, *emptypb.Empty) (*structpb.Struct, err
_, span := otel.Tracer(config.TracerName).Start(a.ctx, "Get Servers")
defer span.End()

servers := make(map[string]interface{})
servers := make(map[string]any)
for name, server := range a.Servers {
servers[name] = map[string]interface{}{
servers[name] = map[string]any{
"network": server.Network,
"address": server.Address,
"status": uint(server.Status),
"tickInterval": server.TickInterval.Nanoseconds(),
"loadBalancer": map[string]any{"strategy": server.LoadbalancerStrategyName},
}
}

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
64 changes: 39 additions & 25 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]any{
config.DefaultConfigurationBlock: map[string]any{"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]any); ok {
if defaultProxy, ok := defaultProxies[config.DefaultConfigurationBlock].(map[string]any); 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,20 +342,21 @@ func TestGetServers(t *testing.T) {
Options: network.Option{
EnableTicker: false,
},
Proxy: proxy,
Logger: zerolog.Logger{},
PluginRegistry: pluginRegistry,
PluginTimeout: config.DefaultPluginTimeout,
HandshakeTimeout: config.DefaultHandshakeTimeout,
Proxies: []network.IProxy{proxy},
Logger: zerolog.Logger{},
PluginRegistry: pluginRegistry,
PluginTimeout: config.DefaultPluginTimeout,
HandshakeTimeout: config.DefaultHandshakeTimeout,
LoadbalancerStrategyName: config.DefaultLoadBalancerStrategy,
},
)

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 All @@ -361,12 +371,16 @@ func TestGetServers(t *testing.T) {
if defaultServer, ok := servers.AsMap()[config.Default].(map[string]interface{}); ok {
assert.Equal(t, config.DefaultNetwork, defaultServer["network"])
assert.Equal(t, config.DefaultAddress, "localhost:5432")
status, ok := defaultServer["status"].(float64)
assert.True(t, ok)
assert.Equal(t, config.Stopped, config.Status(status))
tickInterval, ok := defaultServer["tickInterval"].(float64)
assert.True(t, ok)
assert.Equal(t, config.DefaultTickInterval.Nanoseconds(), int64(tickInterval))
statusFloat, isStatusFloat := defaultServer["status"].(float64)
assert.True(t, isStatusFloat, "status should be of type float64")
status := config.Status(statusFloat)
assert.Equal(t, config.Stopped, status)
tickIntervalFloat, isTickIntervalFloat := defaultServer["tickInterval"].(float64)
assert.True(t, isTickIntervalFloat, "tickInterval should be of type float64")
assert.Equal(t, config.DefaultTickInterval.Nanoseconds(), int64(tickIntervalFloat))
loadBalancerMap, isLoadBalancerMap := defaultServer["loadBalancer"].(map[string]interface{})
assert.True(t, isLoadBalancerMap, "loadBalancer should be a map")
assert.Equal(t, config.DefaultLoadBalancerStrategy, loadBalancerMap["strategy"])
} else {
t.Errorf("servers.default is not found or not a map")
}
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