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

Altered auto-reconnection attempt logic to comply with part 4 of the OCPP2.0.1 standard #221

Merged
Merged
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
17 changes: 10 additions & 7 deletions ws/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func (s *NetworkTestSuite) TestClientConnectionFailedTimeout() {
func (s *NetworkTestSuite) TestClientAutoReconnect() {
t := s.T()
// Set timeouts for test
s.client.timeoutConfig.ReconnectBackoff = 1 * time.Second
s.client.timeoutConfig.RetryBackOffWaitMinimum = 1 * time.Second
s.client.timeoutConfig.RetryBackOffRandomRange = 1 // seconds
// Setup
serverOnDisconnected := make(chan bool, 1)
clientOnDisconnected := make(chan bool, 1)
Expand All @@ -147,7 +148,7 @@ func (s *NetworkTestSuite) TestClientAutoReconnect() {
clientOnDisconnected <- true
})
s.client.SetReconnectedHandler(func() {
time.Sleep(50 * time.Millisecond) // Make sure we reconnected after backoff
time.Sleep(time.Duration(s.client.timeoutConfig.RetryBackOffRandomRange)*time.Second + 50*time.Millisecond) // Make sure we reconnected after backoff
reconnected <- true
})
// Connect client
Expand All @@ -172,7 +173,7 @@ func (s *NetworkTestSuite) TestClientAutoReconnect() {
elapsed := time.Since(start)
assert.True(t, result)
assert.True(t, s.client.IsConnected())
assert.GreaterOrEqual(t, elapsed.Milliseconds(), s.client.timeoutConfig.ReconnectBackoff.Milliseconds())
assert.GreaterOrEqual(t, elapsed.Milliseconds(), s.client.timeoutConfig.RetryBackOffWaitMinimum.Milliseconds())
// Cleanup
s.client.SetDisconnectedHandler(func(err error) {
assert.Nil(t, err)
Expand All @@ -191,7 +192,8 @@ func (s *NetworkTestSuite) TestClientPongTimeout() {
// Server will close connection
s.client.timeoutConfig.PongWait = 2 * time.Second
s.client.timeoutConfig.PingPeriod = (s.client.timeoutConfig.PongWait * 5) / 10
s.client.timeoutConfig.ReconnectBackoff = 1 * time.Second
s.client.timeoutConfig.RetryBackOffWaitMinimum = 1 * time.Second
s.client.timeoutConfig.RetryBackOffWaitMinimum = 0 // remove randomness
s.server.timeoutConfig.PingWait = (s.client.timeoutConfig.PongWait * 7) / 10
// Setup
serverOnDisconnected := make(chan bool, 1)
Expand Down Expand Up @@ -246,7 +248,7 @@ func (s *NetworkTestSuite) TestClientPongTimeout() {
result = <-reconnected
require.True(t, result)
elapsed := time.Since(startTimeout)
assert.GreaterOrEqual(t, elapsed.Milliseconds(), s.client.timeoutConfig.ReconnectBackoff.Milliseconds())
assert.GreaterOrEqual(t, elapsed.Milliseconds(), s.client.timeoutConfig.RetryBackOffWaitMinimum.Milliseconds())
// Cleanup
s.client.SetDisconnectedHandler(nil)
s.client.Stop()
Expand All @@ -258,7 +260,8 @@ func (s *NetworkTestSuite) TestClientReadTimeout() {
// Set timeouts for test
s.client.timeoutConfig.PongWait = 2 * time.Second
s.client.timeoutConfig.PingPeriod = (s.client.timeoutConfig.PongWait * 7) / 10
s.client.timeoutConfig.ReconnectBackoff = 1 * time.Second
s.client.timeoutConfig.RetryBackOffWaitMinimum = 1 * time.Second
s.client.timeoutConfig.RetryBackOffRandomRange = 0 // remove randomness
s.server.timeoutConfig.PingWait = s.client.timeoutConfig.PongWait
// Setup
serverOnDisconnected := make(chan bool, 1)
Expand Down Expand Up @@ -317,7 +320,7 @@ func (s *NetworkTestSuite) TestClientReadTimeout() {
result = <-reconnected
require.True(t, result)
elapsed := time.Since(startTimeout)
assert.GreaterOrEqual(t, elapsed.Milliseconds(), s.client.timeoutConfig.ReconnectBackoff.Milliseconds())
assert.GreaterOrEqual(t, elapsed.Milliseconds(), s.client.timeoutConfig.RetryBackOffWaitMinimum.Milliseconds())
// Cleanup
s.client.SetDisconnectedHandler(nil)
s.client.Stop()
Expand Down
57 changes: 36 additions & 21 deletions ws/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/base64"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
Expand All @@ -33,10 +34,19 @@ const (
defaultPingPeriod = (defaultPongWait * 9) / 10
// Time allowed for the initial handshake to complete.
defaultHandshakeTimeout = 30 * time.Second
// The base delay to be used for automatic reconnection. Will double for every attempt up to maxReconnectionDelay.
defaultReconnectBackoff = 5 * time.Second
// Default maximum reconnection delay for websockets
defaultReconnectMaxBackoff = 2 * time.Minute
// When the Charging Station is reconnecting, after a connection loss, it will use this variable for the amount of time
// it will double the previous back-off time. When the maximum number of increments is reached, the Charging
// Station keeps connecting with the same back-off time.
defaultRetryBackOffRepeatTimes = 5
// When the Charging Station is reconnecting, after a connection loss, it will use this variable as the maximum value
// for the random part of the back-off time. It will add a new random value to every increasing back-off time,
// including the first connection attempt (with this maximum), for the amount of times it will double the previous
// back-off time. When the maximum number of increments is reached, the Charging Station will keep connecting
// with the same back-off time.
defaultRetryBackOffRandomRange = 15 // seconds
// When the Charging Station is reconnecting, after a connection loss, it will use this variable as the minimum backoff
// time, the first time it tries to reconnect.
defaultRetryBackOffWaitMinimum = 10 * time.Second
)

// The internal verbose logger
Expand Down Expand Up @@ -76,25 +86,27 @@ func NewServerTimeoutConfig() ServerTimeoutConfig {
// To set a custom configuration, refer to the client's SetTimeoutConfig method.
// If no configuration is passed, a default configuration is generated via the NewClientTimeoutConfig function.
type ClientTimeoutConfig struct {
WriteWait time.Duration
HandshakeTimeout time.Duration
PongWait time.Duration
PingPeriod time.Duration
ReconnectBackoff time.Duration
ReconnectMaxBackoff time.Duration
WriteWait time.Duration
HandshakeTimeout time.Duration
PongWait time.Duration
PingPeriod time.Duration
RetryBackOffRepeatTimes int
RetryBackOffRandomRange int
RetryBackOffWaitMinimum time.Duration
}

// NewClientTimeoutConfig creates a default timeout configuration for a websocket endpoint.
//
// You may change fields arbitrarily and pass the struct to a SetTimeoutConfig method.
func NewClientTimeoutConfig() ClientTimeoutConfig {
return ClientTimeoutConfig{
WriteWait: defaultWriteWait,
HandshakeTimeout: defaultHandshakeTimeout,
PongWait: defaultPongWait,
PingPeriod: defaultPingPeriod,
ReconnectBackoff: defaultReconnectBackoff,
ReconnectMaxBackoff: defaultReconnectMaxBackoff,
WriteWait: defaultWriteWait,
HandshakeTimeout: defaultHandshakeTimeout,
PongWait: defaultPongWait,
PingPeriod: defaultPingPeriod,
RetryBackOffRepeatTimes: defaultRetryBackOffRepeatTimes,
RetryBackOffRandomRange: defaultRetryBackOffRandomRange,
RetryBackOffWaitMinimum: defaultRetryBackOffWaitMinimum,
}
}

Expand Down Expand Up @@ -973,7 +985,8 @@ func (client *Client) cleanup() {

func (client *Client) handleReconnection() {
log.Info("started automatic reconnection handler")
delay := client.timeoutConfig.ReconnectBackoff
delay := client.timeoutConfig.RetryBackOffWaitMinimum + time.Duration(rand.Intn(client.timeoutConfig.RetryBackOffRandomRange+1))*time.Second
reconnectionAttempts := 1
for {
// Wait before reconnecting
select {
Expand All @@ -992,11 +1005,13 @@ func (client *Client) handleReconnection() {
return
}
client.error(fmt.Errorf("reconnection failed: %w", err))
// Re-connection failed, double the delay
delay *= 2
if delay >= client.timeoutConfig.ReconnectMaxBackoff {
delay = client.timeoutConfig.ReconnectMaxBackoff

if reconnectionAttempts < client.timeoutConfig.RetryBackOffRepeatTimes {
// Re-connection failed, double the delay
delay *= 2
delay += time.Duration(rand.Intn(client.timeoutConfig.RetryBackOffRandomRange+1)) * time.Second
}
reconnectionAttempts += 1
}
}

Expand Down