Skip to content

Commit 993f644

Browse files
author
Maksim Konovalov
committed
feature/balancing-faces: add external load balancing methods support
I added the ability to use custom balancing methods when connecting with a pool.
1 parent 8b2be01 commit 993f644

File tree

5 files changed

+61
-26
lines changed

5 files changed

+61
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1414
also added logs for error case of `ConnectionPool.tryConnect()` calls in
1515
`ConnectionPool.controller()` and `ConnectionPool.reconnect()`
1616
- Methods that are implemented but not included in the pooler interface (#395).
17+
- Add support for external load balancing methods when connecting via Pool (#400).
1718

1819
### Changed
1920

pool/balancer.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pool
2+
3+
import "github.com/tarantool/go-tarantool/v2"
4+
5+
type BalancerFactory interface {
6+
Create(size int) BalancingPool
7+
}
8+
9+
type BalancingPool interface {
10+
GetConnection(string) *tarantool.Connection
11+
DeleteConnection(string) *tarantool.Connection
12+
AddConnection(id string, conn *tarantool.Connection)
13+
GetNextConnection() *tarantool.Connection
14+
GetConnections() map[string]*tarantool.Connection
15+
}
16+
17+
func IsEmpty(pool BalancingPool) bool {
18+
return len(pool.GetConnections()) == 0
19+
}

pool/connection_pool.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ type Opts struct {
8181
CheckTimeout time.Duration
8282
// ConnectionHandler provides an ability to handle connection updates.
8383
ConnectionHandler ConnectionHandler
84+
// BalancerFactory - a factory for creating balancing,
85+
// contains the pool size as well as the connections for which it is used.
86+
BalancerFactory BalancerFactory
8487
}
8588

8689
/*
@@ -110,9 +113,9 @@ type ConnectionPool struct {
110113

111114
state state
112115
done chan struct{}
113-
roPool *roundRobinStrategy
114-
rwPool *roundRobinStrategy
115-
anyPool *roundRobinStrategy
116+
roPool BalancingPool
117+
rwPool BalancingPool
118+
anyPool BalancingPool
116119
poolsMutex sync.RWMutex
117120
watcherContainer watcherContainer
118121
}
@@ -153,6 +156,10 @@ func newEndpoint(name string, dialer tarantool.Dialer, opts tarantool.Opts) *end
153156
// opts. Instances must have unique names.
154157
func ConnectWithOpts(ctx context.Context, instances []Instance,
155158
opts Opts) (*ConnectionPool, error) {
159+
if opts.BalancerFactory == nil {
160+
opts.BalancerFactory = &RoundRobinFactory{}
161+
}
162+
156163
unique := make(map[string]bool)
157164
for _, instance := range instances {
158165
if _, ok := unique[instance.Name]; ok {
@@ -166,9 +173,9 @@ func ConnectWithOpts(ctx context.Context, instances []Instance,
166173
}
167174

168175
size := len(instances)
169-
rwPool := newRoundRobinStrategy(size)
170-
roPool := newRoundRobinStrategy(size)
171-
anyPool := newRoundRobinStrategy(size)
176+
rwPool := opts.BalancerFactory.Create(size)
177+
roPool := opts.BalancerFactory.Create(size)
178+
anyPool := opts.BalancerFactory.Create(size)
172179

173180
connPool := &ConnectionPool{
174181
ends: make(map[string]*endpoint),
@@ -218,15 +225,15 @@ func (p *ConnectionPool) ConnectedNow(mode Mode) (bool, error) {
218225
}
219226
switch mode {
220227
case ANY:
221-
return !p.anyPool.IsEmpty(), nil
228+
return !IsEmpty(p.anyPool), nil
222229
case RW:
223-
return !p.rwPool.IsEmpty(), nil
230+
return !IsEmpty(p.rwPool), nil
224231
case RO:
225-
return !p.roPool.IsEmpty(), nil
232+
return !IsEmpty(p.roPool), nil
226233
case PreferRW:
227234
fallthrough
228235
case PreferRO:
229-
return !p.rwPool.IsEmpty() || !p.roPool.IsEmpty(), nil
236+
return !IsEmpty(p.rwPool) || !IsEmpty(p.roPool), nil
230237
default:
231238
return false, ErrNoHealthyInstance
232239
}

pool/round_robin.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@ import (
77
"github.com/tarantool/go-tarantool/v2"
88
)
99

10-
type roundRobinStrategy struct {
10+
var _ BalancingPool = (*RoundRobinStrategy)(nil)
11+
12+
type RoundRobinStrategy struct {
1113
conns []*tarantool.Connection
1214
indexById map[string]uint
1315
mutex sync.RWMutex
1416
size uint64
1517
current uint64
1618
}
1719

18-
func newRoundRobinStrategy(size int) *roundRobinStrategy {
19-
return &roundRobinStrategy{
20+
type RoundRobinFactory struct{}
21+
22+
func (r *RoundRobinFactory) Create(size int) BalancingPool {
23+
return NewRoundRobinStrategy(size)
24+
}
25+
26+
func NewRoundRobinStrategy(size int) BalancingPool {
27+
return &RoundRobinStrategy{
2028
conns: make([]*tarantool.Connection, 0, size),
2129
indexById: make(map[string]uint, size),
2230
size: 0,
2331
current: 0,
2432
}
2533
}
2634

27-
func (r *roundRobinStrategy) GetConnection(id string) *tarantool.Connection {
35+
func (r *RoundRobinStrategy) GetConnection(id string) *tarantool.Connection {
2836
r.mutex.RLock()
2937
defer r.mutex.RUnlock()
3038

@@ -36,7 +44,7 @@ func (r *roundRobinStrategy) GetConnection(id string) *tarantool.Connection {
3644
return r.conns[index]
3745
}
3846

39-
func (r *roundRobinStrategy) DeleteConnection(id string) *tarantool.Connection {
47+
func (r *RoundRobinStrategy) DeleteConnection(id string) *tarantool.Connection {
4048
r.mutex.Lock()
4149
defer r.mutex.Unlock()
4250

@@ -64,14 +72,14 @@ func (r *roundRobinStrategy) DeleteConnection(id string) *tarantool.Connection {
6472
return conn
6573
}
6674

67-
func (r *roundRobinStrategy) IsEmpty() bool {
75+
func (r *RoundRobinStrategy) IsEmpty() bool {
6876
r.mutex.RLock()
6977
defer r.mutex.RUnlock()
7078

7179
return r.size == 0
7280
}
7381

74-
func (r *roundRobinStrategy) GetNextConnection() *tarantool.Connection {
82+
func (r *RoundRobinStrategy) GetNextConnection() *tarantool.Connection {
7583
r.mutex.RLock()
7684
defer r.mutex.RUnlock()
7785

@@ -81,7 +89,7 @@ func (r *roundRobinStrategy) GetNextConnection() *tarantool.Connection {
8189
return r.conns[r.nextIndex()]
8290
}
8391

84-
func (r *roundRobinStrategy) GetConnections() map[string]*tarantool.Connection {
92+
func (r *RoundRobinStrategy) GetConnections() map[string]*tarantool.Connection {
8593
r.mutex.RLock()
8694
defer r.mutex.RUnlock()
8795

@@ -93,7 +101,7 @@ func (r *roundRobinStrategy) GetConnections() map[string]*tarantool.Connection {
93101
return conns
94102
}
95103

96-
func (r *roundRobinStrategy) AddConnection(id string, conn *tarantool.Connection) {
104+
func (r *RoundRobinStrategy) AddConnection(id string, conn *tarantool.Connection) {
97105
r.mutex.Lock()
98106
defer r.mutex.Unlock()
99107

@@ -106,7 +114,7 @@ func (r *roundRobinStrategy) AddConnection(id string, conn *tarantool.Connection
106114
}
107115
}
108116

109-
func (r *roundRobinStrategy) nextIndex() uint64 {
117+
func (r *RoundRobinStrategy) nextIndex() uint64 {
110118
next := atomic.AddUint64(&r.current, 1)
111119
return (next - 1) % r.size
112120
}

pool/round_robin_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const (
1212
)
1313

1414
func TestRoundRobinAddDelete(t *testing.T) {
15-
rr := newRoundRobinStrategy(10)
15+
rr := NewRoundRobinStrategy(10)
1616

1717
addrs := []string{validAddr1, validAddr2}
1818
conns := []*tarantool.Connection{&tarantool.Connection{}, &tarantool.Connection{}}
@@ -26,13 +26,13 @@ func TestRoundRobinAddDelete(t *testing.T) {
2626
t.Errorf("Unexpected connection on address %s", addr)
2727
}
2828
}
29-
if !rr.IsEmpty() {
29+
if !IsEmpty(rr) {
3030
t.Errorf("RoundRobin does not empty")
3131
}
3232
}
3333

3434
func TestRoundRobinAddDuplicateDelete(t *testing.T) {
35-
rr := newRoundRobinStrategy(10)
35+
rr := NewRoundRobinStrategy(10)
3636

3737
conn1 := &tarantool.Connection{}
3838
conn2 := &tarantool.Connection{}
@@ -43,7 +43,7 @@ func TestRoundRobinAddDuplicateDelete(t *testing.T) {
4343
if rr.DeleteConnection(validAddr1) != conn2 {
4444
t.Errorf("Unexpected deleted connection")
4545
}
46-
if !rr.IsEmpty() {
46+
if !IsEmpty(rr) {
4747
t.Errorf("RoundRobin does not empty")
4848
}
4949
if rr.DeleteConnection(validAddr1) != nil {
@@ -52,7 +52,7 @@ func TestRoundRobinAddDuplicateDelete(t *testing.T) {
5252
}
5353

5454
func TestRoundRobinGetNextConnection(t *testing.T) {
55-
rr := newRoundRobinStrategy(10)
55+
rr := NewRoundRobinStrategy(10)
5656

5757
addrs := []string{validAddr1, validAddr2}
5858
conns := []*tarantool.Connection{&tarantool.Connection{}, &tarantool.Connection{}}
@@ -70,7 +70,7 @@ func TestRoundRobinGetNextConnection(t *testing.T) {
7070
}
7171

7272
func TestRoundRobinStrategy_GetConnections(t *testing.T) {
73-
rr := newRoundRobinStrategy(10)
73+
rr := NewRoundRobinStrategy(10)
7474

7575
addrs := []string{validAddr1, validAddr2}
7676
conns := []*tarantool.Connection{&tarantool.Connection{}, &tarantool.Connection{}}

0 commit comments

Comments
 (0)