@@ -258,12 +258,15 @@ func (c *client) Connect() Token {
258258 return
259259 }
260260
261+ var attemptCount int
262+
261263 RETRYCONN:
262264 var conn net.Conn
263265 var rc byte
264266 var err error
265- conn , rc , t .sessionPresent , err = c .attemptConnection ()
267+ conn , rc , t .sessionPresent , err = c .attemptConnection (false , attemptCount )
266268 if err != nil {
269+ attemptCount ++
267270 if c .options .ConnectRetry {
268271 DEBUG .Println (CLI , "Connect failed, sleeping for" , int (c .options .ConnectRetryInterval .Seconds ()), "seconds and will then retry, error:" , err .Error ())
269272 time .Sleep (c .options .ConnectRetryInterval )
@@ -315,15 +318,17 @@ func (c *client) reconnect(connectionUp connCompletedFn) {
315318 DEBUG .Println (CLI , "Detect continual connection lost after reconnect, slept for" , int (slp .Seconds ()), "seconds" )
316319 }
317320
321+ var attemptCount int
318322 for {
319323 if nil != c .options .OnReconnecting {
320324 c .options .OnReconnecting (c , & c .options )
321325 }
322326 var err error
323- conn , _ , _ , err = c .attemptConnection ()
327+ conn , _ , _ , err = c .attemptConnection (true , attemptCount )
324328 if err == nil {
325329 break
326330 }
331+ attemptCount ++
327332 sleep , _ := c .backoff .sleepWithBackoff ("attemptReconnection" , initSleep , c .options .MaxReconnectInterval , c .options .ConnectTimeout , false )
328333 DEBUG .Println (CLI , "Reconnect failed, slept for" , int (sleep .Seconds ()), "seconds:" , err )
329334
@@ -351,7 +356,7 @@ func (c *client) reconnect(connectionUp connCompletedFn) {
351356// byte - Return code (packets.Accepted indicates a successful connection).
352357// bool - SessionPresent flag from the connect ack (only valid if packets.Accepted)
353358// err - Error (err != nil guarantees that conn has been set to active connection).
354- func (c * client ) attemptConnection () (net.Conn , byte , bool , error ) {
359+ func (c * client ) attemptConnection (isReconnect bool , attempt int ) (net.Conn , byte , bool , error ) {
355360 protocolVersion := c .options .ProtocolVersion
356361 var (
357362 sessionPresent bool
@@ -360,6 +365,10 @@ func (c *client) attemptConnection() (net.Conn, byte, bool, error) {
360365 rc byte
361366 )
362367
368+ if c .options .OnConnectionNotification != nil {
369+ c .options .OnConnectionNotification (c , ConnectionNotificationConnecting {isReconnect , attempt })
370+ }
371+
363372 c .optionsMu .Lock () // Protect c.options.Servers so that servers can be added in test cases
364373 brokers := c .options .Servers
365374 c .optionsMu .Unlock ()
@@ -372,6 +381,9 @@ func (c *client) attemptConnection() (net.Conn, byte, bool, error) {
372381 DEBUG .Println (CLI , "using custom onConnectAttempt handler..." )
373382 tlsCfg = c .options .OnConnectAttempt (broker , c .options .TLSConfig )
374383 }
384+ if c .options .OnConnectionNotification != nil {
385+ c .options .OnConnectionNotification (c , ConnectionNotificationBroker {broker })
386+ }
375387 connDeadline := time .Now ().Add (c .options .ConnectTimeout ) // Time by which connection must be established
376388 dialer := c .options .Dialer
377389 if dialer == nil { //
@@ -388,6 +400,9 @@ func (c *client) attemptConnection() (net.Conn, byte, bool, error) {
388400 ERROR .Println (CLI , err .Error ())
389401 WARN .Println (CLI , "failed to connect to broker, trying next" )
390402 rc = packets .ErrNetworkError
403+ if c .options .OnConnectionNotification != nil {
404+ c .options .OnConnectionNotification (c , ConnectionNotificationBrokerFailed {broker , err })
405+ }
391406 continue
392407 }
393408 DEBUG .Println (CLI , "socket connected to broker" )
@@ -430,6 +445,9 @@ func (c *client) attemptConnection() (net.Conn, byte, bool, error) {
430445 err = fmt .Errorf ("%w : %w" , packets .ConnErrors [rc ], err )
431446 }
432447 }
448+ if err != nil && c .options .OnConnectionNotification != nil {
449+ c .options .OnConnectionNotification (c , ConnectionNotificationFailed {err })
450+ }
433451 return conn , rc , sessionPresent , err
434452}
435453
@@ -564,6 +582,9 @@ func (c *client) internalConnLost(whyConnLost error) {
564582 if c .options .OnConnectionLost != nil {
565583 go c .options .OnConnectionLost (c , whyConnLost )
566584 }
585+ if c .options .OnConnectionNotification != nil {
586+ go c .options .OnConnectionNotification (c , ConnectionNotificationLost {whyConnLost })
587+ }
567588 DEBUG .Println (CLI , "internalConnLost complete" )
568589 }()
569590}
@@ -613,6 +634,9 @@ func (c *client) startCommsWorkers(conn net.Conn, connectionUp connCompletedFn,
613634 if c .options .OnConnect != nil {
614635 go c .options .OnConnect (c )
615636 }
637+ if c .options .OnConnectionNotification != nil {
638+ go c .options .OnConnectionNotification (c , ConnectionNotificationConnected {})
639+ }
616640
617641 // c.oboundP and c.obound need to stay active for the life of the client because, depending upon the options,
618642 // messages may be published while the client is disconnected (they will block unless in a goroutine). However
0 commit comments