Skip to content

Commit ceb6678

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 2f550a3 commit ceb6678

File tree

5 files changed

+107
-0
lines changed

5 files changed

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

connection_pool/example_test.go

Lines changed: 19 additions & 0 deletions
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

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,18 @@ 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 sends the request and returns a response.
459+
func (connMulti *ConnectionMulti) Do(req tarantool.Request) (*tarantool.Response, error) {
460+
return connMulti.getCurrentConnection().Do(req)
461+
}
462+
463+
// DoTyped sends the request and fills the typed result.
464+
func (connMulti *ConnectionMulti) DoTyped(req tarantool.Request, result interface{}) error {
465+
return connMulti.getCurrentConnection().DoTyped(req, result)
466+
}
467+
468+
// DoAsync sends the request and returns a future.
469+
func (connMulti *ConnectionMulti) DoAsync(req tarantool.Request) (*tarantool.Future, error) {
470+
return connMulti.getCurrentConnection().DoAsync(req)
471+
}

0 commit comments

Comments
 (0)