@@ -92,12 +92,24 @@ func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interfac
92
92
case LogReconnectFailed :
93
93
reconnects := v [0 ].(uint )
94
94
err := v [1 ].(error )
95
- log .Printf ("tarantool: reconnect (%d/%d) to %s failed: %s" ,
96
- reconnects , conn .opts .MaxReconnects , conn .Addr (), err )
95
+ addr := conn .Addr ()
96
+ if addr == nil {
97
+ log .Printf ("tarantool: connect (%d/%d) failed: %s" ,
98
+ reconnects , conn .opts .MaxReconnects , err )
99
+ } else {
100
+ log .Printf ("tarantool: reconnect (%d/%d) to %s failed: %s" ,
101
+ reconnects , conn .opts .MaxReconnects , addr , err )
102
+ }
97
103
case LogLastReconnectFailed :
98
104
err := v [0 ].(error )
99
- log .Printf ("tarantool: last reconnect to %s failed: %s, giving it up" ,
100
- conn .Addr (), err )
105
+ addr := conn .Addr ()
106
+ if addr == nil {
107
+ log .Printf ("tarantool: last connect failed: %s, giving it up" ,
108
+ err )
109
+ } else {
110
+ log .Printf ("tarantool: last reconnect to %s failed: %s, giving it up" ,
111
+ addr , err )
112
+ }
101
113
case LogUnexpectedResultId :
102
114
header := v [0 ].(Header )
103
115
log .Printf ("tarantool: connection %s got unexpected request ID (%d) in response " +
@@ -362,8 +374,21 @@ func Connect(ctx context.Context, dialer Dialer, opts Opts) (conn *Connection, e
362
374
363
375
conn .cond = sync .NewCond (& conn .mutex )
364
376
365
- if err = conn .createConnection (ctx ); err != nil {
366
- return nil , err
377
+ if conn .opts .Reconnect > 0 {
378
+ // We don't need these mutex.Lock()/mutex.Unlock() here, but
379
+ // runReconnects() expects mutex.Lock() to be set, so it's
380
+ // easier to add them instead of reworking runReconnects().
381
+ conn .mutex .Lock ()
382
+ err = conn .runReconnects (ctx )
383
+ conn .mutex .Unlock ()
384
+ if err != nil {
385
+ conn .closeConnection (err , true )
386
+ return conn , err
387
+ }
388
+ } else {
389
+ if err = conn .connect (ctx ); err != nil {
390
+ return nil , err
391
+ }
367
392
}
368
393
369
394
go conn .pinger ()
@@ -553,7 +578,7 @@ func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32,
553
578
return
554
579
}
555
580
556
- func (conn * Connection ) createConnection (ctx context.Context ) error {
581
+ func (conn * Connection ) connect (ctx context.Context ) error {
557
582
var err error
558
583
if conn .c == nil && conn .state == connDisconnected {
559
584
if err = conn .dial (ctx ); err == nil {
@@ -616,19 +641,22 @@ func (conn *Connection) getDialTimeout() time.Duration {
616
641
return dialTimeout
617
642
}
618
643
619
- func (conn * Connection ) runReconnects () error {
644
+ func (conn * Connection ) runReconnects (ctx context. Context ) error {
620
645
dialTimeout := conn .getDialTimeout ()
621
646
var reconnects uint
622
647
var err error
623
648
649
+ t := time .NewTicker (conn .opts .Reconnect )
650
+ defer t .Stop ()
624
651
for conn .opts .MaxReconnects == 0 || reconnects <= conn .opts .MaxReconnects {
625
- now := time .Now ()
626
-
627
- ctx , cancel := context .WithTimeout (context .Background (), dialTimeout )
628
- err = conn .createConnection (ctx )
652
+ localCtx , cancel := context .WithTimeout (ctx , dialTimeout )
653
+ err = conn .connect (localCtx )
629
654
cancel ()
630
655
631
656
if err != nil {
657
+ if ctx .Err () != nil {
658
+ return err
659
+ }
632
660
if clientErr , ok := err .(ClientError ); ok &&
633
661
clientErr .Code == ErrConnectionClosed {
634
662
return err
@@ -642,7 +670,12 @@ func (conn *Connection) runReconnects() error {
642
670
reconnects ++
643
671
conn .mutex .Unlock ()
644
672
645
- time .Sleep (time .Until (now .Add (conn .opts .Reconnect )))
673
+ select {
674
+ case <- ctx .Done ():
675
+ // Since the context is cancelled, we don't need to do anything.
676
+ // Conn.connect() will return the correct error.
677
+ case <- t .C :
678
+ }
646
679
647
680
conn .mutex .Lock ()
648
681
}
@@ -656,7 +689,7 @@ func (conn *Connection) reconnectImpl(neterr error, c Conn) {
656
689
if conn .opts .Reconnect > 0 {
657
690
if c == conn .c {
658
691
conn .closeConnection (neterr , false )
659
- if err := conn .runReconnects (); err != nil {
692
+ if err := conn .runReconnects (context . Background () ); err != nil {
660
693
conn .closeConnection (err , true )
661
694
}
662
695
}
0 commit comments