@@ -454,26 +454,30 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
454
454
455
455
func (conn * Connection ) shutdownOnServerEvent () {
456
456
conn .mutex .Lock ()
457
- defer conn .mutex .Unlock ()
458
457
459
- fmt .Printf ("starting shutdown...\n " )
458
+ fmt .Printf ("starting shutdown... (%v) \n " , conn . UniqueId () )
460
459
conn .shutdownOn .Store (true )
461
460
defer conn .shutdownOn .Store (false )
462
461
463
- fmt .Printf ("wait for futures to finish...\n " )
462
+ fmt .Printf ("wait for futures to finish... (%v) \n " , conn . UniqueId () )
464
463
conn .shutdownWg .Wait ()
465
464
466
465
// fmt.Printf("defer unregister watcher...\n")
467
466
// defer conn.shutdownWatcher.Unregister()
468
467
469
- fmt .Printf ("close the connection...\n " )
470
- conn .closeConnection (
471
- ClientError {
472
- ErrConnectionClosed ,
473
- "connection closed after server shutdown" ,
474
- }, false )
468
+ err := ClientError {
469
+ ErrConnectionClosed ,
470
+ "connection closed after server shutdown" ,
471
+ }
472
+
473
+ fmt .Printf ("close the connection... (%v)\n " , conn .UniqueId ())
474
+ conn .closeConnection (err , false )
475
+
476
+ fmt .Printf ("finish shutdown... (%v)\n " , conn .UniqueId ())
475
477
476
- fmt .Printf ("finish shutdown...\n " )
478
+ conn .mutex .Unlock ()
479
+
480
+ defer conn .reconnect (err , conn .c )
477
481
}
478
482
479
483
// ConnectedNow reports if connection is established at the moment.
@@ -779,7 +783,10 @@ func (conn *Connection) readIdResponse(r io.Reader) (Response, error) {
779
783
780
784
func (conn * Connection ) createConnection (reconnect bool ) (err error ) {
781
785
var reconnects uint
786
+ fmt .Printf ("conn.state is %v (%v)\n " , conn .state , conn .UniqueId ())
782
787
for conn .c == nil && conn .state == connDisconnected {
788
+ fmt .Printf ("createConnection iteration (%v)\n " , conn .UniqueId ())
789
+
783
790
now := time .Now ()
784
791
err = conn .dial ()
785
792
if err == nil || ! reconnect {
@@ -797,9 +804,12 @@ func (conn *Connection) createConnection(reconnect bool) (err error) {
797
804
conn .opts .Logger .Report (LogReconnectFailed , conn , reconnects , err )
798
805
conn .notify (ReconnectFailed )
799
806
reconnects ++
807
+
808
+ fmt .Printf ("until sleep %v (%v)\n " , time .Now (), conn .UniqueId ())
800
809
conn .mutex .Unlock ()
801
810
time .Sleep (time .Until (now .Add (conn .opts .Reconnect )))
802
811
conn .mutex .Lock ()
812
+ fmt .Printf ("after sleep %v (%v)\n " , time .Now (), conn .UniqueId ())
803
813
}
804
814
if conn .state == connClosed {
805
815
err = ClientError {ErrConnectionClosed , "using closed connection" }
@@ -836,19 +846,29 @@ func (conn *Connection) closeConnection(neterr error, forever bool) (err error)
836
846
return
837
847
}
838
848
849
+ func (conn * Connection ) UniqueId () string {
850
+ return fmt .Sprintf ("conn %v" , conn .control )
851
+ }
852
+
839
853
func (conn * Connection ) reconnect (neterr error , c net.Conn ) {
854
+ fmt .Printf ("reconnect head (%v)\n " , conn .UniqueId ())
840
855
conn .mutex .Lock ()
841
856
defer conn .mutex .Unlock ()
857
+ fmt .Printf ("conn.opts.Reconnect %d (%v)\n " , conn .opts .Reconnect , conn .UniqueId ())
842
858
if conn .opts .Reconnect > 0 {
843
859
if c == conn .c {
860
+ fmt .Printf ("reconnect attempt (%v)\n " , conn .UniqueId ())
844
861
conn .closeConnection (neterr , false )
845
862
if err := conn .createConnection (true ); err != nil {
863
+ fmt .Printf ("kill that one (folded)(%v)\n " , conn .UniqueId ())
846
864
conn .closeConnection (err , true )
847
865
}
848
866
}
849
867
} else {
868
+ fmt .Printf ("kill that one (%v)\n " , conn .UniqueId ())
850
869
conn .closeConnection (neterr , true )
851
870
}
871
+ fmt .Printf ("reconnect end (%v)\n " , conn .UniqueId ())
852
872
}
853
873
854
874
func (conn * Connection ) lockShards () {
@@ -1166,11 +1186,11 @@ func (conn *Connection) send(req Request, streamId uint64) *Future {
1166
1186
}
1167
1187
1168
1188
func (conn * Connection ) gracefulWait (fut * Future ) {
1169
- fmt .Printf ("start tracking %v\n " , fut )
1189
+ // fmt.Printf("start tracking %v\n", fut)
1170
1190
<- fut .done
1171
- fmt .Printf ("done tracking %v\n " , fut )
1191
+ // fmt.Printf("done tracking %v\n", fut)
1172
1192
conn .shutdownWg .Done ()
1173
- fmt .Printf ("wg done %v\n " , fut )
1193
+ // fmt.Printf("wg done %v\n", fut)
1174
1194
}
1175
1195
1176
1196
func (conn * Connection ) putFuture (fut * Future , req Request , streamId uint64 ) {
@@ -1619,7 +1639,7 @@ func (conn *Connection) NewWatcher(key string, callback WatchCallback) (Watcher,
1619
1639
1620
1640
if sendAck {
1621
1641
fut := conn .Do (newWatchRequest (key ))
1622
- fmt .Printf ("ack request %v %v: %v\n " , key , state .value , fut )
1642
+ // fmt.Printf("ack request %v %v: %v\n", key, state.value, fut)
1623
1643
fut .Get ()
1624
1644
// We expect a reconnect and re-subscribe if it fails to
1625
1645
// send the watch request. So it looks ok do not check a
0 commit comments