Skip to content

Commit eebf5c1

Browse files
committed
hotfixes
1 parent 896e463 commit eebf5c1

File tree

7 files changed

+87
-22
lines changed

7 files changed

+87
-22
lines changed

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func Connect(ctx context.Context, dialer Dialer, opts Opts) (conn *Connection, e
376376
conn.cond = sync.NewCond(&conn.mutex)
377377
conn.slicePool = &sync.Pool{
378378
New: func() any {
379-
buf := make([]byte, 0, 512)
379+
buf := make([]byte, 0, 4096)
380380
return &buf
381381
},
382382
}

future.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (fut *Future) GetResponse() (Response, error) {
101101
// "error" could be Error, if it is error returned by Tarantool,
102102
// or ClientError, if something bad happens in a client process.
103103
func (fut *Future) Get() ([]interface{}, error) {
104-
defer fut.release()
104+
defer fut.Release()
105105
fut.wait()
106106
if fut.err != nil {
107107
return nil, fut.err
@@ -114,7 +114,7 @@ func (fut *Future) Get() ([]interface{}, error) {
114114
//
115115
// Note: Tarantool usually returns array of tuples (except for Eval and Call17 actions).
116116
func (fut *Future) GetTyped(result interface{}) error {
117-
defer fut.release()
117+
defer fut.Release()
118118
fut.wait()
119119
if fut.err != nil {
120120
return fut.err
@@ -136,8 +136,9 @@ func (fut *Future) WaitChan() <-chan struct{} {
136136
return fut.done
137137
}
138138

139-
func (fut *Future) release() {
139+
func (fut *Future) Release() {
140140
if fut.pool != nil && fut.resp != nil {
141-
fut.pool.Put(fut.resp.Buffer())
141+
ptr := fut.resp.Release()
142+
fut.pool.Put(ptr)
142143
}
143144
}

future_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func (resp *futureMockResponse) Header() Header {
5353
return resp.header
5454
}
5555

56-
func (resp *futureMockResponse) Buffer() *[]byte {
56+
func (resp *futureMockResponse) Release() *[]byte {
57+
// Releasing futureMockResponse data.
5758
return &resp.data
5859
}
5960

prepared.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (req *PrepareRequest) Response(header Header, body io.Reader) (Response, er
9090
if err != nil {
9191
return nil, err
9292
}
93-
return &PrepareResponse{baseResponse: baseResp}, nil
93+
return &PrepareResponse{baseResponse: *baseResp}, nil
9494
}
9595

9696
// UnprepareRequest helps you to create an unprepare request object for
@@ -204,5 +204,5 @@ func (req *ExecutePreparedRequest) Response(header Header, body io.Reader) (Resp
204204
if err != nil {
205205
return nil, err
206206
}
207-
return &ExecuteResponse{baseResponse: baseResp}, nil
207+
return &ExecuteResponse{baseResponse: *baseResp}, nil
208208
}

request.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,11 +1031,11 @@ func (req *SelectRequest) Context(ctx context.Context) *SelectRequest {
10311031

10321032
// Response creates a response for the SelectRequest.
10331033
func (req *SelectRequest) Response(header Header, body io.Reader) (Response, error) {
1034-
baseResp, err := createBaseResponse(header, body)
1034+
SelectResp, err := createSelectResponse(header, body)
10351035
if err != nil {
10361036
return nil, err
10371037
}
1038-
return &SelectResponse{baseResponse: baseResp}, nil
1038+
return SelectResp, nil
10391039
}
10401040

10411041
// InsertRequest helps you to create an insert request object for execution
@@ -1565,7 +1565,7 @@ func (req *ExecuteRequest) Response(header Header, body io.Reader) (Response, er
15651565
if err != nil {
15661566
return nil, err
15671567
}
1568-
return &ExecuteResponse{baseResponse: baseResp}, nil
1568+
return &ExecuteResponse{baseResponse: *baseResp}, nil
15691569
}
15701570

15711571
// WatchOnceRequest synchronously fetches the value currently associated with a

response.go

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tarantool
33
import (
44
"fmt"
55
"io"
6+
"sync"
67

78
"github.com/tarantool/go-iproto"
89
"github.com/vmihailenco/msgpack/v5"
@@ -12,8 +13,8 @@ import (
1213
type Response interface {
1314
// Header returns a response header.
1415
Header() Header
15-
// Buffer returns a response buffer.
16-
Buffer() *[]byte
16+
// Release free responses data and returns buffer's data.
17+
Release() *[]byte
1718
// Decode decodes a response.
1819
Decode() ([]interface{}, error)
1920
// DecodeTyped decodes a response into a given container res.
@@ -33,24 +34,54 @@ type baseResponse struct {
3334
err error
3435
}
3536

36-
func createBaseResponse(header Header, body io.Reader) (baseResponse, error) {
37+
var baseResponsePool *sync.Pool = &sync.Pool{
38+
New: func() interface{} {
39+
return &baseResponse{}
40+
},
41+
}
42+
43+
func createBaseResponse(header Header, body io.Reader) (*baseResponse, error) {
44+
resp := baseResponsePool.Get().(*baseResponse)
3745
if body == nil {
38-
return baseResponse{header: header}, nil
46+
resp.header = header
47+
return resp, nil
3948
}
4049
if buf, ok := body.(*smallBuf); ok {
41-
return baseResponse{header: header, buf: *buf}, nil
50+
resp.header = header
51+
resp.buf.b = buf.b
52+
resp.buf.p = buf.p
53+
return resp, nil
4254
}
4355
data, err := io.ReadAll(body)
4456
if err != nil {
45-
return baseResponse{}, err
57+
return resp, err
4658
}
47-
return baseResponse{header: header, buf: smallBuf{b: data}}, nil
59+
resp.header = header
60+
resp.buf.b = data
61+
return resp, nil
62+
}
63+
64+
func (resp *baseResponse) clear() *[]byte {
65+
resp.header.RequestId, resp.header.Error = 0, 0
66+
resp.data = nil
67+
resp.buf.b = resp.buf.b[:0]
68+
resp.buf.p = 0
69+
resp.decoded = false
70+
resp.decodedTyped = false
71+
resp.err = nil
72+
return &resp.buf.b
73+
}
74+
75+
func (resp *baseResponse) Release() *[]byte {
76+
ptr := resp.clear()
77+
baseResponsePool.Put(resp)
78+
return ptr
4879
}
4980

5081
// DecodeBaseResponse parse response header and body.
5182
func DecodeBaseResponse(header Header, body io.Reader) (Response, error) {
5283
resp, err := createBaseResponse(header, body)
53-
return &resp, err
84+
return resp, err
5485
}
5586

5687
// SelectResponse is used for the select requests.
@@ -659,8 +690,39 @@ func (resp *baseResponse) Header() Header {
659690
return resp.header
660691
}
661692

662-
func (resp *baseResponse) Buffer() *[]byte {
663-
return &resp.buf.b
693+
var selectResponsePool *sync.Pool = &sync.Pool{
694+
New: func() interface{} {
695+
return &SelectResponse{}
696+
},
697+
}
698+
699+
func createSelectResponse(header Header, body io.Reader) (*SelectResponse, error) {
700+
resp := selectResponsePool.Get().(*SelectResponse)
701+
if body == nil {
702+
resp.header = header
703+
return resp, nil
704+
}
705+
if buf, ok := body.(*smallBuf); ok {
706+
resp.header = header
707+
resp.buf.b = buf.b
708+
resp.buf.p = buf.p
709+
return resp, nil
710+
}
711+
data, err := io.ReadAll(body)
712+
if err != nil {
713+
return resp, err
714+
}
715+
resp.header = header
716+
resp.buf.b = data
717+
return resp, nil
718+
}
719+
720+
func (resp *SelectResponse) Release() *[]byte {
721+
ptr := resp.baseResponse.clear()
722+
resp.pos = resp.pos[:0]
723+
724+
selectResponsePool.Put(resp)
725+
return ptr
664726
}
665727

666728
// Pos returns a position descriptor of the last selected tuple for the SelectResponse.

test_helpers/response.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func (resp *MockResponse) Header() tarantool.Header {
5454
return resp.header
5555
}
5656

57-
func (resp *MockResponse) Buffer() *[]byte {
57+
func (resp *MockResponse) Release() *[]byte {
58+
// Releasing MockResponse data
5859
return &resp.data
5960
}
6061

0 commit comments

Comments
 (0)