Skip to content

Commit

Permalink
bugfix: logic of add/delete in round robin strategy
Browse files Browse the repository at this point in the history
* Duplicates could be added to a list of connections, which then
  cannot be deleted.
* The delete function uses an address from a connection instead of
  argument value. It may lead to unexpected errors.

Part of #208
  • Loading branch information
oleg-jukovec committed Aug 24, 2022
1 parent d7b0e43 commit 7645f40
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
16 changes: 11 additions & 5 deletions connection_pool/round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ func (r *RoundRobinStrategy) DeleteConnByAddr(addr string) *tarantool.Connection
r.conns = append(r.conns[:index], r.conns[index+1:]...)
r.size -= 1

for index, conn := range r.conns {
r.indexByAddr[conn.Addr()] = index
for k, v := range r.indexByAddr {
if v > index {
r.indexByAddr[k] = v - 1
}
}

return conn
Expand Down Expand Up @@ -94,9 +96,13 @@ func (r *RoundRobinStrategy) AddConn(addr string, conn *tarantool.Connection) {
r.mutex.Lock()
defer r.mutex.Unlock()

r.conns = append(r.conns, conn)
r.indexByAddr[addr] = r.size
r.size += 1
if idx, ok := r.indexByAddr[addr]; ok {
r.conns[idx] = conn
} else {
r.conns = append(r.conns, conn)
r.indexByAddr[addr] = r.size
r.size += 1
}
}

func (r *RoundRobinStrategy) nextIndex() int {
Expand Down
55 changes: 55 additions & 0 deletions connection_pool/round_robin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package connection_pool_test

import (
"testing"

"github.com/tarantool/go-tarantool"
. "github.com/tarantool/go-tarantool/connection_pool"
)

const (
validAddr1 = "x"
validAddr2 = "y"
)

func TestRoundRobinAddDelete(t *testing.T) {
rr := NewEmptyRoundRobin(10)

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

for i, addr := range addrs {
rr.AddConn(addr, conns[i])
}

for i, addr := range addrs {
if conn := rr.DeleteConnByAddr(addr); conn != conns[i] {
t.Errorf("Unexpected connection on address %s", addr)
}
}
if !rr.IsEmpty() {
t.Errorf("RoundRobin does not empty")
}
}

func TestRoundRobinAddDuplicateDelete(t *testing.T) {
rr := NewEmptyRoundRobin(10)

conn1 := &tarantool.Connection{}
conn2 := &tarantool.Connection{}

rr.AddConn(validAddr1, conn1)
rr.AddConn(validAddr1, conn2)

if rr.DeleteConnByAddr(validAddr1) != conn2 {
t.Errorf("Unexpected deleted connection")
}
if !rr.IsEmpty() {
t.Errorf("RoundRobin does not empty")
}
if rr.DeleteConnByAddr(validAddr1) != nil {
t.Errorf("Unexpected valued after second delition")
}
}


0 comments on commit 7645f40

Please sign in to comment.