diff --git a/internal/p2plimits/streams.go b/internal/p2plimits/streams.go index b2361b90..461f54dc 100644 --- a/internal/p2plimits/streams.go +++ b/internal/p2plimits/streams.go @@ -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" @@ -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)) diff --git a/internal/p2plimits/streams_test.go b/internal/p2plimits/streams_test.go index 82335be4..3fcadf19 100644 --- a/internal/p2plimits/streams_test.go +++ b/internal/p2plimits/streams_test.go @@ -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) } }