Skip to content

Commit

Permalink
Do not send echo when inactivity check in progress
Browse files Browse the repository at this point in the history
This commit skips spawning another goroutine to send echo when another
echo check is already in progress. It also throws error if inactivity
timeout value is lesser than reconnect timeout value while initializing
client with WithInactivityCheck option.

Signed-off-by: Periyasamy Palanisamy <[email protected]>
  • Loading branch information
pperiyasamy committed Sep 26, 2023
1 parent 49be92a commit 68f6493
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"reflect"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -1202,6 +1203,7 @@ func (o *ovsdbClient) handleInactivityProbes() {
stopCh := o.stopCh
trafficSeen := o.trafficSeen
timer := time.NewTimer(o.options.inactivityTimeout)
var echoInProgress atomic.Bool

Check failure on line 1206 in client/client.go

View workflow job for this annotation

GitHub Actions / Build & Unit Test

Bool not declared by package atomic (typecheck)
for {
select {
case <-stopCh:
Expand All @@ -1212,15 +1214,22 @@ func (o *ovsdbClient) handleInactivityProbes() {
<-timer.C
}
case <-timer.C:
// Otherwise send an echo in a goroutine so that transactions don't block
// When echo request is already in the fly, ignore sending another
// echo message
if echoInProgress.Load() {
continue
}
// Send an echo in a goroutine so that transactions don't block
go func() {
echoInProgress.Store(true)
ctx, cancel := context.WithTimeout(context.Background(), o.options.timeout)
err := o.Echo(ctx)
if err != nil {
o.logger.V(3).Error(err, "server echo reply error")
o.Disconnect()
}
cancel()
echoInProgress.Store(false)
}()
}
timer.Reset(o.options.inactivityTimeout)
Expand Down
4 changes: 4 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"crypto/tls"
"errors"
"net/url"
"time"

Expand Down Expand Up @@ -120,6 +121,9 @@ func WithReconnect(timeout time.Duration, backoff backoff.BackOff) Option {
func WithInactivityCheck(inactivityTimeout, reconnectTimeout time.Duration,
reconnectBackoff backoff.BackOff) Option {
return func(o *options) error {
if reconnectTimeout >= inactivityTimeout {
return errors.New("inactivity timeout value should be greater than reconnect timeout value")
}
o.reconnect = true
o.timeout = reconnectTimeout
o.backoff = reconnectBackoff
Expand Down

0 comments on commit 68f6493

Please sign in to comment.