-
Notifications
You must be signed in to change notification settings - Fork 18
/
tarantool_adapter.go
88 lines (81 loc) · 1.89 KB
/
tarantool_adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package Tt
import (
"context"
"errors"
"fmt"
"time"
"github.com/kokizzu/gotro/L"
"github.com/tarantool/go-tarantool/v2"
)
type Adapter struct {
*tarantool.Connection
Reconnect func() *tarantool.Connection
}
func (a *Adapter) RetryDo(op tarantool.Request, times ...int) ([]any, error) {
count := 3
if len(times) == 1 {
count = times[0]
}
for {
res, err := a.Connection.Do(op).Get()
if err != nil {
var e tarantool.ClientError
if errors.As(err, &e) && e.Code == 16385 { // using closed connection (0x4001)
a.Connection = a.Reconnect()
count--
if count == 0 {
return res, err
}
continue
}
}
return res, err
}
}
func (a *Adapter) RetryDoResp(op tarantool.Request, times ...int) (tarantool.Response, error) {
count := 3
if len(times) == 1 {
count = times[0]
}
for {
future, err := a.Connection.Do(op).GetResponse()
if err != nil {
var e tarantool.ClientError
if errors.As(err, &e) && e.Code == 16385 { // using closed connection (0x4001)
a.Connection = a.Reconnect()
count--
if count == 0 {
return future, err
}
continue
}
}
return future, err
}
}
// NewAdapter create new tarantool adapter
// adapter contains helper methods for schema manipulation and query execution
func NewAdapter(connectFunc func() *tarantool.Connection) *Adapter {
return &Adapter{
Reconnect: connectFunc,
Connection: connectFunc(),
}
}
// Connect1 is example of connect function
// to connect on terminal locally, use:
// tarantoolctl connect user:password@localhost:3301
func Connect1(host, port, user, pass string) *tarantool.Connection {
hostPort := fmt.Sprintf(`%s:%s`,
host,
port,
)
taran, err := tarantool.Connect(context.Background(), tarantool.NetDialer{
Address: hostPort,
User: user,
Password: pass,
}, tarantool.Opts{
Timeout: 8 * time.Second,
})
L.PanicIf(err, `tarantool.Connect `+hostPort)
return taran
}