From f5fbce3ee6f78126bd6f8b3a4db5df8158d4c2af Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 14 Nov 2017 18:45:06 +1300 Subject: [PATCH] add a fallback reconnection if no keepalives are recieved within ~24 hours --- HubConnection.go | 15 +++++++++++---- libnmdc.go | 11 ++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/HubConnection.go b/HubConnection.go index 5f3a4b3..2137666 100644 --- a/HubConnection.go +++ b/HubConnection.go @@ -2,6 +2,7 @@ package libnmdc import ( "crypto/tls" + "fmt" "net" "strconv" "strings" @@ -24,10 +25,11 @@ type HubConnection struct { OnEvent chan HubEvent // Private state - conn net.Conn // this is an interface - connValid bool - sentOurHello bool - autoReconnect bool + conn net.Conn // this is an interface + connValid bool + sentOurHello bool + autoReconnect bool + lastDataRecieved time.Time supports map[string]struct{} } @@ -398,6 +400,7 @@ func (this *HubConnection) worker() { } if nbytes > 0 { + this.lastDataRecieved = time.Now() fullBuffer += string(readBuff[0:nbytes]) } } @@ -416,6 +419,10 @@ func (this *HubConnection) worker() { } } + if err == nil && time.Now().Sub(this.lastDataRecieved) > RECONNECT_IF_NO_DATA_RECIEVED_IN { + err = fmt.Errorf("No packets recieved since %s, connection presumed lost", this.lastDataRecieved.Format(time.RFC3339)) + } + // Maybe we disconnected // Perform this check *last*, to ensure we've had a final shot at // clearing out any queued messages diff --git a/libnmdc.go b/libnmdc.go index 74fdb27..225c9b3 100644 --- a/libnmdc.go +++ b/libnmdc.go @@ -9,11 +9,12 @@ import ( ) const ( - DEFAULT_CLIENT_TAG string = "libnmdc.go" - DEFAULT_CLIENT_VERSION string = "0.11" - DEFAULT_HUB_NAME string = "(unknown)" - SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second - AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second + DEFAULT_CLIENT_TAG string = "libnmdc.go" + DEFAULT_CLIENT_VERSION string = "0.11" + DEFAULT_HUB_NAME string = "(unknown)" + SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second + AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second + RECONNECT_IF_NO_DATA_RECIEVED_IN time.Duration = 24 * time.Hour // we expect keepalives wayyyy more frequently than this ) var rx_protocolMessage *regexp.Regexp