micro/tunnel/link.go

210 lines
3.8 KiB
Go
Raw Normal View History

2019-08-29 14:42:27 +03:00
package tunnel
import (
2019-08-30 22:05:00 +03:00
"sync"
"time"
2019-08-29 14:42:27 +03:00
"github.com/google/uuid"
"github.com/micro/go-micro/transport"
)
2019-08-30 22:05:00 +03:00
type link struct {
transport.Socket
2019-08-30 22:05:00 +03:00
sync.RWMutex
2019-09-11 17:11:40 +03:00
// stops the link
closed chan bool
2019-08-30 22:05:00 +03:00
// unique id of this link e.g uuid
// which we define for ourselves
id string
// whether its a loopback connection
// this flag is used by the transport listener
// which accepts inbound quic connections
loopback bool
// whether its actually connected
// dialled side sets it to connected
// after sending the message. the
// listener waits for the connect
connected bool
// the last time we received a keepalive
// on this link from the remote side
lastKeepAlive time.Time
// channels keeps a mapping of channels and last seen
channels map[string]time.Time
2019-10-22 21:38:29 +03:00
// the weighted moving average roundtrip
2019-10-22 20:43:09 +03:00
rtt int64
2019-10-22 21:38:29 +03:00
// weighted moving average of bits flowing
rate float64
// keep an error count on the link
errCount int
2019-08-30 22:05:00 +03:00
}
2019-08-29 14:42:27 +03:00
func newLink(s transport.Socket) *link {
l := &link{
Socket: s,
id: uuid.New().String(),
channels: make(map[string]time.Time),
closed: make(chan bool),
lastKeepAlive: time.Now(),
}
2019-09-11 17:11:40 +03:00
go l.expiry()
return l
}
2019-10-22 20:43:09 +03:00
func (l *link) setRTT(d time.Duration) {
l.Lock()
defer l.Unlock()
2019-10-22 21:38:29 +03:00
if l.rtt <= 0 {
2019-10-22 20:43:09 +03:00
l.rtt = d.Nanoseconds()
return
}
// https://fishi.devtail.io/weblog/2015/04/12/measuring-bandwidth-and-round-trip-time-tcp-connection-inside-application-layer/
rtt := 0.8*float64(l.rtt) + 0.2*float64(d.Nanoseconds())
// set new rtt
l.rtt = int64(rtt)
}
2019-09-11 17:11:40 +03:00
// watches the channel expiry
func (l *link) expiry() {
t := time.NewTicker(time.Minute)
defer t.Stop()
for {
select {
case <-l.closed:
return
case <-t.C:
// drop any channel mappings older than 2 minutes
var kill []string
killTime := time.Minute * 2
l.RLock()
for ch, t := range l.channels {
if d := time.Since(t); d > killTime {
kill = append(kill, ch)
}
}
l.RUnlock()
2019-09-04 13:58:25 +03:00
// if nothing to kill don't bother with a wasted lock
if len(kill) == 0 {
continue
}
// kill the channels!
l.Lock()
for _, ch := range kill {
delete(l.channels, ch)
}
l.Unlock()
}
}
}
2019-10-22 18:50:00 +03:00
// Delay is the current load on the link
func (l *link) Delay() int64 {
return 0
}
2019-10-22 18:53:47 +03:00
// Current transfer rate as bits per second (lower is better)
2019-10-22 18:50:00 +03:00
func (l *link) Rate() float64 {
2019-10-22 21:38:29 +03:00
l.RLock()
defer l.RUnlock()
return l.rate
2019-10-22 18:50:00 +03:00
}
2019-10-22 20:43:09 +03:00
// Length returns the roundtrip time as nanoseconds (lower is better).
// Returns 0 where no measurement has been taken.
2019-10-22 18:50:00 +03:00
func (l *link) Length() int64 {
2019-10-22 20:43:09 +03:00
l.RLock()
defer l.RUnlock()
return l.rtt
2019-10-22 18:50:00 +03:00
}
2019-09-04 17:41:57 +03:00
func (l *link) Id() string {
l.RLock()
defer l.RUnlock()
return l.id
}
func (l *link) Close() error {
select {
case <-l.closed:
2019-09-04 17:41:57 +03:00
return nil
default:
close(l.closed)
2019-08-29 14:42:27 +03:00
}
2019-09-04 17:41:57 +03:00
return nil
2019-08-29 14:42:27 +03:00
}
2019-09-11 17:11:40 +03:00
func (l *link) Send(m *transport.Message) error {
2019-10-22 21:38:29 +03:00
dataSent := len(m.Body)
// set header length
for k, v := range m.Header {
dataSent += (len(k) + len(v))
}
// get time now
now := time.Now()
// send the message
err := l.Socket.Send(m)
l.Lock()
defer l.Unlock()
2019-10-22 21:38:29 +03:00
// calculate based on data
if dataSent > 0 {
// measure time taken
delta := time.Since(now)
// bit sent
bits := dataSent * 1024
// rate of send in bits per nanosecond
rate := float64(bits) / float64(delta.Nanoseconds())
2019-10-23 12:55:53 +03:00
// default the rate if its zero
2019-10-22 21:38:29 +03:00
if l.rate == 0 {
// rate per second
l.rate = rate * 1e9
} else {
// set new rate per second
l.rate = 0.8*l.rate + 0.2*(rate*1e9)
}
}
// if theres no error reset the counter
if err == nil {
l.errCount = 0
}
// otherwise increment the counter
l.errCount++
return err
}
2019-09-11 17:11:40 +03:00
func (l *link) Status() string {
select {
case <-l.closed:
return "closed"
default:
l.RLock()
defer l.RUnlock()
if l.errCount > 3 {
return "error"
}
2019-09-11 17:11:40 +03:00
return "connected"
}
}