mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-05-04 08:50:20 +02:00
peer: triple timeout parameters to give tor more grace
This commit is contained in:
parent
61191a576d
commit
1148e572bf
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@ -82,6 +83,10 @@ const (
|
|||||||
// MaxPongBytes (upper bound accepted by the protocol) because it is
|
// MaxPongBytes (upper bound accepted by the protocol) because it is
|
||||||
// needlessly wasteful of precious Tor bandwidth for little to no gain.
|
// needlessly wasteful of precious Tor bandwidth for little to no gain.
|
||||||
pongSizeCeiling = 4096
|
pongSizeCeiling = 4096
|
||||||
|
|
||||||
|
// torTimeoutMultiplier is the scaling factor we use on network timeouts
|
||||||
|
// for Tor peers.
|
||||||
|
torTimeoutMultiplier = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -395,6 +400,23 @@ type Brontide struct {
|
|||||||
bytesReceived uint64
|
bytesReceived uint64
|
||||||
bytesSent 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
|
pingManager *PingManager
|
||||||
|
|
||||||
// lastPingPayload stores an unsafe pointer wrapped as an atomic
|
// lastPingPayload stores an unsafe pointer wrapped as an atomic
|
||||||
@ -534,6 +556,12 @@ func NewBrontide(cfg Config) *Brontide {
|
|||||||
log: build.NewPrefixLog(logPrefix, peerLog),
|
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 (
|
var (
|
||||||
lastBlockHeader *wire.BlockHeader
|
lastBlockHeader *wire.BlockHeader
|
||||||
lastSerializedBlockHeader [wire.MaxBlockHeaderPayload]byte
|
lastSerializedBlockHeader [wire.MaxBlockHeaderPayload]byte
|
||||||
@ -580,8 +608,8 @@ func NewBrontide(cfg Config) *Brontide {
|
|||||||
p.pingManager = NewPingManager(&PingManagerConfig{
|
p.pingManager = NewPingManager(&PingManagerConfig{
|
||||||
NewPingPayload: newPingPayload,
|
NewPingPayload: newPingPayload,
|
||||||
NewPongSize: randPongSize,
|
NewPongSize: randPongSize,
|
||||||
IntervalDuration: pingInterval,
|
IntervalDuration: p.scaleTimeout(pingInterval),
|
||||||
TimeoutDuration: pingTimeout,
|
TimeoutDuration: p.scaleTimeout(pingTimeout),
|
||||||
SendPing: func(ping *lnwire.Ping) {
|
SendPing: func(ping *lnwire.Ping) {
|
||||||
p.queueMsg(ping, nil)
|
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
|
// pool. We do so only after the task has been scheduled to
|
||||||
// ensure the deadline doesn't expire while the message is in
|
// ensure the deadline doesn't expire while the message is in
|
||||||
// the process of being scheduled.
|
// the process of being scheduled.
|
||||||
readDeadline := time.Now().Add(readMessageTimeout)
|
readDeadline := time.Now().Add(
|
||||||
|
p.scaleTimeout(readMessageTimeout),
|
||||||
|
)
|
||||||
readErr := noiseConn.SetReadDeadline(readDeadline)
|
readErr := noiseConn.SetReadDeadline(readDeadline)
|
||||||
if readErr != nil {
|
if readErr != nil {
|
||||||
return readErr
|
return readErr
|
||||||
@ -2181,7 +2211,9 @@ func (p *Brontide) writeMessage(msg lnwire.Message) error {
|
|||||||
flushMsg := func() error {
|
flushMsg := func() error {
|
||||||
// Ensure the write deadline is set before we attempt to send
|
// Ensure the write deadline is set before we attempt to send
|
||||||
// the message.
|
// the message.
|
||||||
writeDeadline := time.Now().Add(writeMessageTimeout)
|
writeDeadline := time.Now().Add(
|
||||||
|
p.scaleTimeout(writeMessageTimeout),
|
||||||
|
)
|
||||||
err := noiseConn.SetWriteDeadline(writeDeadline)
|
err := noiseConn.SetWriteDeadline(writeDeadline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -4151,3 +4183,15 @@ func (p *Brontide) sendLinkUpdateMsg(cid lnwire.ChannelID, msg lnwire.Message) {
|
|||||||
// continue processing message.
|
// continue processing message.
|
||||||
chanStream.AddMsg(msg)
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user