mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
use backpressure instead of estimated bandwidth, sorted by uptime to break ties
This commit is contained in:
parent
707e23d392
commit
38e7704161
4 changed files with 27 additions and 51 deletions
|
@ -190,26 +190,12 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
|
|||
buf := bufio.NewWriterSize(sock, tcp_msgSize)
|
||||
send := func(msg []byte) {
|
||||
msgLen := wire_encode_uint64(uint64(len(msg)))
|
||||
before := buf.Buffered()
|
||||
start := time.Now()
|
||||
buf.Write(tcp_msg[:])
|
||||
buf.Write(msgLen)
|
||||
buf.Write(msg)
|
||||
timed := time.Since(start)
|
||||
after := buf.Buffered()
|
||||
written := (before + len(tcp_msg) + len(msgLen) + len(msg)) - after
|
||||
if written > 0 {
|
||||
p.updateBandwidth(written, timed)
|
||||
}
|
||||
p.updateQueueSize(-1)
|
||||
util_putBytes(msg)
|
||||
}
|
||||
flush := func() {
|
||||
size := buf.Buffered()
|
||||
start := time.Now()
|
||||
buf.Flush()
|
||||
timed := time.Since(start)
|
||||
p.updateBandwidth(size, timed)
|
||||
}
|
||||
go func() {
|
||||
var stack [][]byte
|
||||
put := func(msg []byte) {
|
||||
|
@ -217,6 +203,7 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
|
|||
for len(stack) > 32 {
|
||||
util_putBytes(stack[0])
|
||||
stack = stack[1:]
|
||||
p.updateQueueSize(-1)
|
||||
}
|
||||
}
|
||||
for msg := range out {
|
||||
|
@ -226,7 +213,7 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
|
|||
select {
|
||||
case msg, ok := <-out:
|
||||
if !ok {
|
||||
flush()
|
||||
buf.Flush()
|
||||
return
|
||||
}
|
||||
put(msg)
|
||||
|
@ -236,13 +223,14 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
|
|||
send(msg)
|
||||
}
|
||||
}
|
||||
flush()
|
||||
buf.Flush()
|
||||
}
|
||||
}()
|
||||
p.out = func(msg []byte) {
|
||||
defer func() { recover() }()
|
||||
select {
|
||||
case out <- msg:
|
||||
p.updateQueueSize(1)
|
||||
default:
|
||||
util_putBytes(msg)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue