Skip to content

p2plimits: say a peer-scope connection refusal as a connection, not a stream - #988

Open
kavishkartha05 wants to merge 1 commit into
google:mainfrom
kavishkartha05:p2plimits-count-peer-conn-refusals
Open

kavishkartha05 wants to merge 1 commit into
google:mainfrom
kavishkartha05:p2plimits-count-peer-conn-refusals

Conversation

@kavishkartha05

Copy link
Copy Markdown

What

StreamRefusals.BlockPeer no longer counts under *_streams_refused_total{scope="peer"} and no longer logs the [Stream] Refused ... the peer sees its stream reset line. A peer-scope connection refusal is now a rate-limited [Conn] Refused N connection(s) at the peer scope since the last report: ... line (one per RefusalLogInterval, carrying the count since the last report). The now-unused StreamScopePeer constant is removed.

Why

In go-libp2p v0.51.0 the resource manager calls BlockPeer only from connectionScope.SetPeer (rcmgr.go:826), i.e. when a connection is refused at its peer scope. A stream refused at its peer scope arrives as BlockStream. So connection refusals were showing up as stream refusals in the metric and the log. This follows the fix aojea described on #951: log it as a [Conn] line instead of counting it in *_streams_refused_total. It is left out of *_inbound_connections_refused_total because BlockPeer carries no connection direction, so counting it there could mislabel outbound refusals.

TestStreamRefusalsLogOneProtocolOncePerInterval only used BlockPeer as a synthetic "scope with no protocol" case, which BlockStream already covers, so it uses BlockStream now. A new test, TestStreamRefusalsSayPeerScopeConnectionRefusalsApart, checks the [Conn] line, that it is logged once per interval with the accumulated count, and that the stream refusal counter gets no series.

Fixes #951

Testing

  • go test -race -count 1 ./internal/p2plimits ./internal/node ./internal/router: pass.
  • hack/lint.sh (golangci-lint v2.14.0 plus deadcode): 0 issues. gofmt and go vet clean.
  • I ran the tests on macOS with TMPDIR=/tmp/am: internal/node has tests that fail on macOS's long default temp path (unix socket paths are limited to 103 bytes). They pass with a short TMPDIR and are unrelated to this change.
  • tests/integration and make e2e-test were not run locally (the integration suite fails on my machine before this change too, for what looks like a local port conflict, and kind/bats are not installed); I'm relying on CI for those.

Breaking changes

None to the API or wire format. Operators who alerted on *_streams_refused_total{scope="peer"} will no longer see peer-scope connection refusals there; they are in the [Conn] log line.

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented Nov 29, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors peer blocking behavior in internal/p2plimits/streams.go to treat peer-scope blocks as connection refusals rather than stream refusals, updating the logging and metrics accordingly. The review feedback focuses on adhering to the repository style guide: specifically, ensuring that log lines for peer blocks include the peer identifier (Section 6, Rule 4) and that test assertions explicitly state both the observed and expected values (Section 3, Rule 8).

Comment thread internal/p2plimits/streams.go Resolved
Comment thread internal/p2plimits/streams_test.go Resolved
Comment thread internal/p2plimits/streams_test.go Resolved
@kavishkartha05
kavishkartha05 force-pushed the p2plimits-count-peer-conn-refusals branch from d8f3a85 to 002e1a9 Compare November 29, 2026 03:28
… stream

libp2p calls MetricsReporter.BlockPeer from a connection scope's SetPeer,
when a connection is refused at its peer scope. A stream refused at its
peer scope arrives through BlockStream. StreamRefusals counted BlockPeer
under *_streams_refused_total{scope="peer"} and logged "[Stream] Refused
... the peer sees its stream reset", so a refused connection read as a
refused stream.

BlockPeer no longer counts in the stream counter. It logs a rate-limited
"[Conn] Refused N connection(s) at the peer scope" line instead, naming
the peer of the last refusal it counts. It stays out of the inbound
connection counters too: BlockPeer carries no direction.

The existing log test used BlockPeer as a synthetic empty-protocol case;
BlockStream covers that, so it uses BlockStream now. A new test covers
the [Conn] line, its once-per-interval limit and that the stream counter
gets no series. Test assertions state both the observed and the expected
value.

Fixes google#951
@kavishkartha05
kavishkartha05 force-pushed the p2plimits-count-peer-conn-refusals branch from 002e1a9 to 5ef4d6c Compare November 29, 2026 03:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

p2plimits: a connection refused at its peer scope is counted and logged as a refused stream

1 participant