@@ -10,6 +10,7 @@ import (
10
10
"io"
11
11
"log"
12
12
"math"
13
+ "net"
13
14
"runtime"
14
15
"sync"
15
16
"sync/atomic"
@@ -90,15 +91,15 @@ func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interfac
90
91
reconnects := v [0 ].(uint )
91
92
err := v [1 ].(error )
92
93
log .Printf ("tarantool: reconnect (%d/%d) to %s failed: %s" ,
93
- reconnects , conn .opts .MaxReconnects , conn .addr , err )
94
+ reconnects , conn .opts .MaxReconnects , conn .Addr () , err )
94
95
case LogLastReconnectFailed :
95
96
err := v [0 ].(error )
96
97
log .Printf ("tarantool: last reconnect to %s failed: %s, giving it up" ,
97
- conn .addr , err )
98
+ conn .Addr () , err )
98
99
case LogUnexpectedResultId :
99
100
resp := v [0 ].(* Response )
100
101
log .Printf ("tarantool: connection %s got unexpected resultId (%d) in response" ,
101
- conn .addr , resp .RequestId )
102
+ conn .Addr () , resp .RequestId )
102
103
case LogWatchEventReadFailed :
103
104
err := v [0 ].(error )
104
105
log .Printf ("tarantool: unable to parse watch event: %s" , err )
@@ -156,10 +157,11 @@ func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interfac
156
157
// More on graceful shutdown:
157
158
// https://www.tarantool.io/en/doc/latest/dev_guide/internals/iproto/graceful_shutdown/
158
159
type Connection struct {
159
- addr string
160
- c Conn
161
- mutex sync.Mutex
162
- cond * sync.Cond
160
+ addr net.Addr
161
+ dialer Dialer
162
+ c Conn
163
+ mutex sync.Mutex
164
+ cond * sync.Cond
163
165
// Schema contains schema loaded on connection.
164
166
Schema * Schema
165
167
// schemaResolver contains a SchemaResolver implementation.
@@ -262,11 +264,6 @@ const (
262
264
263
265
// Opts is a way to configure Connection
264
266
type Opts struct {
265
- // Auth is an authentication method.
266
- Auth Auth
267
- // Dialer is a Dialer object used to create a new connection to a
268
- // Tarantool instance. TtDialer is a default one.
269
- Dialer Dialer
270
267
// Timeout for response to a particular request. The timeout is reset when
271
268
// push messages are received. If Timeout is zero, any request can be
272
269
// blocked infinitely.
@@ -289,10 +286,6 @@ type Opts struct {
289
286
// endlessly.
290
287
// After MaxReconnects attempts Connection becomes closed.
291
288
MaxReconnects uint
292
- // Username for logging in to Tarantool.
293
- User string
294
- // User password for logging in to Tarantool.
295
- Pass string
296
289
// RateLimit limits number of 'in-fly' request, i.e. already put into
297
290
// requests queue, but not yet answered by server or timeouted.
298
291
// It is disabled by default.
@@ -317,83 +310,23 @@ type Opts struct {
317
310
Handle interface {}
318
311
// Logger is user specified logger used for error messages.
319
312
Logger Logger
320
- // Transport is the connection type, by default the connection is unencrypted.
321
- Transport string
322
- // SslOpts is used only if the Transport == 'ssl' is set.
323
- Ssl SslOpts
324
- // RequiredProtocolInfo contains minimal protocol version and
325
- // list of protocol features that should be supported by
326
- // Tarantool server. By default there are no restrictions.
327
- RequiredProtocolInfo ProtocolInfo
328
- }
329
-
330
- // SslOpts is a way to configure ssl transport.
331
- type SslOpts struct {
332
- // KeyFile is a path to a private SSL key file.
333
- KeyFile string
334
- // CertFile is a path to an SSL certificate file.
335
- CertFile string
336
- // CaFile is a path to a trusted certificate authorities (CA) file.
337
- CaFile string
338
- // Ciphers is a colon-separated (:) list of SSL cipher suites the connection
339
- // can use.
340
- //
341
- // We don't provide a list of supported ciphers. This is what OpenSSL
342
- // does. The only limitation is usage of TLSv1.2 (because other protocol
343
- // versions don't seem to support the GOST cipher). To add additional
344
- // ciphers (GOST cipher), you must configure OpenSSL.
345
- //
346
- // See also
347
- //
348
- // * https://www.openssl.org/docs/man1.1.1/man1/ciphers.html
349
- Ciphers string
350
- // Password is a password for decrypting the private SSL key file.
351
- // The priority is as follows: try to decrypt with Password, then
352
- // try PasswordFile.
353
- Password string
354
- // PasswordFile is a path to the list of passwords for decrypting
355
- // the private SSL key file. The connection tries every line from the
356
- // file as a password.
357
- PasswordFile string
358
- }
359
-
360
- // Clone returns a copy of the Opts object.
361
- // Any changes in copy RequiredProtocolInfo will not affect the original
362
- // RequiredProtocolInfo value.
363
- func (opts Opts ) Clone () Opts {
364
- optsCopy := opts
365
- optsCopy .RequiredProtocolInfo = opts .RequiredProtocolInfo .Clone ()
366
-
367
- return optsCopy
368
313
}
369
314
370
315
// Connect creates and configures a new Connection.
371
- //
372
- // Address could be specified in following ways:
373
- //
374
- // - TCP connections (tcp://192.168.1.1:3013, tcp://my.host:3013,
375
- // tcp:192.168.1.1:3013, tcp:my.host:3013, 192.168.1.1:3013, my.host:3013)
376
- //
377
- // - Unix socket, first '/' or '.' indicates Unix socket
378
- // (unix:///abs/path/tnt.sock, unix:path/tnt.sock, /abs/path/tnt.sock,
379
- // ./rel/path/tnt.sock, unix/:path/tnt.sock)
380
- func Connect (ctx context.Context , addr string , opts Opts ) (conn * Connection , err error ) {
316
+ func Connect (ctx context.Context , dialer Dialer , opts Opts ) (conn * Connection , err error ) {
381
317
conn = & Connection {
382
- addr : addr ,
318
+ dialer : dialer ,
383
319
requestId : 0 ,
384
320
contextRequestId : 1 ,
385
321
Greeting : & Greeting {},
386
322
control : make (chan struct {}),
387
- opts : opts . Clone () ,
323
+ opts : opts ,
388
324
dec : msgpack .NewDecoder (& smallBuf {}),
389
325
}
390
326
maxprocs := uint32 (runtime .GOMAXPROCS (- 1 ))
391
327
if conn .opts .Concurrency == 0 || conn .opts .Concurrency > maxprocs * 128 {
392
328
conn .opts .Concurrency = maxprocs * 4
393
329
}
394
- if conn .opts .Dialer == nil {
395
- conn .opts .Dialer = TtDialer {}
396
- }
397
330
if c := conn .opts .Concurrency ; c & (c - 1 ) != 0 {
398
331
for i := uint (1 ); i < 32 ; i *= 2 {
399
332
c |= c >> i
@@ -474,30 +407,10 @@ func (conn *Connection) CloseGraceful() error {
474
407
}
475
408
476
409
// Addr returns a configured address of Tarantool socket.
477
- func (conn * Connection ) Addr () string {
410
+ func (conn * Connection ) Addr () net. Addr {
478
411
return conn .addr
479
412
}
480
413
481
- // RemoteAddr returns an address of Tarantool socket.
482
- func (conn * Connection ) RemoteAddr () string {
483
- conn .mutex .Lock ()
484
- defer conn .mutex .Unlock ()
485
- if conn .c == nil {
486
- return ""
487
- }
488
- return conn .c .RemoteAddr ().String ()
489
- }
490
-
491
- // LocalAddr returns an address of outgoing socket.
492
- func (conn * Connection ) LocalAddr () string {
493
- conn .mutex .Lock ()
494
- defer conn .mutex .Unlock ()
495
- if conn .c == nil {
496
- return ""
497
- }
498
- return conn .c .LocalAddr ().String ()
499
- }
500
-
501
414
// Handle returns a user-specified handle from Opts.
502
415
func (conn * Connection ) Handle () interface {} {
503
416
return conn .opts .Handle
@@ -514,19 +427,14 @@ func (conn *Connection) dial(ctx context.Context) error {
514
427
opts := conn .opts
515
428
516
429
var c Conn
517
- c , err := conn .opts .Dialer .Dial (ctx , conn .addr , DialOpts {
518
- IoTimeout : opts .Timeout ,
519
- Transport : opts .Transport ,
520
- Ssl : opts .Ssl ,
521
- RequiredProtocol : opts .RequiredProtocolInfo ,
522
- Auth : opts .Auth ,
523
- User : opts .User ,
524
- Password : opts .Pass ,
430
+ c , err := conn .dialer .Dial (ctx , DialOpts {
431
+ IoTimeout : opts .Timeout ,
525
432
})
526
433
if err != nil {
527
434
return err
528
435
}
529
436
437
+ conn .addr = c .Addr ()
530
438
conn .Greeting .Version = c .Greeting ().Version
531
439
conn .serverProtocolInfo = c .ProtocolInfo ()
532
440
@@ -1447,8 +1355,7 @@ func isFeatureInSlice(expected iproto.Feature, actualSlice []iproto.Feature) boo
1447
1355
1448
1356
// NewWatcher creates a new Watcher object for the connection.
1449
1357
//
1450
- // You need to require IPROTO_FEATURE_WATCHERS to use watchers, see examples
1451
- // for the function.
1358
+ // Server must support IPROTO_FEATURE_WATCHERS to use watchers.
1452
1359
//
1453
1360
// After watcher creation, the watcher callback is invoked for the first time.
1454
1361
// In this case, the callback is triggered whether or not the key has already
@@ -1484,9 +1391,9 @@ func (conn *Connection) NewWatcher(key string, callback WatchCallback) (Watcher,
1484
1391
// That's why we can't just check the Tarantool response for an unsupported
1485
1392
// request error.
1486
1393
if ! isFeatureInSlice (iproto .IPROTO_FEATURE_WATCHERS ,
1487
- conn .opts . RequiredProtocolInfo .Features ) {
1488
- err := fmt .Errorf ("the feature %s must be required by connection " +
1489
- "options to create a watcher" , iproto .IPROTO_FEATURE_WATCHERS )
1394
+ conn .c . ProtocolInfo () .Features ) {
1395
+ err := fmt .Errorf ("the feature %s must be supported by connection " +
1396
+ "to create a watcher" , iproto .IPROTO_FEATURE_WATCHERS )
1490
1397
return nil , err
1491
1398
}
1492
1399
@@ -1577,23 +1484,14 @@ func (conn *Connection) newWatcherImpl(key string, callback WatchCallback) (Watc
1577
1484
}, nil
1578
1485
}
1579
1486
1580
- // ServerProtocolVersion returns protocol version and protocol features
1487
+ // ProtocolInfo returns protocol version and protocol features
1581
1488
// supported by connected Tarantool server. Beware that values might be
1582
1489
// outdated if connection is in a disconnected state.
1583
- // Since 1.10 .0
1584
- func (conn * Connection ) ServerProtocolInfo () ProtocolInfo {
1490
+ // Since 2.0 .0
1491
+ func (conn * Connection ) ProtocolInfo () ProtocolInfo {
1585
1492
return conn .serverProtocolInfo .Clone ()
1586
1493
}
1587
1494
1588
- // ClientProtocolVersion returns protocol version and protocol features
1589
- // supported by Go connection client.
1590
- // Since 1.10.0
1591
- func (conn * Connection ) ClientProtocolInfo () ProtocolInfo {
1592
- info := clientProtocolInfo .Clone ()
1593
- info .Auth = conn .opts .Auth
1594
- return info
1595
- }
1596
-
1597
1495
func shutdownEventCallback (event WatchEvent ) {
1598
1496
// Receives "true" on server shutdown.
1599
1497
// See https://www.tarantool.io/en/doc/latest/dev_guide/internals/iproto/graceful_shutdown/
0 commit comments