Skip to content

Commit 28adc0b

Browse files
authored
Merge pull request #71 from alexopryshko/multi
Reconnection strategy - round-robin between instances #68
2 parents 5628de8 + 61b54ef commit 28adc0b

9 files changed

+565
-9
lines changed

connection.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interfac
111111
// always returns array of array (array of tuples for space related methods).
112112
// For Eval* and Call17* tarantool always returns array, but does not forces
113113
// array of arrays.
114+
114115
type Connection struct {
115116
addr string
116117
c net.Conn
@@ -132,6 +133,8 @@ type Connection struct {
132133
lenbuf [PacketLengthBytes]byte
133134
}
134135

136+
var _ = Connector(&Connection{}) // check compatibility with connector interface
137+
135138
type connShard struct {
136139
rmut sync.Mutex
137140
requests [requestsMap]struct {
@@ -224,7 +227,6 @@ type Opts struct {
224227
// - If opts.Reconnect is non-zero, then error will be returned only if authorization// fails. But if Tarantool is not reachable, then it will attempt to reconnect later
225228
// and will not end attempts on authorization failures.
226229
func Connect(addr string, opts Opts) (conn *Connection, err error) {
227-
228230
conn = &Connection{
229231
addr: addr,
230232
requestId: 0,
@@ -307,6 +309,11 @@ func (conn *Connection) ConnectedNow() bool {
307309
return atomic.LoadUint32(&conn.state) == connConnected
308310
}
309311

312+
// ClosedNow reports if connection is closed by user or after reconnect.
313+
func (conn *Connection) ClosedNow() bool {
314+
return atomic.LoadUint32(&conn.state) == connClosed
315+
}
316+
310317
// Close closes Connection.
311318
// After this method called, there is no way to reopen this Connection.
312319
func (conn *Connection) Close() error {

connector.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tarantool
2+
3+
import "time"
4+
5+
type Connector interface {
6+
ConnectedNow() bool
7+
Close() error
8+
Ping() (resp *Response, err error)
9+
ConfiguredTimeout() time.Duration
10+
11+
Select(space, index interface{}, offset, limit, iterator uint32, key interface{}) (resp *Response, err error)
12+
Insert(space interface{}, tuple interface{}) (resp *Response, err error)
13+
Replace(space interface{}, tuple interface{}) (resp *Response, err error)
14+
Delete(space, index interface{}, key interface{}) (resp *Response, err error)
15+
Update(space, index interface{}, key, ops interface{}) (resp *Response, err error)
16+
Upsert(space interface{}, tuple, ops interface{}) (resp *Response, err error)
17+
Call(functionName string, args interface{}) (resp *Response, err error)
18+
Call17(functionName string, args interface{}) (resp *Response, err error)
19+
Eval(expr string, args interface{}) (resp *Response, err error)
20+
21+
GetTyped(space, index interface{}, key interface{}, result interface{}) (err error)
22+
SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}) (err error)
23+
InsertTyped(space interface{}, tuple interface{}, result interface{}) (err error)
24+
ReplaceTyped(space interface{}, tuple interface{}, result interface{}) (err error)
25+
DeleteTyped(space, index interface{}, key interface{}, result interface{}) (err error)
26+
UpdateTyped(space, index interface{}, key, ops interface{}, result interface{}) (err error)
27+
CallTyped(functionName string, args interface{}, result interface{}) (err error)
28+
Call17Typed(functionName string, args interface{}, result interface{}) (err error)
29+
EvalTyped(expr string, args interface{}, result interface{}) (err error)
30+
31+
SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}) *Future
32+
InsertAsync(space interface{}, tuple interface{}) *Future
33+
ReplaceAsync(space interface{}, tuple interface{}) *Future
34+
DeleteAsync(space, index interface{}, key interface{}) *Future
35+
UpdateAsync(space, index interface{}, key, ops interface{}) *Future
36+
UpsertAsync(space interface{}, tuple interface{}, ops interface{}) *Future
37+
CallAsync(functionName string, args interface{}) *Future
38+
Call17Async(functionName string, args interface{}) *Future
39+
EvalAsync(expr string, args interface{}) *Future
40+
}

multi/config_m1.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
box.cfg {
2+
listen = 3013,
3+
wal_dir = 'm1/xlog',
4+
snap_dir = 'm1/snap',
5+
}
6+
7+
box.once("init", function()
8+
box.schema.user.create('test', { password = 'test' })
9+
box.schema.user.grant('test', 'read,write,execute', 'universe')
10+
end)

multi/config_m2.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
box.cfg {
2+
listen = 3014,
3+
wal_dir = 'm2/xlog',
4+
snap_dir = 'm2/snap',
5+
}
6+
7+
box.once("init", function()
8+
box.schema.user.create('test', { password = 'test' })
9+
box.schema.user.grant('test', 'read,write,execute', 'universe')
10+
end)

0 commit comments

Comments
 (0)