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 (
"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

View File

@ -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