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/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const (
StreamScopeProtocolPeer = "protocol_peer"
StreamScopeService = "service"
StreamScopeServicePeer = "service_peer"
StreamScopePeer = "peer"
// StreamScopeConn is the system or transient scope, where the manager
// does not yet know the protocol.
StreamScopeConn = "conn"
Expand Down Expand Up @@ -125,7 +124,16 @@ func (s *StreamRefusals) BlockStream(_ peer.ID, _ network.Direction) {
s.refusedStream(StreamScopeConn, "")
}

func (s *StreamRefusals) BlockPeer(peer.ID) { s.refusedStream(StreamScopePeer, "") }
// BlockPeer is a connection the manager refused to attach to its peer: its
// peer scope has no room for it. libp2p calls it from a connection
// scope's SetPeer, never for a stream (a stream refused at its peer scope
// arrives through BlockStream), so it is said as a connection refusal and
// kept out of the stream counter.
func (s *StreamRefusals) BlockPeer(p peer.ID) {
s.report("conn peer", func(n uint64) string {
return fmt.Sprintf("[Conn] Refused %d connection(s) at the peer scope since the last report, the last for peer %s: the peer scope has no room for them; the dialer sees the connection close", n, p)
})
}

func (s *StreamRefusals) BlockProtocol(proto protocol.ID) {
s.refusedStream(StreamScopeProtocol, string(proto))
Expand Down
42 changes: 39 additions & 3 deletions internal/p2plimits/streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,45 @@ func TestStreamRefusalsLogOneProtocolOncePerInterval(t *testing.T) {
t.Errorf("after the interval: %q", lines[len(lines)-1])
}
// A scope with no protocol to name is said without one.
s.BlockPeer("")
if lines[len(lines)-1] != "[Stream] Refused 1 stream(s) at the peer scope since the last report; the peer sees its stream reset" {
t.Errorf("peer scope: %q", lines[len(lines)-1])
s.BlockStream("", network.DirInbound)
const wantConn = "[Stream] Refused 1 stream(s) at the conn scope since the last report; the peer sees its stream reset"
if got := lines[len(lines)-1]; got != wantConn {
t.Errorf("conn scope: got %q, want %q", got, wantConn)
}
}

// libp2p calls BlockPeer when a connection is refused at its peer scope, not
// a stream: it is logged as a connection refusal once per interval and never
// counted as a refused stream.
func TestStreamRefusalsSayPeerScopeConnectionRefusalsApart(t *testing.T) {
now := time.Unix(1_700_000_000, 0)
refused := newStreamsRefusedCounter()
var lines []string
s := CountStreamRefusals(refused, nil, func(format string, args ...any) {
lines = append(lines, fmt.Sprintf(format, args...))
})
s.SetClock(func() time.Time { return now })
first := test.RandPeerIDFatal(t)
last := test.RandPeerIDFatal(t)

s.BlockPeer(first)
s.BlockPeer(first)
s.BlockPeer(last)
wantFirst := "[Conn] Refused 1 connection(s) at the peer scope since the last report, the last for peer " + first.String() + ":"
if len(lines) != 1 || !strings.HasPrefix(lines[0], wantFirst) {
t.Fatalf("lines = %q, want one line starting with %q", lines, wantFirst)
}
series := make(chan prometheus.Metric, 8)
refused.Collect(series)
if got := len(series); got != 0 {
t.Errorf("series in the stream refusal counter after connection refusals = %d, want 0", got)
}

now = now.Add(RefusalLogInterval)
s.BlockPeer(last)
wantNext := "[Conn] Refused 3 connection(s) at the peer scope since the last report, the last for peer " + last.String() + ":"
if len(lines) != 2 || !strings.HasPrefix(lines[1], wantNext) {
t.Errorf("after the interval: got %q, want 2 lines with the second starting with %q", lines, wantNext)
}
}

Expand Down
Loading