Skip to content

Commit

Permalink
api: left only asynchronous Connection.Do(request)
Browse files Browse the repository at this point in the history
The patch deletes synchronous Connection.Do and Connection.DoTyped and
renames an asynchronous Connection.DoAsync to Connection.Do. There are
several reasons for this:

1. It helps to make the public API shorter.
2. It makes the asynchronous essence of the connector is more clear.
3. It helps not to spread the unnecessary API to submodules.
4. We will have more freedom for changes in the future.
  • Loading branch information
oleg-jukovec committed Jun 27, 2022
1 parent 61d0739 commit 9084fd0
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 100 deletions.
2 changes: 1 addition & 1 deletion call_16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCallRequest(t *testing.T) {
defer conn.Close()

req := NewCallRequest("simple_incr").Args([]interface{}{1})
resp, err = conn.Do(req)
resp, err = conn.Do(req).Get()
if err != nil {
t.Errorf("Failed to use Call")
}
Expand Down
2 changes: 1 addition & 1 deletion call_17_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCallRequest(t *testing.T) {
defer conn.Close()

req := NewCallRequest("simple_incr").Args([]interface{}{1})
resp, err = conn.Do(req)
resp, err = conn.Do(req).Get()
if err != nil {
t.Errorf("Failed to use Call")
}
Expand Down
22 changes: 2 additions & 20 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,29 +988,11 @@ func (conn *Connection) nextRequestId() (requestId uint32) {
return atomic.AddUint32(&conn.requestId, 1)
}

// Do verifies, sends the request and returns a response.
//
// An error is returned if the request was formed incorrectly, or failed to
// communicate by the connection, or unable to decode the response.
func (conn *Connection) Do(req Request) (*Response, error) {
fut := conn.DoAsync(req)
return fut.Get()
}

// DoTyped verifies, sends the request and fills the typed result.
//
// An error is returned if the request was formed incorrectly, or failed to
// communicate by the connection, or unable to decode the response.
func (conn *Connection) DoTyped(req Request, result interface{}) error {
fut := conn.DoAsync(req)
return fut.GetTyped(result)
}

// DoAsync verifies, sends the request and returns a future.
// Do performs a request asynchronously on the connection.
//
// An error is returned if the request was formed incorrectly, or failed to
// create the future.
func (conn *Connection) DoAsync(req Request) *Future {
func (conn *Connection) Do(req Request) *Future {
return conn.send(req)
}

Expand Down
26 changes: 3 additions & 23 deletions connection_pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,34 +524,14 @@ func (connPool *ConnectionPool) EvalAsync(expr string, args interface{}, userMod
return conn.EvalAsync(expr, args)
}

// Do sends the request and returns a response.
func (connPool *ConnectionPool) Do(req tarantool.Request, userMode Mode) (*tarantool.Response, error) {
conn, err := connPool.getNextConnection(userMode)
if err != nil {
return nil, err
}

return conn.Do(req)
}

// DoTyped sends the request and fills the typed result.
func (connPool *ConnectionPool) DoTyped(req tarantool.Request, result interface{}, userMode Mode) error {
conn, err := connPool.getNextConnection(userMode)
if err != nil {
return err
}

return conn.DoTyped(req, result)
}

// DoAsync sends the request and returns a future.
func (connPool *ConnectionPool) DoAsync(req tarantool.Request, userMode Mode) *tarantool.Future {
// Do sends the request and returns a future.
func (connPool *ConnectionPool) Do(req tarantool.Request, userMode Mode) *tarantool.Future {
conn, err := connPool.getNextConnection(userMode)
if err != nil {
return tarantool.NewErrorFuture(err)
}

return conn.DoAsync(req)
return conn.Do(req)
}

//
Expand Down
10 changes: 5 additions & 5 deletions connection_pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,27 +1266,27 @@ func TestDo(t *testing.T) {

req := tarantool.NewPingRequest()
// ANY
resp, err := connPool.Do(req, connection_pool.ANY)
resp, err := connPool.Do(req, connection_pool.ANY).Get()
require.Nilf(t, err, "failed to Ping")
require.NotNilf(t, resp, "response is nil after Ping")

// RW
resp, err = connPool.Do(req, connection_pool.RW)
resp, err = connPool.Do(req, connection_pool.RW).Get()
require.Nilf(t, err, "failed to Ping")
require.NotNilf(t, resp, "response is nil after Ping")

// RO
resp, err = connPool.Do(req, connection_pool.RO)
resp, err = connPool.Do(req, connection_pool.RO).Get()
require.Nilf(t, err, "failed to Ping")
require.NotNilf(t, resp, "response is nil after Ping")

// PreferRW
resp, err = connPool.Do(req, connection_pool.PreferRW)
resp, err = connPool.Do(req, connection_pool.PreferRW).Get()
require.Nilf(t, err, "failed to Ping")
require.NotNilf(t, resp, "response is nil after Ping")

// PreferRO
resp, err = connPool.Do(req, connection_pool.PreferRO)
resp, err = connPool.Do(req, connection_pool.PreferRO).Get()
require.Nilf(t, err, "failed to Ping")
require.NotNilf(t, resp, "response is nil after Ping")
}
Expand Down
2 changes: 1 addition & 1 deletion connection_pool/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func ExampleConnectionPool_Do() {

// Ping a Tarantool instance to check connection.
req := tarantool.NewPingRequest()
resp, err := pool.Do(req, connection_pool.ANY)
resp, err := pool.Do(req, connection_pool.ANY).Get()
fmt.Println("Ping Code", resp.Code)
fmt.Println("Ping Data", resp.Data)
fmt.Println("Ping Error", err)
Expand Down
4 changes: 1 addition & 3 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,5 @@ type Connector interface {
Call17Async(functionName string, args interface{}) *Future
EvalAsync(expr string, args interface{}) *Future

Do(req Request) (resp *Response, err error)
DoTyped(req Request, result interface{}) (err error)
DoAsync(req Request) (fut *Future)
Do(req Request) (fut *Future)
}
14 changes: 7 additions & 7 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func ExampleSelectRequest() {
req := tarantool.NewSelectRequest(517).
Limit(100).
Key(tarantool.IntKey{1111})
resp, err := conn.Do(req)
resp, err := conn.Do(req).Get()
if err != nil {
fmt.Printf("error in do select request is %v", err)
return
Expand All @@ -144,7 +144,7 @@ func ExampleSelectRequest() {
Index("primary").
Limit(100).
Key(tarantool.IntKey{1111})
fut := conn.DoAsync(req)
fut := conn.Do(req)
resp, err = fut.Get()
if err != nil {
fmt.Printf("error in do async select request is %v", err)
Expand All @@ -163,7 +163,7 @@ func ExampleUpdateRequest() {
req := tarantool.NewUpdateRequest(517).
Key(tarantool.IntKey{1111}).
Operations(tarantool.NewOperations().Assign(1, "bye"))
resp, err := conn.Do(req)
resp, err := conn.Do(req).Get()
if err != nil {
fmt.Printf("error in do update request is %v", err)
return
Expand All @@ -174,7 +174,7 @@ func ExampleUpdateRequest() {
Index("primary").
Key(tarantool.IntKey{1111}).
Operations(tarantool.NewOperations().Assign(1, "hello"))
fut := conn.DoAsync(req)
fut := conn.Do(req)
resp, err = fut.Get()
if err != nil {
fmt.Printf("error in do async update request is %v", err)
Expand All @@ -194,7 +194,7 @@ func ExampleUpsertRequest() {
req = tarantool.NewUpsertRequest(517).
Tuple([]interface{}{uint(1113), "first", "first"}).
Operations(tarantool.NewOperations().Assign(1, "updated"))
resp, err := conn.Do(req)
resp, err := conn.Do(req).Get()
if err != nil {
fmt.Printf("error in do select upsert is %v", err)
return
Expand All @@ -204,7 +204,7 @@ func ExampleUpsertRequest() {
req = tarantool.NewUpsertRequest("test").
Tuple([]interface{}{uint(1113), "second", "second"}).
Operations(tarantool.NewOperations().Assign(2, "updated"))
fut := conn.DoAsync(req)
fut := conn.Do(req)
resp, err = fut.Get()
if err != nil {
fmt.Printf("error in do async upsert request is %v", err)
Expand All @@ -215,7 +215,7 @@ func ExampleUpsertRequest() {
req = tarantool.NewSelectRequest(517).
Limit(100).
Key(tarantool.IntKey{1113})
resp, err = conn.Do(req)
resp, err = conn.Do(req).Get()
if err != nil {
fmt.Printf("error in do select request is %v", err)
return
Expand Down
14 changes: 2 additions & 12 deletions multi/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,17 +482,7 @@ func (connMulti *ConnectionMulti) EvalAsync(expr string, args interface{}) *tara
return connMulti.getCurrentConnection().EvalAsync(expr, args)
}

// Do sends the request and returns a response.
func (connMulti *ConnectionMulti) Do(req tarantool.Request) (*tarantool.Response, error) {
// Do sends the request and returns a future.
func (connMulti *ConnectionMulti) Do(req tarantool.Request) *tarantool.Future {
return connMulti.getCurrentConnection().Do(req)
}

// DoTyped sends the request and fills the typed result.
func (connMulti *ConnectionMulti) DoTyped(req tarantool.Request, result interface{}) error {
return connMulti.getCurrentConnection().DoTyped(req, result)
}

// DoAsync sends the request and returns a future.
func (connMulti *ConnectionMulti) DoAsync(req tarantool.Request) *tarantool.Future {
return connMulti.getCurrentConnection().DoAsync(req)
}
24 changes: 12 additions & 12 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func fillPing(enc *msgpack.Encoder) error {

// Ping sends empty request to Tarantool to check connection.
func (conn *Connection) Ping() (resp *Response, err error) {
return conn.Do(NewPingRequest())
return conn.Do(NewPingRequest()).Get()
}

// Select performs select to box space.
Expand Down Expand Up @@ -311,44 +311,44 @@ func (conn *Connection) SelectAsync(space, index interface{}, offset, limit, ite
Limit(limit).
Iterator(iterator).
Key(key)
return conn.DoAsync(req)
return conn.Do(req)
}

// InsertAsync sends insert action to Tarantool and returns Future.
// Tarantool will reject Insert when tuple with same primary key exists.
func (conn *Connection) InsertAsync(space interface{}, tuple interface{}) *Future {
req := NewInsertRequest(space).Tuple(tuple)
return conn.DoAsync(req)
return conn.Do(req)
}

// ReplaceAsync sends "insert or replace" action to Tarantool and returns Future.
// If tuple with same primary key exists, it will be replaced.
func (conn *Connection) ReplaceAsync(space interface{}, tuple interface{}) *Future {
req := NewReplaceRequest(space).Tuple(tuple)
return conn.DoAsync(req)
return conn.Do(req)
}

// DeleteAsync sends deletion action to Tarantool and returns Future.
// Future's result will contain array with deleted tuple.
func (conn *Connection) DeleteAsync(space, index interface{}, key interface{}) *Future {
req := NewDeleteRequest(space).Index(index).Key(key)
return conn.DoAsync(req)
return conn.Do(req)
}

// Update sends deletion of a tuple by key and returns Future.
// Future's result will contain array with updated tuple.
func (conn *Connection) UpdateAsync(space, index interface{}, key, ops interface{}) *Future {
req := NewUpdateRequest(space).Index(index).Key(key)
req.ops = ops
return conn.DoAsync(req)
return conn.Do(req)
}

// UpsertAsync sends "update or insert" action to Tarantool and returns Future.
// Future's sesult will not contain any tuple.
func (conn *Connection) UpsertAsync(space interface{}, tuple interface{}, ops interface{}) *Future {
req := NewUpsertRequest(space).Tuple(tuple)
req.ops = ops
return conn.DoAsync(req)
return conn.Do(req)
}

// CallAsync sends a call to registered Tarantool function and returns Future.
Expand All @@ -357,36 +357,36 @@ func (conn *Connection) UpsertAsync(space interface{}, tuple interface{}, ops in
// Otherwise, uses request code for Tarantool 1.6.
func (conn *Connection) CallAsync(functionName string, args interface{}) *Future {
req := NewCallRequest(functionName).Args(args)
return conn.DoAsync(req)
return conn.Do(req)
}

// Call16Async sends a call to registered Tarantool function and returns Future.
// It uses request code for Tarantool 1.6, so future's result is always array of arrays.
// Deprecated since Tarantool 1.7.2.
func (conn *Connection) Call16Async(functionName string, args interface{}) *Future {
req := NewCall16Request(functionName).Args(args)
return conn.DoAsync(req)
return conn.Do(req)
}

// Call17Async sends a call to registered Tarantool function and returns Future.
// It uses request code for Tarantool >= 1.7, so future's result will not be converted
// (though, keep in mind, result is always array)
func (conn *Connection) Call17Async(functionName string, args interface{}) *Future {
req := NewCall17Request(functionName).Args(args)
return conn.DoAsync(req)
return conn.Do(req)
}

// EvalAsync sends a Lua expression for evaluation and returns Future.
func (conn *Connection) EvalAsync(expr string, args interface{}) *Future {
req := NewEvalRequest(expr).Args(args)
return conn.DoAsync(req)
return conn.Do(req)
}

// ExecuteAsync sends a sql expression for execution and returns Future.
// Since 1.6.0
func (conn *Connection) ExecuteAsync(expr string, args interface{}) *Future {
req := NewExecuteRequest(expr).Args(args)
return conn.DoAsync(req)
return conn.Do(req)
}

// KeyValueBind is a type for encoding named SQL parameters
Expand Down

0 comments on commit 9084fd0

Please sign in to comment.