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 e0bed3f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.18
uses: actions/setup-go@v2
- name: Set up Go 1.19.6
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19.6
id: go

- name: Install benchstat
Expand Down Expand Up @@ -82,10 +82,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Set up Go 1.18
uses: actions/setup-go@v1
- name: Set up Go 1.19.6
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19.6
id: go

- name: Check out code into the Go module directory
Expand Down
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
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ovn-org/libovsdb

go 1.18
go 1.19

require (
github.com/cenkalti/backoff/v4 v4.1.3
Expand Down

0 comments on commit e0bed3f

Please sign in to comment.