@@ -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
// schemaResolver contains a SchemaResolver implementation.
164
166
schemaResolver SchemaResolver
165
167
// requestId contains the last request ID for requests with nil context.
@@ -260,11 +262,6 @@ const (
260
262
261
263
// Opts is a way to configure Connection
262
264
type Opts struct {
263
- // Auth is an authentication method.
264
- Auth Auth
265
- // Dialer is a Dialer object used to create a new connection to a
266
- // Tarantool instance. TtDialer is a default one.
267
- Dialer Dialer
268
265
// Timeout for response to a particular request. The timeout is reset when
269
266
// push messages are received. If Timeout is zero, any request can be
270
267
// blocked infinitely.
@@ -287,10 +284,6 @@ type Opts struct {
287
284
// endlessly.
288
285
// After MaxReconnects attempts Connection becomes closed.
289
286
MaxReconnects uint
290
- // Username for logging in to Tarantool.
291
- User string
292
- // User password for logging in to Tarantool.
293
- Pass string
294
287
// RateLimit limits number of 'in-fly' request, i.e. already put into
295
288
// requests queue, but not yet answered by server or timeouted.
296
289
// It is disabled by default.
@@ -315,83 +308,23 @@ type Opts struct {
315
308
Handle interface {}
316
309
// Logger is user specified logger used for error messages.
317
310
Logger Logger
318
- // Transport is the connection type, by default the connection is unencrypted.
319
- Transport string
320
- // SslOpts is used only if the Transport == 'ssl' is set.
321
- Ssl SslOpts
322
- // RequiredProtocolInfo contains minimal protocol version and
323
- // list of protocol features that should be supported by
324
- // Tarantool server. By default there are no restrictions.
325
- RequiredProtocolInfo ProtocolInfo
326
- }
327
-
328
- // SslOpts is a way to configure ssl transport.
329
- type SslOpts struct {
330
- // KeyFile is a path to a private SSL key file.
331
- KeyFile string
332
- // CertFile is a path to an SSL certificate file.
333
- CertFile string
334
- // CaFile is a path to a trusted certificate authorities (CA) file.
335
- CaFile string
336
- // Ciphers is a colon-separated (:) list of SSL cipher suites the connection
337
- // can use.
338
- //
339
- // We don't provide a list of supported ciphers. This is what OpenSSL
340
- // does. The only limitation is usage of TLSv1.2 (because other protocol
341
- // versions don't seem to support the GOST cipher). To add additional
342
- // ciphers (GOST cipher), you must configure OpenSSL.
343
- //
344
- // See also
345
- //
346
- // * https://www.openssl.org/docs/man1.1.1/man1/ciphers.html
347
- Ciphers string
348
- // Password is a password for decrypting the private SSL key file.
349
- // The priority is as follows: try to decrypt with Password, then
350
- // try PasswordFile.
351
- Password string
352
- // PasswordFile is a path to the list of passwords for decrypting
353
- // the private SSL key file. The connection tries every line from the
354
- // file as a password.
355
- PasswordFile string
356
- }
357
-
358
- // Clone returns a copy of the Opts object.
359
- // Any changes in copy RequiredProtocolInfo will not affect the original
360
- // RequiredProtocolInfo value.
361
- func (opts Opts ) Clone () Opts {
362
- optsCopy := opts
363
- optsCopy .RequiredProtocolInfo = opts .RequiredProtocolInfo .Clone ()
364
-
365
- return optsCopy
366
311
}
367
312
368
313
// Connect creates and configures a new Connection.
369
- //
370
- // Address could be specified in following ways:
371
- //
372
- // - TCP connections (tcp://192.168.1.1:3013, tcp://my.host:3013,
373
- // tcp:192.168.1.1:3013, tcp:my.host:3013, 192.168.1.1:3013, my.host:3013)
374
- //
375
- // - Unix socket, first '/' or '.' indicates Unix socket
376
- // (unix:///abs/path/tnt.sock, unix:path/tnt.sock, /abs/path/tnt.sock,
377
- // ./rel/path/tnt.sock, unix/:path/tnt.sock)
378
- func Connect (ctx context.Context , addr string , opts Opts ) (conn * Connection , err error ) {
314
+ func Connect (ctx context.Context , dialer Dialer , opts Opts ) (conn * Connection , err error ) {
379
315
conn = & Connection {
380
- addr : addr ,
316
+ dialer : dialer ,
381
317
requestId : 0 ,
382
318
contextRequestId : 1 ,
383
319
Greeting : & Greeting {},
384
320
control : make (chan struct {}),
385
- opts : opts . Clone () ,
321
+ opts : opts ,
386
322
dec : msgpack .NewDecoder (& smallBuf {}),
387
323
}
388
324
maxprocs := uint32 (runtime .GOMAXPROCS (- 1 ))
389
325
if conn .opts .Concurrency == 0 || conn .opts .Concurrency > maxprocs * 128 {
390
326
conn .opts .Concurrency = maxprocs * 4
391
327
}
392
- if conn .opts .Dialer == nil {
393
- conn .opts .Dialer = TtDialer {}
394
- }
395
328
if c := conn .opts .Concurrency ; c & (c - 1 ) != 0 {
396
329
for i := uint (1 ); i < 32 ; i *= 2 {
397
330
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
@@ -1453,8 +1361,7 @@ func isFeatureInSlice(expected iproto.Feature, actualSlice []iproto.Feature) boo
1453
1361
1454
1362
// NewWatcher creates a new Watcher object for the connection.
1455
1363
//
1456
- // You need to require IPROTO_FEATURE_WATCHERS to use watchers, see examples
1457
- // for the function.
1364
+ // Server must support IPROTO_FEATURE_WATCHERS to use watchers.
1458
1365
//
1459
1366
// After watcher creation, the watcher callback is invoked for the first time.
1460
1367
// In this case, the callback is triggered whether or not the key has already
@@ -1490,9 +1397,9 @@ func (conn *Connection) NewWatcher(key string, callback WatchCallback) (Watcher,
1490
1397
// That's why we can't just check the Tarantool response for an unsupported
1491
1398
// request error.
1492
1399
if ! isFeatureInSlice (iproto .IPROTO_FEATURE_WATCHERS ,
1493
- conn .opts . RequiredProtocolInfo .Features ) {
1494
- err := fmt .Errorf ("the feature %s must be required by connection " +
1495
- "options to create a watcher" , iproto .IPROTO_FEATURE_WATCHERS )
1400
+ conn .c . ProtocolInfo () .Features ) {
1401
+ err := fmt .Errorf ("the feature %s must be supported by connection " +
1402
+ "to create a watcher" , iproto .IPROTO_FEATURE_WATCHERS )
1496
1403
return nil , err
1497
1404
}
1498
1405
@@ -1583,23 +1490,14 @@ func (conn *Connection) newWatcherImpl(key string, callback WatchCallback) (Watc
1583
1490
}, nil
1584
1491
}
1585
1492
1586
- // ServerProtocolVersion returns protocol version and protocol features
1493
+ // ProtocolInfo returns protocol version and protocol features
1587
1494
// supported by connected Tarantool server. Beware that values might be
1588
1495
// outdated if connection is in a disconnected state.
1589
- // Since 1.10 .0
1590
- func (conn * Connection ) ServerProtocolInfo () ProtocolInfo {
1496
+ // Since 2.0 .0
1497
+ func (conn * Connection ) ProtocolInfo () ProtocolInfo {
1591
1498
return conn .serverProtocolInfo .Clone ()
1592
1499
}
1593
1500
1594
- // ClientProtocolVersion returns protocol version and protocol features
1595
- // supported by Go connection client.
1596
- // Since 1.10.0
1597
- func (conn * Connection ) ClientProtocolInfo () ProtocolInfo {
1598
- info := clientProtocolInfo .Clone ()
1599
- info .Auth = conn .opts .Auth
1600
- return info
1601
- }
1602
-
1603
1501
func shutdownEventCallback (event WatchEvent ) {
1604
1502
// Receives "true" on server shutdown.
1605
1503
// See https://www.tarantool.io/en/doc/latest/dev_guide/internals/iproto/graceful_shutdown/
0 commit comments