micro/tunnel/link.go

339 lines
6.1 KiB
Go
Raw Normal View History

2019-08-29 12:42:27 +01:00
package tunnel
import (
2019-10-23 16:05:21 +01:00
"io"
2019-08-30 20:05:00 +01:00
"sync"
"time"
2019-08-29 12:42:27 +01:00
"github.com/google/uuid"
"github.com/micro/go-micro/transport"
)
2019-08-30 20:05:00 +01:00
type link struct {
transport.Socket
2019-08-30 20:05:00 +01:00
sync.RWMutex
2019-09-11 07:11:40 -07:00
// stops the link
closed chan bool
2019-10-23 16:05:21 +01:00
// send queue
sendQueue chan *packet
// receive queue
recvQueue chan *packet
2019-08-30 20:05:00 +01: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 19:38:29 +01:00
// the weighted moving average roundtrip
2019-10-23 16:05:21 +01:00
length int64
2019-10-22 19:38:29 +01:00
// weighted moving average of bits flowing
rate float64
// keep an error count on the link
errCount int
2019-08-30 20:05:00 +01:00
}
2019-10-23 16:05:21 +01:00
// packet send over link
type packet struct {
// message to send or received
message *transport.Message
// status returned when sent
status chan error
// receive related error
err error
}
2019-08-29 12:42:27 +01:00
func newLink(s transport.Socket) *link {
l := &link{
Socket: s,
id: uuid.New().String(),
lastKeepAlive: time.Now(),
2019-10-23 16:05:21 +01:00
closed: make(chan bool),
channels: make(map[string]time.Time),
sendQueue: make(chan *packet, 128),
recvQueue: make(chan *packet, 128),
}
2019-10-23 16:05:21 +01:00
go l.process()
2019-09-11 07:11:40 -07:00
go l.expiry()
return l
}
2019-10-22 18:43:09 +01:00
func (l *link) setRTT(d time.Duration) {
l.Lock()
defer l.Unlock()
2019-10-23 16:05:21 +01:00
if l.length <= 0 {
l.length = d.Nanoseconds()
2019-10-22 18:43:09 +01:00
return
}
// https://fishi.devtail.io/weblog/2015/04/12/measuring-bandwidth-and-round-trip-time-tcp-connection-inside-application-layer/
2019-10-23 16:05:21 +01:00
length := 0.8*float64(l.length) + 0.2*float64(d.Nanoseconds())
// set new length
l.length = int64(length)
}
// process deals with the send queue
func (l *link) process() {
// receive messages
go func() {
for {
m := new(transport.Message)
err := l.recv(m)
if err != nil {
l.Lock()
l.errCount++
l.Unlock()
}
// process new received message
select {
case l.recvQueue <- &packet{message: m, err: err}:
case <-l.closed:
return
}
}
}()
// send messages
for {
select {
case pk := <-l.sendQueue:
// send the message
pk.status <- l.send(pk.message)
case <-l.closed:
return
}
}
2019-10-22 18:43:09 +01:00
}
2019-09-11 07:11:40 -07: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 11:58:25 +01: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-23 16:05:21 +01:00
func (l *link) send(m *transport.Message) error {
if m.Header == nil {
m.Header = make(map[string]string)
2019-10-23 16:05:21 +01:00
}
// send the message
return l.Socket.Send(m)
2019-10-23 16:05:21 +01:00
}
// recv a message on the link
func (l *link) recv(m *transport.Message) error {
if m.Header == nil {
m.Header = make(map[string]string)
}
// receive the transport message
return l.Socket.Recv(m)
}
2019-10-22 16:50:00 +01:00
// Delay is the current load on the link
func (l *link) Delay() int64 {
2019-10-23 16:05:21 +01:00
return int64(len(l.sendQueue) + len(l.recvQueue))
2019-10-22 16:50:00 +01:00
}
2019-10-22 16:53:47 +01:00
// Current transfer rate as bits per second (lower is better)
2019-10-22 16:50:00 +01:00
func (l *link) Rate() float64 {
2019-10-22 19:38:29 +01:00
l.RLock()
defer l.RUnlock()
2019-10-23 16:05:21 +01:00
2019-10-22 19:38:29 +01:00
return l.rate
2019-10-22 16:50:00 +01:00
}
2019-10-22 18:43:09 +01:00
// Length returns the roundtrip time as nanoseconds (lower is better).
// Returns 0 where no measurement has been taken.
2019-10-22 16:50:00 +01:00
func (l *link) Length() int64 {
2019-10-22 18:43:09 +01:00
l.RLock()
defer l.RUnlock()
2019-10-23 16:05:21 +01:00
return l.length
2019-10-22 16:50:00 +01:00
}
2019-09-04 15:41:57 +01: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 15:41:57 +01:00
return nil
default:
2019-10-23 16:39:26 +01:00
l.Socket.Close()
close(l.closed)
2019-08-29 12:42:27 +01:00
}
2019-09-04 15:41:57 +01:00
return nil
2019-08-29 12:42:27 +01:00
}
2019-09-11 07:11:40 -07:00
2019-10-23 16:05:21 +01:00
// Send sencs a message on the link
func (l *link) Send(m *transport.Message) error {
2019-10-23 16:05:21 +01:00
// create a new packet to send over the link
p := &packet{
message: m,
status: make(chan error, 1),
2019-10-22 19:38:29 +01:00
}
// get time now
now := time.Now()
2019-10-23 16:39:26 +01:00
// check if its closed first
2019-10-23 16:05:21 +01:00
select {
case <-l.closed:
return io.EOF
2019-10-23 16:39:26 +01:00
default:
}
// queue the message
select {
2019-10-23 16:05:21 +01:00
case l.sendQueue <- p:
// in the send queue
2019-10-23 16:39:26 +01:00
case <-l.closed:
return io.EOF
2019-10-22 19:38:29 +01:00
}
// error to use
var err error
2019-10-23 16:05:21 +01:00
// wait for response
select {
case <-l.closed:
return io.EOF
case err = <-p.status:
}
l.Lock()
defer l.Unlock()
// there's an error increment the counter and bail
if err != nil {
l.errCount++
2019-10-23 16:05:21 +01:00
return err
}
// reset the counter
l.errCount = 0
// calculate the data sent
dataSent := len(m.Body)
// set header length
for k, v := range m.Header {
dataSent += (len(k) + len(v))
}
// 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())
// default the rate if its zero
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)
}
}
2019-10-23 16:05:21 +01:00
return nil
}
2019-10-23 16:05:21 +01:00
// Accept accepts a message on the socket
func (l *link) Recv(m *transport.Message) error {
select {
case <-l.closed:
2019-10-23 16:39:26 +01:00
// check if there's any messages left
select {
case pk := <-l.recvQueue:
// check the packet receive error
if pk.err != nil {
return pk.err
}
*m = *pk.message
default:
return io.EOF
}
2019-10-23 16:05:21 +01:00
case pk := <-l.recvQueue:
// check the packet receive error
if pk.err != nil {
return pk.err
}
*m = *pk.message
}
return nil
}
2019-10-23 16:05:21 +01:00
// Status can return connected, closed, error
2019-09-11 07:11:40 -07: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 07:11:40 -07:00
return "connected"
}
}