Merge pull request #785 from micro/link

Keep track of errors and delete beyond error count > 3
This commit is contained in:
Asim Aslam 2019-09-25 12:36:23 +01:00 committed by GitHub
commit c3b430af53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -200,6 +200,35 @@ func (t *tun) monitor() {
case <-t.closed: case <-t.closed:
return return
case <-reconnect.C: 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 {
log.Debugf("Tunnel deleting dead link for %s", node)
link := t.links[node]
link.Close()
delete(t.links, node)
}
t.Unlock()
}
// check current link status
var connect []string var connect []string
// build list of unknown nodes to connect to // build list of unknown nodes to connect to

View File

@ -31,6 +31,9 @@ type link struct {
lastKeepAlive time.Time lastKeepAlive time.Time
// channels keeps a mapping of channels and last seen // channels keeps a mapping of channels and last seen
channels map[string]time.Time channels map[string]time.Time
// keep an error count on the link
errCount int
} }
func newLink(s transport.Socket) *link { func newLink(s transport.Socket) *link {
@ -101,11 +104,33 @@ func (l *link) Close() error {
return nil 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 { func (l *link) Status() string {
select { select {
case <-l.closed: case <-l.closed:
return "closed" return "closed"
default: default:
l.RLock()
defer l.RUnlock()
if l.errCount > 3 {
return "error"
}
return "connected" return "connected"
} }
} }