Skip to content

Commit d42b1b8

Browse files
api: more informative request canceling
Log the probable reason for unexpected requestId. Add requestId info to context done error message.
1 parent 0ccdee2 commit d42b1b8

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1818

1919
### Changed
2020

21+
- More informative request canceling: log the probable reason for unexpected request ID
22+
and add request ID info to context done error message (#407).
23+
2124
### Fixed
2225

2326
## [2.1.0] - 2024-03-06

connection.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interfac
100100
conn.Addr(), err)
101101
case LogUnexpectedResultId:
102102
header := v[0].(Header)
103-
log.Printf("tarantool: connection %s got unexpected resultId (%d) in response",
103+
log.Printf("tarantool: connection %s got unexpected request ID (%d) in response "+
104+
"(probably cancelled request)",
104105
conn.Addr(), header.RequestId)
105106
case LogWatchEventReadFailed:
106107
err := v[0].(error)
@@ -940,7 +941,7 @@ func (conn *Connection) newFuture(req Request) (fut *Future) {
940941
if ctx != nil {
941942
select {
942943
case <-ctx.Done():
943-
fut.SetError(fmt.Errorf("context is done"))
944+
fut.SetError(fmt.Errorf("context is done (request ID %d)", fut.requestId))
944945
shard.rmut.Unlock()
945946
return
946947
default:
@@ -982,7 +983,7 @@ func (conn *Connection) contextWatchdog(fut *Future, ctx context.Context) {
982983
case <-fut.done:
983984
return
984985
default:
985-
conn.cancelFuture(fut, fmt.Errorf("context is done"))
986+
conn.cancelFuture(fut, fmt.Errorf("context is done (request ID %d)", fut.requestId))
986987
}
987988
}
988989

@@ -1008,7 +1009,7 @@ func (conn *Connection) send(req Request, streamId uint64) *Future {
10081009
if req.Ctx() != nil {
10091010
select {
10101011
case <-req.Ctx().Done():
1011-
conn.cancelFuture(fut, fmt.Errorf("context is done"))
1012+
conn.cancelFuture(fut, fmt.Errorf("context is done (request ID %d)", fut.requestId))
10121013
return fut
10131014
default:
10141015
}

example_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"regexp"
78
"time"
89

910
"github.com/tarantool/go-iproto"
@@ -163,10 +164,10 @@ func ExamplePingRequest_Context() {
163164
// Ping a Tarantool instance to check connection.
164165
data, err := conn.Do(req).Get()
165166
fmt.Println("Ping Resp data", data)
166-
fmt.Println("Ping Error", err)
167+
fmt.Println("Ping Error", regexp.MustCompile("[0-9]+").ReplaceAllString(err.Error(), "N"))
167168
// Output:
168169
// Ping Resp data []
169-
// Ping Error context is done
170+
// Ping Error context is done (request ID N)
170171
}
171172

172173
func ExampleSelectRequest() {

tarantool_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os/exec"
1212
"path/filepath"
1313
"reflect"
14+
"regexp"
1415
"runtime"
1516
"strings"
1617
"sync"
@@ -47,6 +48,8 @@ type Member struct {
4748
Val uint
4849
}
4950

51+
var contextDoneErrRegexp = regexp.MustCompile(`^context is done \(request ID [0-9]+\)$`)
52+
5053
func (m *Member) EncodeMsgpack(e *msgpack.Encoder) error {
5154
if err := e.EncodeArrayLen(2); err != nil {
5255
return err
@@ -2731,7 +2734,7 @@ func TestClientRequestObjectsWithPassedCanceledContext(t *testing.T) {
27312734
req := NewPingRequest().Context(ctx)
27322735
cancel()
27332736
resp, err := conn.Do(req).Get()
2734-
if err.Error() != "context is done" {
2737+
if !contextDoneErrRegexp.Match([]byte(err.Error())) {
27352738
t.Fatalf("Failed to catch an error from done context")
27362739
}
27372740
if resp != nil {
@@ -2802,7 +2805,7 @@ func TestClientRequestObjectsWithContext(t *testing.T) {
28022805
if err == nil {
28032806
t.Fatalf("caught nil error")
28042807
}
2805-
if err.Error() != "context is done" {
2808+
if !contextDoneErrRegexp.Match([]byte(err.Error())) {
28062809
t.Fatalf("wrong error caught: %v", err)
28072810
}
28082811
}
@@ -3295,7 +3298,7 @@ func TestClientIdRequestObjectWithPassedCanceledContext(t *testing.T) {
32953298
resp, err := conn.Do(req).Get()
32963299
require.Nilf(t, resp, "Response is empty")
32973300
require.NotNilf(t, err, "Error is not empty")
3298-
require.Equal(t, err.Error(), "context is done")
3301+
require.Regexp(t, contextDoneErrRegexp, err.Error())
32993302
}
33003303

33013304
func TestConnectionProtocolInfoUnsupported(t *testing.T) {

0 commit comments

Comments
 (0)