1
+ // Package with implementation of methods and structures for work with
2
+ // Tarantool instance.
1
3
package tarantool
2
4
3
5
import (
@@ -27,26 +29,26 @@ type ConnEventKind int
27
29
type ConnLogKind int
28
30
29
31
const (
30
- // Connect signals that connection is established or reestablished
32
+ // Connected signals that connection is established or reestablished.
31
33
Connected ConnEventKind = iota + 1
32
- // Disconnect signals that connection is broken
34
+ // Disconnected signals that connection is broken.
33
35
Disconnected
34
- // ReconnectFailed signals that attempt to reconnect has failed
36
+ // ReconnectFailed signals that attempt to reconnect has failed.
35
37
ReconnectFailed
36
- // Either reconnect attempts exhausted, or explicit Close is called
38
+ // Either reconnect attempts exhausted, or explicit Close is called.
37
39
Closed
38
40
39
- // LogReconnectFailed is logged when reconnect attempt failed
41
+ // LogReconnectFailed is logged when reconnect attempt failed.
40
42
LogReconnectFailed ConnLogKind = iota + 1
41
43
// LogLastReconnectFailed is logged when last reconnect attempt failed,
42
44
// connection will be closed after that.
43
45
LogLastReconnectFailed
44
- // LogUnexpectedResultId is logged when response with unknown id were received.
46
+ // LogUnexpectedResultId is logged when response with unknown id was received.
45
47
// Most probably it is due to request timeout.
46
48
LogUnexpectedResultId
47
49
)
48
50
49
- // ConnEvent is sent throw Notify channel specified in Opts
51
+ // ConnEvent is sent throw Notify channel specified in Opts.
50
52
type ConnEvent struct {
51
53
Conn * Connection
52
54
Kind ConnEventKind
@@ -80,47 +82,47 @@ func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interfac
80
82
}
81
83
}
82
84
83
- // Connection is a handle to Tarantool.
85
+ // Connection is a handle with a single connection to a Tarantool instance .
84
86
//
85
87
// It is created and configured with Connect function, and could not be
86
88
// reconfigured later.
87
89
//
88
- // It is could be "Connected", "Disconnected", and "Closed".
90
+ // Connection could be in three possible states:
89
91
//
90
- // When "Connected" it sends queries to Tarantool.
92
+ // - In "Connected" state it sends queries to Tarantool.
91
93
//
92
- // When "Disconnected" it rejects queries with ClientError{Code: ErrConnectionNotReady}
94
+ // - In "Disconnected" state it rejects queries with ClientError{Code:
95
+ // ErrConnectionNotReady}
93
96
//
94
- // When "Closed" it rejects queries with ClientError{Code: ErrConnectionClosed}
95
- //
96
- // Connection could become "Closed" when Connection .Close() method called,
97
- // or when Tarantool disconnected and Reconnect pause is not specified or
98
- // MaxReconnects is specified and MaxReconnect reconnect attempts already performed.
97
+ // - In "Closed" state it rejects queries with ClientError{Code:
98
+ // ErrConnectionClosed}. Connection could become "Closed" when
99
+ // Connection.Close() method called, or when Tarantool disconnected and
100
+ // Reconnect pause is not specified or MaxReconnects is specified and
101
+ // MaxReconnect reconnect attempts already performed.
99
102
//
100
103
// You may perform data manipulation operation by calling its methods:
101
104
// Call*, Insert*, Replace*, Update*, Upsert*, Call*, Eval*.
102
105
//
103
- // In any method that accepts ` space` you my pass either space number or
104
- // space name (in this case it will be looked up in schema). Same is true for ` index` .
106
+ // In any method that accepts space you my pass either space number or space
107
+ // name (in this case it will be looked up in schema). Same is true for index.
105
108
//
106
- // ATTENTION: ` tuple`, ` key`, ` ops` and ` args` arguments for any method should be
109
+ // ATTENTION: tuple, key, ops and args arguments for any method should be
107
110
// and array or should serialize to msgpack array.
108
111
//
109
- // ATTENTION: ` result` argument for *Typed methods should deserialize from
112
+ // ATTENTION: result argument for *Typed methods should deserialize from
110
113
// msgpack array, cause Tarantool always returns result as an array.
111
114
// For all space related methods and Call* (but not Call17*) methods Tarantool
112
115
// always returns array of array (array of tuples for space related methods).
113
- // For Eval* and Call17* tarantool always returns array, but does not forces
116
+ // For Eval* and Call17* Tarantool always returns array, but does not forces
114
117
// array of arrays.
115
-
116
118
type Connection struct {
117
119
addr string
118
120
c net.Conn
119
121
mutex sync.Mutex
120
122
// Schema contains schema loaded on connection.
121
123
Schema * Schema
122
124
requestId uint32
123
- // Greeting contains first message sent by tarantool
125
+ // Greeting contains first message sent by Tarantool.
124
126
Greeting * Greeting
125
127
126
128
shard []connShard
@@ -134,7 +136,7 @@ type Connection struct {
134
136
lenbuf [PacketLengthBytes ]byte
135
137
}
136
138
137
- var _ = Connector (& Connection {}) // check compatibility with connector interface
139
+ var _ = Connector (& Connection {}) // Check compatibility with connector interface.
138
140
139
141
type connShard struct {
140
142
rmut sync.Mutex
@@ -148,7 +150,7 @@ type connShard struct {
148
150
_pad [16 ]uint64
149
151
}
150
152
151
- // Greeting is a message sent by tarantool on connect.
153
+ // Greeting is a message sent by Tarantool on connect.
152
154
type Greeting struct {
153
155
Version string
154
156
auth string
@@ -157,22 +159,22 @@ type Greeting struct {
157
159
// Opts is a way to configure Connection
158
160
type Opts struct {
159
161
// Timeout is requests timeout.
160
- // Also used to setup net.TCPConn.Set(Read|Write)Deadline
162
+ // Also used to setup net.TCPConn.Set(Read|Write)Deadline.
161
163
Timeout time.Duration
162
164
// Reconnect is a pause between reconnection attempts.
163
- // If specified, then when tarantool is not reachable or disconnected,
165
+ // If specified, then when Tarantool is not reachable or disconnected,
164
166
// new connect attempt is performed after pause.
165
167
// By default, no reconnection attempts are performed,
166
168
// so once disconnected, connection becomes Closed.
167
169
Reconnect time.Duration
168
170
// MaxReconnects is a maximum reconnect attempts.
169
171
// After MaxReconnects attempts Connection becomes closed.
170
172
MaxReconnects uint
171
- // User name for authorization
173
+ // User name for authorization.
172
174
User string
173
- // Pass is password for authorization
175
+ // Pass is password for authorization.
174
176
Pass string
175
- // RateLimit limits number of 'in-fly' request, ie already put into
177
+ // RateLimit limits number of 'in-fly' request, i.e. already put into
176
178
// requests queue, but not yet answered by server or timeouted.
177
179
// It is disabled by default.
178
180
// See RLimitAction for possible actions when RateLimit.reached.
@@ -191,42 +193,37 @@ type Opts struct {
191
193
// By default it is runtime.GOMAXPROCS(-1) * 4
192
194
Concurrency uint32
193
195
// SkipSchema disables schema loading. Without disabling schema loading,
194
- // there is no way to create Connection for currently not accessible tarantool .
196
+ // there is no way to create Connection for currently not accessible Tarantool .
195
197
SkipSchema bool
196
198
// Notify is a channel which receives notifications about Connection status
197
199
// changes.
198
200
Notify chan <- ConnEvent
199
- // Handle is user specified value, that could be retrivied with Handle() method
201
+ // Handle is user specified value, that could be retrivied with
202
+ // Handle() method.
200
203
Handle interface {}
201
- // Logger is user specified logger used for error messages
204
+ // Logger is user specified logger used for error messages.
202
205
Logger Logger
203
206
}
204
207
205
- // Connect creates and configures new Connection
208
+ // Connect creates and configures a new Connection.
206
209
//
207
210
// Address could be specified in following ways:
208
211
//
209
- // TCP connections:
210
- // - tcp://192.168.1.1:3013
211
- // - tcp://my.host:3013
212
- // - tcp:192.168.1.1:3013
213
- // - tcp:my.host:3013
214
- // - 192.168.1.1:3013
215
- // - my.host:3013
216
- // Unix socket:
217
- // - unix:///abs/path/tnt.sock
218
- // - unix:path/tnt.sock
219
- // - /abs/path/tnt.sock - first '/' indicates unix socket
220
- // - ./rel/path/tnt.sock - first '.' indicates unix socket
221
- // - unix/:path/tnt.sock - 'unix/' acts as a "host" and "/path..." as a port
212
+ // - TCP connections (tcp://192.168.1.1:3013, tcp://my.host:3013,
213
+ // tcp:192.168.1.1:3013, tcp:my.host:3013, 192.168.1.1:3013, my.host:3013)
214
+ //
215
+ // - Unix socket, first '/' or '.' indicates Unix socket
216
+ // (unix:///abs/path/tnt.sock, unix:path/tnt.sock, /abs/path/tnt.sock,
217
+ // ./rel/path/tnt.sock, unix/:path/tnt.sock)
222
218
//
223
- // Note :
219
+ // Notes :
224
220
//
225
221
// - If opts.Reconnect is zero (default), then connection either already connected
226
222
// or error is returned.
227
223
//
228
- // - 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
229
- // and will not end attempts on authorization failures.
224
+ // - If opts.Reconnect is non-zero, then error will be returned only if authorization
225
+ // fails. But if Tarantool is not reachable, then it will make an attempt to reconnect later
226
+ // and will not finish to make attempts on authorization failures.
230
227
func Connect (addr string , opts Opts ) (conn * Connection , err error ) {
231
228
conn = & Connection {
232
229
addr : addr ,
@@ -272,10 +269,10 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
272
269
return nil , err
273
270
} else if ok && (ter .Code == ErrNoSuchUser ||
274
271
ter .Code == ErrPasswordMismatch ) {
275
- /* reported auth errors immediatly */
272
+ // Reported auth errors immediately.
276
273
return nil , err
277
274
} else {
278
- // without SkipSchema it is useless
275
+ // Without SkipSchema it is useless.
279
276
go func (conn * Connection ) {
280
277
conn .mutex .Lock ()
281
278
defer conn .mutex .Unlock ()
@@ -292,7 +289,7 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
292
289
go conn .timeouts ()
293
290
}
294
291
295
- // TODO: reload schema after reconnect
292
+ // TODO: reload schema after reconnect.
296
293
if ! conn .opts .SkipSchema {
297
294
if err = conn .loadSchema (); err != nil {
298
295
conn .mutex .Lock ()
@@ -324,12 +321,12 @@ func (conn *Connection) Close() error {
324
321
return conn .closeConnection (err , true )
325
322
}
326
323
327
- // Addr is configured address of Tarantool socket
324
+ // Addr returns a configured address of Tarantool socket.
328
325
func (conn * Connection ) Addr () string {
329
326
return conn .addr
330
327
}
331
328
332
- // RemoteAddr is address of Tarantool socket
329
+ // RemoteAddr returns an address of Tarantool socket.
333
330
func (conn * Connection ) RemoteAddr () string {
334
331
conn .mutex .Lock ()
335
332
defer conn .mutex .Unlock ()
@@ -339,7 +336,7 @@ func (conn *Connection) RemoteAddr() string {
339
336
return conn .c .RemoteAddr ().String ()
340
337
}
341
338
342
- // LocalAddr is address of outgoing socket
339
+ // LocalAddr returns an address of outgoing socket.
343
340
func (conn * Connection ) LocalAddr () string {
344
341
conn .mutex .Lock ()
345
342
defer conn .mutex .Unlock ()
@@ -349,7 +346,7 @@ func (conn *Connection) LocalAddr() string {
349
346
return conn .c .LocalAddr ().String ()
350
347
}
351
348
352
- // Handle returns user specified handle from Opts
349
+ // Handle returns a user- specified handle from Opts.
353
350
func (conn * Connection ) Handle () interface {} {
354
351
return conn .opts .Handle
355
352
}
@@ -416,7 +413,7 @@ func (conn *Connection) dial() (err error) {
416
413
}
417
414
}
418
415
419
- // Only if connected and authenticated
416
+ // Only if connected and authenticated.
420
417
conn .lockShards ()
421
418
conn .c = connection
422
419
atomic .StoreUint32 (& conn .state , connConnected )
@@ -873,12 +870,12 @@ func (conn *Connection) nextRequestId() (requestId uint32) {
873
870
return atomic .AddUint32 (& conn .requestId , 1 )
874
871
}
875
872
876
- // ConfiguredTimeout returns timeout from connection config
873
+ // ConfiguredTimeout returns a timeout from connection config.
877
874
func (conn * Connection ) ConfiguredTimeout () time.Duration {
878
875
return conn .opts .Timeout
879
876
}
880
877
881
- // OverrideSchema sets Schema for the connection
878
+ // OverrideSchema sets Schema for the connection.
882
879
func (conn * Connection ) OverrideSchema (s * Schema ) {
883
880
if s != nil {
884
881
conn .mutex .Lock ()
0 commit comments