Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions toversok/actors/a_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,15 @@
// This prevents routers from blocking when the conn is shutting down,
// or if its blocked otherwise.
func (ic *InConn) ForwardPacket(pkt []byte) {
select {
case ic.pktCh <- pkt:
default:
// TODO maybe convert dropping to timeout?
// making lots of timers would be costly though
// TODO log? metric?
}
//select {

Check failure on line 278 in toversok/actors/a_conn.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
//case ic.pktCh <- pkt:
//default:
// // TODO maybe convert dropping to timeout?
// // making lots of timers would be costly though
// // TODO log? metric?
//}

ic.pktCh <- pkt
}

// Bump the activity timer.
Expand Down
10 changes: 6 additions & 4 deletions toversok/actors/a_sockrecv.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package actors

import (
"bytes"
"context"
"errors"
"log/slog"
"net"
"net/netip"
"runtime/debug"
"slices"
"time"

"github.com/edup2p/common/types"
Expand Down Expand Up @@ -52,7 +52,9 @@ func (r *SockRecv) Run() {
}
}()

buf := make([]byte, 1<<16)
const MaxBuf = 1 << 16

var buf = [MaxBuf]byte{0}

for {
if r.ctx.Err() != nil {
Expand All @@ -65,7 +67,7 @@ func (r *SockRecv) Run() {
return
}

n, ap, err := r.Conn.ReadFromUDPAddrPort(buf)
n, ap, err := r.Conn.ReadFromUDPAddrPort(buf[:])

ts := time.Now()

Expand All @@ -92,7 +94,7 @@ func (r *SockRecv) Run() {
continue
}

pkt := slices.Clone(buf[:n])
pkt := bytes.Clone(buf[:n])

if r.ctx.Err() != nil {
return
Expand Down
31 changes: 20 additions & 11 deletions usrwg/channel_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ func (cc *ChannelConn) SetReadDeadline(t time.Time) error {
func (cc *ChannelConn) ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error) {
var val []byte

if cc.currentReadDeadline == (time.Time{}) {
// Block until value received.
val = <-cc.incoming
} else {
// Block until value or timeout.
select {
case val = <-cc.incoming:
case <-time.After(time.Until(cc.currentReadDeadline)):
return 0, netip.AddrPort{}, context.DeadlineExceeded
select {
case val = <-cc.incoming:
default:
if cc.currentReadDeadline == (time.Time{}) {
// Block until value received.
val = <-cc.incoming
} else {
// Block until value or timeout.
select {
case val = <-cc.incoming:
case <-time.After(time.Until(cc.currentReadDeadline)):
return 0, netip.AddrPort{}, context.DeadlineExceeded
}
}
}

Expand Down Expand Up @@ -110,8 +114,13 @@ func (cc *ChannelConn) putIn(pkt []byte, d time.Duration) (ok bool) {
select {
case cc.incoming <- pkt:
ok = true
case <-time.After(d):
ok = false
default:
select {
case cc.incoming <- pkt:
ok = true
case <-time.After(d):
ok = false
}
}

return
Expand Down
Loading