Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 18b653f

Browse files
author
Aleksandr Emelin
committed
prepare v0.1.0
1 parent 5604864 commit 18b653f

File tree

5 files changed

+80
-33
lines changed

5 files changed

+80
-33
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This is an opinionated modification of [github.com/tarantool/go-tarantool](https
2121
* Per-request timeout detached from underlying connection read and write timeouts.
2222
* `Op` type to express different update/upsert operations.
2323
* Some other cosmetic changes including several linter fixes.
24+
* No default `Logger` – developer needs to provide custom implementation explicitly.
2425

2526
The networking core of `github.com/tarantool/go-tarantool` kept mostly unchanged at the moment so this package should behave in similar way.
2627

changelog.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
v0.1.0
2+
======
3+
4+
* Remove default logger - user must explicitly provide logger to get logs from a package
5+
6+
v0.0.1
7+
======
8+
9+
This is an opinionated modification of [github.com/tarantool/go-tarantool](https://github.com/tarantool/go-tarantool) package.
10+
11+
Changes from the original:
12+
13+
* API changed, some non-obvious (mostly to me personally) API removed.
14+
* This package uses the latest msgpack library [github.com/vmihailenco/msgpack/v5](https://github.com/vmihailenco/msgpack) instead of `v2` in original.
15+
* Uses `enc.UseArrayEncodedStructs(true)` for `msgpack.Encoder` internally so there is no need to define `msgpack:",as_array"` struct tags.
16+
* Supports out-of-bound pushes (see [box.session.push](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_session/#box-session-push))
17+
* Adds optional support for `context.Context` (though performance will suffer a bit, if you want a maximum performance then use non-context methods which use per-connection timeout).
18+
* Uses sync.Pool for `*msgpack.Decoder` to reduce allocations on decoding stage a bit. Actually this package allocates a bit more than the original one, but allocations are small and overall performance is comparable to the original (based on observations from internal benchmarks).
19+
* No `multi` and `queue` packages.
20+
* Only one version of `Call` which uses Tarantool 1.7 request code.
21+
* Modified connection address behavior: refer to `Connect` function docs to see details.
22+
* Per-request timeout detached from underlying connection read and write timeouts.
23+
* `Op` type to express different update/upsert operations.
24+
* Some other cosmetic changes including several linter fixes.
25+
* No default `Logger` – developer needs to provide custom implementation explicitly.

config.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
box.cfg{}
1+
fiber = require 'fiber'
2+
3+
box.cfg{
4+
readahead = 10 * 1024 * 1024, -- to keep up with benchmark load.
5+
net_msg_max = 10 * 1024, -- to keep up with benchmark load.
6+
}
27

38
box.once("init", function()
49
local s = box.schema.space.create('test', {
@@ -46,6 +51,11 @@ box.schema.user.grant('test', 'read,write', 'space', 'test')
4651
box.schema.user.grant('test', 'read,write', 'space', 'schematest')
4752
end)
4853

54+
function timeout()
55+
fiber.sleep(1)
56+
return 1
57+
end
58+
4959
function simple_incr(a)
5060
return a+1
5161
end

connection.go

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import (
55
"bytes"
66
"context"
77
"errors"
8-
"fmt"
98
"io"
10-
"log"
119
"net"
1210
"net/url"
1311
"runtime"
@@ -76,26 +74,6 @@ type Logger interface {
7674
Report(event ConnLogKind, conn *Connection, v ...interface{})
7775
}
7876

79-
type defaultLogger struct{}
80-
81-
func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interface{}) {
82-
switch event {
83-
case LogReconnectFailed:
84-
reconnects := v[0].(uint64)
85-
err := v[1].(error)
86-
log.Printf("tarantool: reconnect (%d/%d) to %s://%s failed: %s\n", reconnects, conn.opts.MaxReconnects, conn.opts.network, conn.opts.address, err.Error())
87-
case LogLastReconnectFailed:
88-
err := v[0].(error)
89-
log.Printf("tarantool: last reconnect to %s://%s failed: %s, giving it up.\n", conn.opts.network, conn.opts.address, err.Error())
90-
case LogUnexpectedResultID:
91-
resp := v[0].(*Response)
92-
log.Printf("tarantool: connection %s://%s got unexpected resultId (%d) in response", conn.opts.network, conn.opts.address, resp.RequestID)
93-
default:
94-
args := append([]interface{}{"tarantool: unexpected event ", event, conn}, v...)
95-
log.Print(args...)
96-
}
97-
}
98-
9977
// Connection to Tarantool.
10078
//
10179
// It is created and configured with Connect function, and could not be
@@ -212,7 +190,7 @@ type Opts struct {
212190
Notify chan<- ConnEvent
213191
// Handle is user specified value, that could be retrieved with Handle() method.
214192
Handle interface{}
215-
// Logger is user specified logger used for error messages.
193+
// Logger is user specified logger used for log messages.
216194
Logger Logger
217195

218196
network string
@@ -276,10 +254,6 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
276254
}
277255
}
278256

279-
if conn.opts.Logger == nil {
280-
conn.opts.Logger = defaultLogger{}
281-
}
282-
283257
if err = conn.createConnection(false); err != nil {
284258
ter, ok := err.(Error)
285259
if conn.opts.ReconnectDelay <= 0 {
@@ -408,7 +382,7 @@ func (conn *Connection) timeouts() {
408382
}
409383
fut.err = ClientError{
410384
Code: ErrTimedOut,
411-
Msg: fmt.Sprintf("client timeout for request %d", fut.requestID),
385+
Msg: "request timeout",
412386
}
413387
fut.markReady(conn)
414388
shard.bufMu.Unlock()
@@ -632,12 +606,16 @@ func (conn *Connection) createConnection(reconnect bool) (err error) {
632606
return
633607
}
634608
if conn.opts.MaxReconnects > 0 && reconnects > conn.opts.MaxReconnects {
635-
conn.opts.Logger.Report(LogLastReconnectFailed, conn, err)
609+
if conn.opts.Logger != nil {
610+
conn.opts.Logger.Report(LogLastReconnectFailed, conn, err)
611+
}
636612
err = ClientError{ErrConnectionClosed, "last reconnect failed"}
637613
// mark connection as closed to avoid reopening by another goroutine.
638614
return
639615
}
640-
conn.opts.Logger.Report(LogReconnectFailed, conn, reconnects, err)
616+
if conn.opts.Logger != nil {
617+
conn.opts.Logger.Report(LogReconnectFailed, conn, reconnects, err)
618+
}
641619
conn.notify(ReconnectFailed)
642620
reconnects++
643621
conn.mutex.Unlock()
@@ -797,15 +775,19 @@ func (conn *Connection) reader(r *bufio.Reader, c net.Conn) {
797775
if fut := conn.peekFuture(resp.RequestID); fut != nil {
798776
fut.markPushReady(resp)
799777
} else {
800-
conn.opts.Logger.Report(LogUnexpectedResultID, conn, resp)
778+
if conn.opts.Logger != nil {
779+
conn.opts.Logger.Report(LogUnexpectedResultID, conn, resp)
780+
}
801781
}
802782
continue
803783
}
804784
if fut := conn.fetchFuture(resp.RequestID); fut != nil {
805785
fut.resp = resp
806786
fut.markReady(conn)
807787
} else {
808-
conn.opts.Logger.Report(LogUnexpectedResultID, conn, resp)
788+
if conn.opts.Logger != nil {
789+
conn.opts.Logger.Report(LogUnexpectedResultID, conn, resp)
790+
}
809791
}
810792
}
811793
}

tarantool_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,35 @@ func BenchmarkClientFutureParallelTyped(b *testing.B) {
285285
})
286286
}
287287

288+
func BenchmarkClientParallelTimeouts(b *testing.B) {
289+
var err error
290+
291+
var options = Opts{
292+
RequestTimeout: time.Millisecond,
293+
User: "test",
294+
Password: "test",
295+
SkipSchema: true,
296+
}
297+
298+
conn, err := Connect(server, options)
299+
if err != nil {
300+
b.Errorf("No connection available")
301+
return
302+
}
303+
defer func() { _ = conn.Close() }()
304+
305+
b.SetParallelism(1024)
306+
b.ResetTimer()
307+
b.RunParallel(func(pb *testing.PB) {
308+
for pb.Next() {
309+
_, err := conn.Exec(Call("timeout", [][]interface{}{}))
310+
if err.(ClientError).Code != ErrTimedOut {
311+
b.Fatal(err.Error())
312+
}
313+
}
314+
})
315+
}
316+
288317
func BenchmarkClientParallel(b *testing.B) {
289318
conn, err := Connect(server, opts)
290319
if err != nil {

0 commit comments

Comments
 (0)