Cleanup and speedup network convergence along with direct messaging for connect and solicit
This commit is contained in:
@@ -2,6 +2,7 @@ package tunnel
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -14,7 +15,11 @@ import (
|
||||
type link struct {
|
||||
transport.Socket
|
||||
|
||||
// transport to use for connections
|
||||
transport transport.Transport
|
||||
|
||||
sync.RWMutex
|
||||
|
||||
// stops the link
|
||||
closed chan bool
|
||||
// link state channel for testing link
|
||||
@@ -65,6 +70,8 @@ var (
|
||||
linkRequest = []byte{0, 0, 0, 0}
|
||||
// the 4 byte 1 filled packet sent to determine link state
|
||||
linkResponse = []byte{1, 1, 1, 1}
|
||||
|
||||
ErrLinkConnectTimeout = errors.New("link connect timeout")
|
||||
)
|
||||
|
||||
func newLink(s transport.Socket) *link {
|
||||
@@ -72,8 +79,8 @@ func newLink(s transport.Socket) *link {
|
||||
Socket: s,
|
||||
id: uuid.New().String(),
|
||||
lastKeepAlive: time.Now(),
|
||||
channels: make(map[string]time.Time),
|
||||
closed: make(chan bool),
|
||||
channels: make(map[string]time.Time),
|
||||
state: make(chan *packet, 64),
|
||||
sendQueue: make(chan *packet, 128),
|
||||
recvQueue: make(chan *packet, 128),
|
||||
@@ -87,6 +94,32 @@ func newLink(s transport.Socket) *link {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *link) connect(addr string) error {
|
||||
c, err := l.transport.Dial(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.Lock()
|
||||
l.Socket = c
|
||||
l.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *link) accept(sock transport.Socket) error {
|
||||
l.Lock()
|
||||
l.Socket = sock
|
||||
l.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *link) setLoopback(v bool) {
|
||||
l.Lock()
|
||||
l.loopback = v
|
||||
l.Unlock()
|
||||
}
|
||||
|
||||
// setRate sets the bits per second rate as a float64
|
||||
func (l *link) setRate(bits int64, delta time.Duration) {
|
||||
// rate of send in bits per nanosecond
|
||||
@@ -201,11 +234,15 @@ func (l *link) manage() {
|
||||
t := time.NewTicker(time.Minute)
|
||||
defer t.Stop()
|
||||
|
||||
// get link id
|
||||
linkId := l.Id()
|
||||
|
||||
// used to send link state packets
|
||||
send := func(b []byte) error {
|
||||
return l.Send(&transport.Message{
|
||||
Header: map[string]string{
|
||||
"Micro-Method": "link",
|
||||
"Micro-Method": "link",
|
||||
"Micro-Link-Id": linkId,
|
||||
}, Body: b,
|
||||
})
|
||||
}
|
||||
@@ -229,9 +266,7 @@ func (l *link) manage() {
|
||||
// check the type of message
|
||||
switch {
|
||||
case bytes.Equal(p.message.Body, linkRequest):
|
||||
l.RLock()
|
||||
log.Tracef("Link %s received link request %v", l.id, p.message.Body)
|
||||
l.RUnlock()
|
||||
log.Tracef("Link %s received link request", linkId)
|
||||
|
||||
// send response
|
||||
if err := send(linkResponse); err != nil {
|
||||
@@ -242,9 +277,7 @@ func (l *link) manage() {
|
||||
case bytes.Equal(p.message.Body, linkResponse):
|
||||
// set round trip time
|
||||
d := time.Since(now)
|
||||
l.RLock()
|
||||
log.Tracef("Link %s received link response in %v", p.message.Body, d)
|
||||
l.RUnlock()
|
||||
log.Tracef("Link %s received link response in %v", linkId, d)
|
||||
// set the RTT
|
||||
l.setRTT(d)
|
||||
}
|
||||
@@ -309,6 +342,12 @@ func (l *link) Rate() float64 {
|
||||
return l.rate
|
||||
}
|
||||
|
||||
func (l *link) Loopback() bool {
|
||||
l.RLock()
|
||||
defer l.RUnlock()
|
||||
return l.loopback
|
||||
}
|
||||
|
||||
// Length returns the roundtrip time as nanoseconds (lower is better).
|
||||
// Returns 0 where no measurement has been taken.
|
||||
func (l *link) Length() int64 {
|
||||
@@ -320,7 +359,6 @@ func (l *link) Length() int64 {
|
||||
func (l *link) Id() string {
|
||||
l.RLock()
|
||||
defer l.RUnlock()
|
||||
|
||||
return l.id
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user