peer: triple timeout parameters to give tor more grace

This commit is contained in:
Keagan McClelland 2024-05-16 11:19:20 -07:00
parent 61191a576d
commit 1148e572bf

View file

@ -7,6 +7,7 @@ import (
"fmt"
"math/rand"
"net"
"strings"
"sync"
"sync/atomic"
"time"
@ -82,6 +83,10 @@ const (
// MaxPongBytes (upper bound accepted by the protocol) because it is
// needlessly wasteful of precious Tor bandwidth for little to no gain.
pongSizeCeiling = 4096
// torTimeoutMultiplier is the scaling factor we use on network timeouts
// for Tor peers.
torTimeoutMultiplier = 3
)
var (
@ -395,6 +400,23 @@ type Brontide struct {
bytesReceived uint64
bytesSent uint64
// isTorConnection is a flag that indicates whether or not we believe
// the remote peer is a tor connection. It is not always possible to
// know this with certainty but we have heuristics we use that should
// catch most cases.
//
// NOTE: We judge the tor-ness of a connection by if the remote peer has
// ".onion" in the address OR if it's connected over localhost.
// This will miss cases where our peer is connected to our clearnet
// address over the tor network (via exit nodes). It will also misjudge
// actual localhost connections as tor. We need to include this because
// inbound connections to our tor address will appear to come from the
// local socks5 proxy. This heuristic is only used to expand the timeout
// window for peers so it is OK to misjudge this. If you use this field
// for any other purpose you should seriously consider whether or not
// this heuristic is good enough for your use case.
isTorConnection bool
pingManager *PingManager
// lastPingPayload stores an unsafe pointer wrapped as an atomic
@ -534,6 +556,12 @@ func NewBrontide(cfg Config) *Brontide {
log: build.NewPrefixLog(logPrefix, peerLog),
}
if cfg.Conn != nil && cfg.Conn.RemoteAddr() != nil {
remoteAddr := cfg.Conn.RemoteAddr().String()
p.isTorConnection = strings.Contains(remoteAddr, ".onion") ||
strings.Contains(remoteAddr, "127.0.0.1")
}
var (
lastBlockHeader *wire.BlockHeader
lastSerializedBlockHeader [wire.MaxBlockHeaderPayload]byte
@ -580,8 +608,8 @@ func NewBrontide(cfg Config) *Brontide {
p.pingManager = NewPingManager(&PingManagerConfig{
NewPingPayload: newPingPayload,
NewPongSize: randPongSize,
IntervalDuration: pingInterval,
TimeoutDuration: pingTimeout,
IntervalDuration: p.scaleTimeout(pingInterval),
TimeoutDuration: p.scaleTimeout(pingTimeout),
SendPing: func(ping *lnwire.Ping) {
p.queueMsg(ping, nil)
},
@ -1299,7 +1327,9 @@ func (p *Brontide) readNextMessage() (lnwire.Message, error) {
// pool. We do so only after the task has been scheduled to
// ensure the deadline doesn't expire while the message is in
// the process of being scheduled.
readDeadline := time.Now().Add(readMessageTimeout)
readDeadline := time.Now().Add(
p.scaleTimeout(readMessageTimeout),
)
readErr := noiseConn.SetReadDeadline(readDeadline)
if readErr != nil {
return readErr
@ -2181,7 +2211,9 @@ func (p *Brontide) writeMessage(msg lnwire.Message) error {
flushMsg := func() error {
// Ensure the write deadline is set before we attempt to send
// the message.
writeDeadline := time.Now().Add(writeMessageTimeout)
writeDeadline := time.Now().Add(
p.scaleTimeout(writeMessageTimeout),
)
err := noiseConn.SetWriteDeadline(writeDeadline)
if err != nil {
return err
@ -4151,3 +4183,15 @@ func (p *Brontide) sendLinkUpdateMsg(cid lnwire.ChannelID, msg lnwire.Message) {
// continue processing message.
chanStream.AddMsg(msg)
}
// scaleTimeout multiplies the argument duration by a constant factor depending
// on variious heuristics. Currently this is only used to check whether our peer
// appears to be connected over Tor and relaxes the timout deadline. However,
// this is subject to change and should be treated as opaque.
func (p *Brontide) scaleTimeout(timeout time.Duration) time.Duration {
if p.isTorConnection {
return timeout * time.Duration(torTimeoutMultiplier)
}
return timeout
}