Skip to content

Commit

Permalink
Merge pull request #142 from alexanderConstantinescu/bugfix/connect-w…
Browse files Browse the repository at this point in the history
…ith-timeout

Add interface for libovsdb connection with timeout
  • Loading branch information
dave-tucker authored Jun 7, 2021
2 parents dddb539 + 1dafc33 commit 1294653
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
13 changes: 9 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -48,8 +49,9 @@ const (

// Connect to ovn, using endpoint in format ovsdb Connection Methods
// If address is empty, use default address for specified protocol
func Connect(endpoints string, database *model.DBModel, tlsConfig *tls.Config) (*OvsdbClient, error) {
func Connect(ctx context.Context, endpoints string, database *model.DBModel, tlsConfig *tls.Config) (*OvsdbClient, error) {
var c net.Conn
var dialer net.Dialer
var err error
var u *url.URL

Expand All @@ -69,11 +71,14 @@ func Connect(endpoints string, database *model.DBModel, tlsConfig *tls.Config) (
if len(path) == 0 {
path = defaultUnixAddress
}
c, err = net.Dial(u.Scheme, path)
c, err = dialer.DialContext(ctx, u.Scheme, path)
case TCP:
c, err = net.Dial(u.Scheme, host)
c, err = dialer.DialContext(ctx, u.Scheme, host)
case SSL:
c, err = tls.Dial("tcp", host, tlsConfig)
dialer := tls.Dialer{
Config: tlsConfig,
}
c, err = dialer.DialContext(ctx, "tcp", host)
default:
err = fmt.Errorf("unknown network protocol %s", u.Scheme)
}
Expand Down
31 changes: 16 additions & 15 deletions client/ovs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"context"
"log"
"os"
"testing"
Expand Down Expand Up @@ -71,14 +72,14 @@ func TestConnectIntegration(t *testing.T) {
go func() {
// Use Convenience params. Ignore failure even if any

_, err := Connect(cfg.Addr, defDB, nil)
_, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
log.Println("Couldnt establish OVSDB connection with Defult params. No big deal")
}
}()

go func() {
ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
connected <- false
} else {
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestListDbsIntegration(t *testing.T) {
t.Skip()
}

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down Expand Up @@ -141,7 +142,7 @@ func TestGetSchemasIntegration(t *testing.T) {
t.Skip()
}

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down Expand Up @@ -169,7 +170,7 @@ func TestInsertTransactIntegration(t *testing.T) {
}
SetConfig()

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down Expand Up @@ -227,7 +228,7 @@ func TestDeleteTransactIntegration(t *testing.T) {
t.Skip()
}

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down Expand Up @@ -272,7 +273,7 @@ func TestMonitorIntegration(t *testing.T) {
t.Skip()
}

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand All @@ -290,7 +291,7 @@ func TestNotifyIntegration(t *testing.T) {
}
SetConfig()

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down Expand Up @@ -321,7 +322,7 @@ func TestRemoveNotifyIntegration(t *testing.T) {
}
SetConfig()

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down Expand Up @@ -364,7 +365,7 @@ func TestTableSchemaValidationIntegration(t *testing.T) {
}
SetConfig()

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand All @@ -389,7 +390,7 @@ func TestColumnSchemaInRowValidationIntegration(t *testing.T) {
}
SetConfig()

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand All @@ -415,7 +416,7 @@ func TestColumnSchemaInMultipleRowsValidationIntegration(t *testing.T) {
}
SetConfig()

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down Expand Up @@ -447,7 +448,7 @@ func TestColumnSchemaValidationIntegration(t *testing.T) {
t.Skip()
}

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand All @@ -472,7 +473,7 @@ func TestMonitorCancelIntegration(t *testing.T) {
}
SetConfig()

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down Expand Up @@ -504,7 +505,7 @@ func TestInsertDuplicateTransactIntegration(t *testing.T) {
}
SetConfig()

ovs, err := Connect(cfg.Addr, defDB, nil)
ovs, err := Connect(context.Background(), cfg.Addr, defDB, nil)
if err != nil {
t.Fatalf("Failed to Connect. error: %s", err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/stress/stress.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -45,7 +46,7 @@ var (
)

func run() {
ovs, err := client.Connect(*connection, dbModel, nil)
ovs, err := client.Connect(context.Background(), *connection, dbModel, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion example/play_with_ovs/play_with_ovs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -101,7 +102,7 @@ func main() {
log.Fatal("Unable to create DB model ", err)
}
// By default libovsdb connects to 127.0.0.0:6400.
ovs, err := client.Connect(*connection, dbmodel, nil)
ovs, err := client.Connect(context.Background(), *connection, dbmodel, nil)

// If you prefer to connect to OVS in a specific location :
// ovs, err := client.Connect("tcp:192.168.56.101:6640", nil)
Expand Down

0 comments on commit 1294653

Please sign in to comment.