Skip to content

Commit 0d42a5e

Browse files
committed
api: add Do* functions to ConnectionPool and ConnectionMulti
This patch provides Do, DoTyped and DoAsync functions for ConnectionPool and ConnectionMulti types. Closes #126
1 parent 3818833 commit 0d42a5e

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

connection_pool/connection_pool.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,36 @@ func (connPool *ConnectionPool) EvalAsync(expr string, args interface{}, userMod
482482
return conn.EvalAsync(expr, args)
483483
}
484484

485+
// Do verifies, sends the request and returns a response.
486+
func (connPool *ConnectionPool) Do(req tarantool.Request, userMode Mode) (*tarantool.Response, error) {
487+
conn, err := connPool.getNextConnection(userMode)
488+
if err != nil {
489+
return nil, err
490+
}
491+
492+
return conn.Do(req)
493+
}
494+
495+
// DoTyped verifies, sends the request and fills the typed result.
496+
func (connPool *ConnectionPool) DoTyped(req tarantool.Request, result interface{}, userMode Mode) error {
497+
conn, err := connPool.getNextConnection(userMode)
498+
if err != nil {
499+
return err
500+
}
501+
502+
return conn.DoTyped(req, result)
503+
}
504+
505+
// DoAsync verifies, sends the request and returns a future.
506+
func (connPool *ConnectionPool) DoAsync(req tarantool.Request, userMode Mode) (*tarantool.Future, error) {
507+
conn, err := connPool.getNextConnection(userMode)
508+
if err != nil {
509+
return nil, err
510+
}
511+
512+
return conn.DoAsync(req)
513+
}
514+
485515
//
486516
// private
487517
//

connection_pool/connection_pool_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,45 @@ func TestPing(t *testing.T) {
12521252
require.NotNilf(t, resp, "response is nil after Ping")
12531253
}
12541254

1255+
func TestDo(t *testing.T) {
1256+
roles := []bool{true, true, false, true, false}
1257+
1258+
err := test_helpers.SetClusterRO(servers, connOpts, roles)
1259+
require.Nilf(t, err, "fail to set roles for cluster")
1260+
1261+
connPool, err := connection_pool.Connect(servers, connOpts)
1262+
require.Nilf(t, err, "failed to connect")
1263+
require.NotNilf(t, connPool, "conn is nil after Connect")
1264+
1265+
defer connPool.Close()
1266+
1267+
req := tarantool.NewPingRequest()
1268+
// ANY
1269+
resp, err := connPool.Do(req, connection_pool.ANY)
1270+
require.Nilf(t, err, "failed to Ping")
1271+
require.NotNilf(t, resp, "response is nil after Ping")
1272+
1273+
// RW
1274+
resp, err = connPool.Do(req, connection_pool.RW)
1275+
require.Nilf(t, err, "failed to Ping")
1276+
require.NotNilf(t, resp, "response is nil after Ping")
1277+
1278+
// RO
1279+
resp, err = connPool.Do(req, connection_pool.RO)
1280+
require.Nilf(t, err, "failed to Ping")
1281+
require.NotNilf(t, resp, "response is nil after Ping")
1282+
1283+
// PreferRW
1284+
resp, err = connPool.Do(req, connection_pool.PreferRW)
1285+
require.Nilf(t, err, "failed to Ping")
1286+
require.NotNilf(t, resp, "response is nil after Ping")
1287+
1288+
// PreferRO
1289+
resp, err = connPool.Do(req, connection_pool.PreferRO)
1290+
require.Nilf(t, err, "failed to Ping")
1291+
require.NotNilf(t, resp, "response is nil after Ping")
1292+
}
1293+
12551294
// runTestMain is a body of TestMain function
12561295
// (see https://pkg.go.dev/testing#hdr-Main).
12571296
// Using defer + os.Exit is not works so TestMain body

connector.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ type Connector interface {
3838
CallAsync(functionName string, args interface{}) *Future
3939
Call17Async(functionName string, args interface{}) *Future
4040
EvalAsync(expr string, args interface{}) *Future
41+
42+
Do(req Request) (resp *Response, err error)
43+
DoTyped(req Request, result interface{}) (err error)
44+
DoAsync(req Request) (fut *Future, err error)
4145
}

multi/multi.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,27 @@ func (connMulti *ConnectionMulti) Call17Async(functionName string, args interfac
454454
func (connMulti *ConnectionMulti) EvalAsync(expr string, args interface{}) *tarantool.Future {
455455
return connMulti.getCurrentConnection().EvalAsync(expr, args)
456456
}
457+
458+
// Do verifies, sends the request and returns a response.
459+
//
460+
// An error is returned if the request was formed incorrectly, or failure to
461+
// communicate by the connection, or unable to decode the response.
462+
func (connMulti *ConnectionMulti) Do(req tarantool.Request) (*tarantool.Response, error) {
463+
return connMulti.getCurrentConnection().Do(req)
464+
}
465+
466+
// DoTyped verifies, sends the request and fills the typed result.
467+
//
468+
// An error is returned if the request was formed incorrectly, or failure to
469+
// communicate by the connection, or unable to decode the response.
470+
func (connMulti *ConnectionMulti) DoTyped(req tarantool.Request, result interface{}) error {
471+
return connMulti.getCurrentConnection().DoTyped(req, result)
472+
}
473+
474+
// DoAsync verifies, sends the request and returns a future.
475+
//
476+
// An error is returned if the request was formed incorrectly, or failure to
477+
// create the future.
478+
func (connMulti *ConnectionMulti) DoAsync(req tarantool.Request) (*tarantool.Future, error) {
479+
return connMulti.getCurrentConnection().DoAsync(req)
480+
}

0 commit comments

Comments
 (0)