@@ -87,6 +87,7 @@ type ovsdbClient struct {
87
87
metrics metrics
88
88
connected bool
89
89
rpcClient * rpc2.Client
90
+ conn net.Conn
90
91
rpcMutex sync.RWMutex
91
92
// endpoints contains all possible endpoints; the first element is
92
93
// the active endpoint if connected=true
@@ -427,6 +428,7 @@ func (o *ovsdbClient) createRPC2Client(conn net.Conn) {
427
428
if o .options .inactivityTimeout > 0 {
428
429
o .trafficSeen = make (chan struct {})
429
430
}
431
+ o .conn = conn
430
432
o .rpcClient = rpc2 .NewClientWithCodec (jsonrpc .NewJSONCodec (conn ))
431
433
o .rpcClient .SetBlocking (true )
432
434
o .rpcClient .Handle ("echo" , func (_ * rpc2.Client , args []interface {}, reply * []interface {}) error {
@@ -748,7 +750,7 @@ func (o *ovsdbClient) update3(params []json.RawMessage, reply *[]interface{}) er
748
750
func (o * ovsdbClient ) getSchema (ctx context.Context , dbName string ) (ovsdb.DatabaseSchema , error ) {
749
751
args := ovsdb .NewGetSchemaArgs (dbName )
750
752
var reply ovsdb.DatabaseSchema
751
- err := o .rpcClient . CallWithContext (ctx , "get_schema" , args , & reply )
753
+ err := o .CallWithContext (ctx , "get_schema" , args , & reply )
752
754
if err != nil {
753
755
if err == rpc2 .ErrShutdown {
754
756
return ovsdb.DatabaseSchema {}, ErrNotConnected
@@ -763,7 +765,7 @@ func (o *ovsdbClient) getSchema(ctx context.Context, dbName string) (ovsdb.Datab
763
765
// Should only be called when mutex is held
764
766
func (o * ovsdbClient ) listDbs (ctx context.Context ) ([]string , error ) {
765
767
var dbs []string
766
- err := o .rpcClient . CallWithContext (ctx , "list_dbs" , nil , & dbs )
768
+ err := o .CallWithContext (ctx , "list_dbs" , nil , & dbs )
767
769
if err != nil {
768
770
if err == rpc2 .ErrShutdown {
769
771
return nil , ErrNotConnected
@@ -836,7 +838,7 @@ func (o *ovsdbClient) transact(ctx context.Context, dbName string, skipChWrite b
836
838
if dbgLogger .Enabled () {
837
839
dbgLogger .Info ("transacting operations" , "operations" , fmt .Sprintf ("%+v" , operation ))
838
840
}
839
- err := o .rpcClient . CallWithContext (ctx , "transact" , args , & reply )
841
+ err := o .CallWithContext (ctx , "transact" , args , & reply )
840
842
if err != nil {
841
843
if err == rpc2 .ErrShutdown {
842
844
return nil , ErrNotConnected
@@ -869,7 +871,7 @@ func (o *ovsdbClient) MonitorCancel(ctx context.Context, cookie MonitorCookie) e
869
871
if o .rpcClient == nil {
870
872
return ErrNotConnected
871
873
}
872
- err := o .rpcClient . CallWithContext (ctx , "monitor_cancel" , args , & reply )
874
+ err := o .CallWithContext (ctx , "monitor_cancel" , args , & reply )
873
875
if err != nil {
874
876
if err == rpc2 .ErrShutdown {
875
877
return ErrNotConnected
@@ -981,15 +983,15 @@ func (o *ovsdbClient) monitor(ctx context.Context, cookie MonitorCookie, reconne
981
983
switch monitor .Method {
982
984
case ovsdb .MonitorRPC :
983
985
var reply ovsdb.TableUpdates
984
- err = o .rpcClient . CallWithContext (ctx , monitor .Method , args , & reply )
986
+ err = o .CallWithContext (ctx , monitor .Method , args , & reply )
985
987
tableUpdates = reply
986
988
case ovsdb .ConditionalMonitorRPC :
987
989
var reply ovsdb.TableUpdates2
988
- err = o .rpcClient . CallWithContext (ctx , monitor .Method , args , & reply )
990
+ err = o .CallWithContext (ctx , monitor .Method , args , & reply )
989
991
tableUpdates = reply
990
992
case ovsdb .ConditionalMonitorSinceRPC :
991
993
var reply ovsdb.MonitorCondSinceReply
992
- err = o .rpcClient . CallWithContext (ctx , monitor .Method , args , & reply )
994
+ err = o .CallWithContext (ctx , monitor .Method , args , & reply )
993
995
if err == nil && reply .Found {
994
996
monitor .LastTransactionID = reply .LastTransactionID
995
997
lastTransactionFound = true
@@ -1080,7 +1082,7 @@ func (o *ovsdbClient) Echo(ctx context.Context) error {
1080
1082
if o .rpcClient == nil {
1081
1083
return ErrNotConnected
1082
1084
}
1083
- err := o .rpcClient . CallWithContext (ctx , "echo" , args , & reply )
1085
+ err := o .CallWithContext (ctx , "echo" , args , & reply )
1084
1086
if err != nil {
1085
1087
if err == rpc2 .ErrShutdown {
1086
1088
return ErrNotConnected
@@ -1439,3 +1441,20 @@ func (o *ovsdbClient) WhereAll(m model.Model, conditions ...model.Condition) Con
1439
1441
func (o * ovsdbClient ) WhereCache (predicate interface {}) ConditionalAPI {
1440
1442
return o .primaryDB ().api .WhereCache (predicate )
1441
1443
}
1444
+
1445
+ // CallWithContext invokes the named function, waits for it to complete, and
1446
+ // returns its error status, or an error from Context timeout.
1447
+ func (o * ovsdbClient ) CallWithContext (ctx context.Context , method string , args interface {}, reply interface {}) error {
1448
+ // Set up read/write deadline for tcp connection before making
1449
+ // a rpc request to the server.
1450
+ if reflect .TypeOf (o .conn ).String () == "*net.TCPConn" {
1451
+ tcpConn := o .conn .(* net.TCPConn )
1452
+ if o .options .timeout > 0 {
1453
+ err := tcpConn .SetDeadline (time .Now ().Add (o .options .timeout * 3 ))
1454
+ if err != nil {
1455
+ return err
1456
+ }
1457
+ }
1458
+ }
1459
+ return o .rpcClient .CallWithContext (ctx , method , args , reply )
1460
+ }
0 commit comments