From 6c2b9d763671c5406f1a45f1c959da3f4092f1f3 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 25 Sep 2019 12:14:09 +0100 Subject: [PATCH 1/2] Keep track of errors and delete beyond error count > 3 --- tunnel/default.go | 28 ++++++++++++++++++++++++++++ tunnel/link.go | 25 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/tunnel/default.go b/tunnel/default.go index a9eb2aea..0a770b6a 100644 --- a/tunnel/default.go +++ b/tunnel/default.go @@ -200,6 +200,34 @@ func (t *tun) monitor() { case <-t.closed: return case <-reconnect.C: + t.RLock() + + var delLinks []string + // check the link status and purge dead links + for node, link := range t.links { + // check link status + switch link.Status() { + case "closed": + delLinks = append(delLinks, node) + case "error": + delLinks = append(delLinks, node) + } + } + + t.RUnlock() + + // delete the dead links + if len(delLinks) > 0 { + t.Lock() + for _, node := range delLinks { + link := t.links[node] + link.Close() + delete(t.links, node) + } + t.Unlock() + } + + // check current link status var connect []string // build list of unknown nodes to connect to diff --git a/tunnel/link.go b/tunnel/link.go index d43b927c..f2395727 100644 --- a/tunnel/link.go +++ b/tunnel/link.go @@ -31,6 +31,9 @@ type link struct { lastKeepAlive time.Time // channels keeps a mapping of channels and last seen channels map[string]time.Time + + // keep an error count on the link + errCount int } func newLink(s transport.Socket) *link { @@ -101,11 +104,33 @@ func (l *link) Close() error { return nil } +func (l *link) Send(m *transport.Message) error { + err := l.Socket.Send(m) + + l.Lock() + defer l.Unlock() + + // if theres no error reset the counter + if err == nil { + l.errCount = 0 + } + + // otherwise increment the counter + l.errCount++ + + return err +} + func (l *link) Status() string { select { case <-l.closed: return "closed" default: + l.RLock() + defer l.RUnlock() + if l.errCount > 3 { + return "error" + } return "connected" } } From 3d2bf7d4f69c280e0e6375afc17f4cd0eb3501e4 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 25 Sep 2019 12:36:07 +0100 Subject: [PATCH 2/2] Add log message --- tunnel/default.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tunnel/default.go b/tunnel/default.go index 0a770b6a..ff13e833 100644 --- a/tunnel/default.go +++ b/tunnel/default.go @@ -220,6 +220,7 @@ func (t *tun) monitor() { if len(delLinks) > 0 { t.Lock() for _, node := range delLinks { + log.Debugf("Tunnel deleting dead link for %s", node) link := t.links[node] link.Close() delete(t.links, node)