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
12 changes: 10 additions & 2 deletions internal/p2plimits/pressure.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type StreamPressure struct {
mu sync.Mutex
lastLogAt map[peer.ID]time.Time
now func() time.Time
// reported are the transports busiest has a value for.
reported map[string]bool
}

// connLoad is what the sampler reads off a connection.
Expand All @@ -72,7 +74,7 @@ func ObserveStreamPressure(conns func() []network.Conn, busiest *prometheus.Gaug
if warnf == nil {
warnf = func(string, ...any) {}
}
return &StreamPressure{conns: conns, busiest: busiest, warnf: warnf, lastLogAt: map[peer.ID]time.Time{}, now: time.Now}
return &StreamPressure{conns: conns, busiest: busiest, warnf: warnf, lastLogAt: map[peer.ID]time.Time{}, now: time.Now, reported: map[string]bool{}}
}

// SetClock replaces the clock the log limiter reads; for tests.
Expand Down Expand Up @@ -106,6 +108,12 @@ func (p *StreamPressure) observe(loads []connLoad) {
// Relayed connections are streams on a transport connection already
// counted; they carry one stream each and have no limit of their own.
busiest := map[string]int{TransportQUIC: 0, "tcp": 0}
p.mu.Lock()
// A transport reported before falls back to 0 when its last connection
// is gone, rather than keep the closed connection's count.
for transport := range p.reported {
busiest[transport] = 0
}
for _, l := range loads {
if l.transport == "p2p-circuit" {
continue
Expand All @@ -115,12 +123,12 @@ func (p *StreamPressure) observe(loads []connLoad) {
}
}
for transport, n := range busiest {
p.reported[transport] = true
p.busiest.WithLabelValues(transport).Set(float64(n))
}

threshold := StreamPressureWarnAt
now := p.now()
p.mu.Lock()
for k, t := range p.lastLogAt {
if now.Sub(t) >= RefusalLogInterval {
delete(p.lastLogAt, k)
Expand Down
27 changes: 27 additions & 0 deletions internal/p2plimits/pressure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,30 @@ func TestStreamPressureNamesAQUICConnectionNearItsLimit(t *testing.T) {
t.Errorf("peers remembered after an interval of quiet = %d, want 0", n)
}
}

// A transport other than QUIC and TCP (the router listens on WebSocket,
// WebTransport and WebRTC) falls back to 0 once its last connection is
// gone, instead of keeping the closed connection's count.
func TestStreamPressureClearsATransportWithNoConnectionLeft(t *testing.T) {
busiest := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: "test_busiest"}, []string{"transport"})
p := ObserveStreamPressure(func() []network.Conn { return nil }, busiest, nil)
transports := []string{"websocket", "webtransport", "webrtc-direct"}

var loads []connLoad
for _, transport := range transports {
loads = append(loads, connLoad{peer: test.RandPeerIDFatal(t), transport: transport, streams: 7})
}
p.observe(loads)
for _, transport := range transports {
if got := gaugeValue(t, busiest.WithLabelValues(transport)); got != 7 {
t.Errorf("busiest %s with one connection of 7 streams = %v, want 7", transport, got)
}
}

p.observe(nil)
for _, transport := range transports {
if got := gaugeValue(t, busiest.WithLabelValues(transport)); got != 0 {
t.Errorf("busiest %s after its last connection closed = %v, want 0", transport, got)
}
}
}
Loading