add a fallback reconnection if no keepalives are recieved within ~24 hours

This commit is contained in:
mappu 2017-11-14 18:45:06 +13:00
parent 2cbc8f8496
commit f5fbce3ee6
2 changed files with 17 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package libnmdc
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"net" "net"
"strconv" "strconv"
"strings" "strings"
@ -28,6 +29,7 @@ type HubConnection struct {
connValid bool connValid bool
sentOurHello bool sentOurHello bool
autoReconnect bool autoReconnect bool
lastDataRecieved time.Time
supports map[string]struct{} supports map[string]struct{}
} }
@ -398,6 +400,7 @@ func (this *HubConnection) worker() {
} }
if nbytes > 0 { if nbytes > 0 {
this.lastDataRecieved = time.Now()
fullBuffer += string(readBuff[0:nbytes]) 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 // Maybe we disconnected
// Perform this check *last*, to ensure we've had a final shot at // Perform this check *last*, to ensure we've had a final shot at
// clearing out any queued messages // clearing out any queued messages

View File

@ -14,6 +14,7 @@ const (
DEFAULT_HUB_NAME string = "(unknown)" DEFAULT_HUB_NAME string = "(unknown)"
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
AUTO_RECONNECT_AFTER time.Duration = 30 * 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 var rx_protocolMessage *regexp.Regexp