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

do not stop reconnect loop if factory returns error #91

Open
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (p *Pool) recreateConnection(closedConn *Connection) {
conn, err := p.Factory(closedConn.addr)
if err != nil {
p.handleError(fmt.Errorf("failed to re-create connection for %s: %w", closedConn.addr, err))
return
continue
}

// When connection is closed, remove it from the pool of connections and start
Expand Down
50 changes: 50 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ func TestPool(t *testing.T) {
}
}()

var (
shouldFactoryFail atomic.Bool
factoryCalled atomic.Int32
)

// And a factory method that will build connection for the pool
factory := func(addr string) (*connection.Connection, error) {
// increment factoryCalled counter
factoryCalled.Add(1)

if shouldFactoryFail.Load() {
return nil, fmt.Errorf("factory error")
}
// all our addresses have same configs, but if you need to use
// different TLS config, you can define config out of this
// function like:
Expand Down Expand Up @@ -151,6 +162,45 @@ func TestPool(t *testing.T) {
}, 2000*time.Millisecond, 50*time.Millisecond, "expect to have one less connection")
})

t.Run("connection factory returning error should not break the reconnect loop", func(t *testing.T) {
connectionsCntBeforeServerShutdown := len(pool.Connections())

// when we shutdown one of the servers
servers[0].Close()

// then we have one less connection
require.Eventually(t, func() bool {
return len(pool.Connections()) == connectionsCntBeforeServerShutdown-1
}, 500*time.Millisecond, 50*time.Millisecond, "expect to have one less connection")

// let factory return error
shouldFactoryFail.Store(true)

// reset factoryCalled counter
factoryCalled.Store(0)

// wait for the reconnect loop to call factory at least once
require.Eventually(t, func() bool {
return factoryCalled.Load() > 0
}, 1000*time.Millisecond, 50*time.Millisecond, "expect factory to be called at least once")

// stop returning error for factory
shouldFactoryFail.Store(false)

// when we start server again
server, err := NewTestServerWithAddr(servers[0].Addr)
require.NoError(t, err)

// so we will not forget to close server on exit
servers[0] = server

// then we have one more connection (the same as it was before
// we shut down the server)
require.Eventually(t, func() bool {
return len(pool.Connections()) == connectionsCntBeforeServerShutdown
}, 2000*time.Millisecond, 50*time.Millisecond, "expect to have one less connection")
})

t.Run("when MaxReconnectWait is set reconnect wait time will exponentially increase", func(t *testing.T) {
// Context: MaxReconnectWait is set to 400ms and initial
// ReconnectWait is 100ms. The server is offline, so the pool
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestServer_WithConnectionFactory(t *testing.T) {

require.Eventually(t, func() bool {
return isCalled.Load() == true
}, 100*time.Millisecond, 10*time.Millisecond)
}, 200*time.Millisecond, 10*time.Millisecond)

require.ErrorIs(t, gotErr, expectedErr)
})
Expand Down
Loading