Skip to content

Commit 1e1f8f4

Browse files
committed
api: add Do* functions to ConnectionPool and ConnectionMulti
This patch provides Do, DoTyped and DoAsync functions for ConnectionPool and ConnectionMulti types. Part of #126
1 parent d6a79d9 commit 1e1f8f4

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

connection_pool/connection_pool.go

+30
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,36 @@ func (connPool *ConnectionPool) EvalAsync(expr string, args interface{}, userMod
524524
return conn.EvalAsync(expr, args)
525525
}
526526

527+
// Do sends the request and returns a response.
528+
func (connPool *ConnectionPool) Do(req tarantool.Request, userMode Mode) (*tarantool.Response, error) {
529+
conn, err := connPool.getNextConnection(userMode)
530+
if err != nil {
531+
return nil, err
532+
}
533+
534+
return conn.Do(req)
535+
}
536+
537+
// DoTyped sends the request and fills the typed result.
538+
func (connPool *ConnectionPool) DoTyped(req tarantool.Request, result interface{}, userMode Mode) error {
539+
conn, err := connPool.getNextConnection(userMode)
540+
if err != nil {
541+
return err
542+
}
543+
544+
return conn.DoTyped(req, result)
545+
}
546+
547+
// DoAsync sends the request and returns a future.
548+
func (connPool *ConnectionPool) DoAsync(req tarantool.Request, userMode Mode) (*tarantool.Future, error) {
549+
conn, err := connPool.getNextConnection(userMode)
550+
if err != nil {
551+
return nil, err
552+
}
553+
554+
return conn.DoAsync(req)
555+
}
556+
527557
//
528558
// private
529559
//

connection_pool/connection_pool_test.go

+39
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

connection_pool/example_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,22 @@ func ExampleConnectionPool_Eval() {
529529
// Code 0
530530
// Data [3]
531531
}
532+
533+
func ExampleConnectionPool_Do() {
534+
pool, err := examplePool(testRoles)
535+
if err != nil {
536+
fmt.Println(err)
537+
}
538+
defer pool.Close()
539+
540+
// Ping a Tarantool instance to check connection.
541+
req := tarantool.NewPingRequest()
542+
resp, err := pool.Do(req, connection_pool.ANY)
543+
fmt.Println("Ping Code", resp.Code)
544+
fmt.Println("Ping Data", resp.Data)
545+
fmt.Println("Ping Error", err)
546+
// Output:
547+
// Ping Code 0
548+
// Ping Data []
549+
// Ping Error <nil>
550+
}

connector.go

+4
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ type Connector interface {
4141
Call16Async(functionName string, args interface{}) *Future
4242
Call17Async(functionName string, args interface{}) *Future
4343
EvalAsync(expr string, args interface{}) *Future
44+
45+
Do(req Request) (resp *Response, err error)
46+
DoTyped(req Request, result interface{}) (err error)
47+
DoAsync(req Request) (fut *Future, err error)
4448
}

multi/multi.go

+15
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,18 @@ func (connMulti *ConnectionMulti) Call17Async(functionName string, args interfac
481481
func (connMulti *ConnectionMulti) EvalAsync(expr string, args interface{}) *tarantool.Future {
482482
return connMulti.getCurrentConnection().EvalAsync(expr, args)
483483
}
484+
485+
// Do sends the request and returns a response.
486+
func (connMulti *ConnectionMulti) Do(req tarantool.Request) (*tarantool.Response, error) {
487+
return connMulti.getCurrentConnection().Do(req)
488+
}
489+
490+
// DoTyped sends the request and fills the typed result.
491+
func (connMulti *ConnectionMulti) DoTyped(req tarantool.Request, result interface{}) error {
492+
return connMulti.getCurrentConnection().DoTyped(req, result)
493+
}
494+
495+
// DoAsync sends the request and returns a future.
496+
func (connMulti *ConnectionMulti) DoAsync(req tarantool.Request) (*tarantool.Future, error) {
497+
return connMulti.getCurrentConnection().DoAsync(req)
498+
}

0 commit comments

Comments
 (0)