Skip to content

Service connection limiter - #2686

Merged
otoolep merged 1 commit into
rqlite:masterfrom
shrtyk:fix/service-connection-limit
Jun 2, 2026
Merged

otoolep merged 1 commit into
rqlite:masterfrom
shrtyk:fix/service-connection-limit

Conversation

@shrtyk

@shrtyk shrtyk commented May 29, 2026

Copy link
Copy Markdown
Contributor

Hi @otoolep,

I added a connection limiter to the cluster service, along with a test covering rejection when the limiter is full and accepting new connections again once a slot is released.
I set maxConcurrentConns to 1024 for now. My reasoning was that this should be well above normal cluster traffic, while still putting a hard cap on goroutines and per-connection resources during a flood.
Does this default look reasonable to you, or would you rather use a different value or make it configurable?

Fixes #2617

@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 introduces a connection limiter to the cluster service to bound the number of concurrent connections to 1024, preventing unbounded goroutine creation. When the limit is reached, incoming connections are rejected and closed, and a new metric num_conn_rejected is incremented. A test is also added to verify this behavior. The review comments correctly point out compilation errors in the test where the non-existent errors.AsType function is used instead of errors.As.

Comment thread cluster/service_test.go Resolved
Comment thread cluster/service_test.go Resolved
@otoolep

otoolep commented May 31, 2026

Copy link
Copy Markdown
Member

This looks reasonable -- thanks.

Comment thread cluster/service.go Resolved
@shrtyk
shrtyk force-pushed the fix/service-connection-limit branch from 5515875 to bb22399 Compare May 31, 2026 14:56
Comment thread cluster/service_test.go Resolved
@shrtyk
shrtyk force-pushed the fix/service-connection-limit branch from bb22399 to a23bddc Compare June 1, 2026 02:02

@otoolep otoolep left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the latest updates. However the unit test is trying to do too much. It should be possible to test the Service's behavior purely using its public behavior.

Comment thread cluster/service_test.go Resolved
Comment thread cluster/service_test.go Resolved
Comment thread cluster/service_test.go Resolved
Comment thread cluster/service_test.go Resolved
Comment thread cluster/service_test.go Resolved
Comment thread cluster/service_test.go Resolved
Comment thread cluster/service.go Resolved
@shrtyk
shrtyk force-pushed the fix/service-connection-limit branch from a23bddc to f36e51b Compare June 1, 2026 17:32
Comment thread cluster/service.go Outdated
}

func (s *Service) serve() error {
defer s.open.Unset()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only unset in Close(). Unsetting here is not what someone would expect.

Comment thread cluster/service_test.go Outdated
// The first client's pooled connection remains open and holds the limiter slot.
c2 := NewClient(md, 30*time.Second)
var getErr error
if !waitFor(2*time.Second, func() bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why does this need to be in a polling function? It either succeeds or it doesn't.

Comment thread cluster/service_test.go Outdated

// A new client request should be accepted once capacity is available again.
c3 := NewClient(md, 30*time.Second)
if !waitFor(2*time.Second, func() bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ditto. There is no reason for this to poll.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you're right. The polling came from the earlier version of the test.
It was only needed because previous version also checked slot release with raw TCP connections. Close() doesn't free the server-side slot synchronously. The server goroutine has to notice the close and exit first. A follow-up connection could race that cleanup and be rejected even with correct code.

Comment thread cluster/service_test.go
}
}

type mockDialer struct {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems to be getting very complicated. Why not just use a standard dialer?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I used the mock dialer to keep this test on the same NewClient / GetNodeMeta path as the surrounding service tests.
The tracking in mockDialer is only there to make the limiter-slot release deterministic.
I can switch this to a plain net.Dialer, but then the test would need to bypass the public client API and use helpers like writeCommandReadResponse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK, let me take a look at the code again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

And yes, I jumped the gun when I said writeCommandReadResponse should be used. This can definitely be done more simply, without overcomplicating it.

@otoolep

otoolep commented Jun 1, 2026 •

Copy link
Copy Markdown
Member

Hi -- thanks for the latest changes, it's getting better. But the change still seems to be too complicated, polling functions, mock dialers. I don't follow why all of that is needed.

@shrtyk

shrtyk commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

I left a note on the polling question above. Short version: it was carried over from the raw TCP version, where a connection opened right after Close() could race with the server goroutine releasing the slot.

Stepping back, I see two options:

  1. Keep mockDialer, but remove the polling.
  2. Go back to raw TCP connections. In that case, we still need a small poll after closing the first connection, because that can race with server-side cleanup.

One caveat: the race does not really disappear with mockDialer; the client path mostly hides it. GetNodeMeta() does more work before the next request reaches the server, and the retry path can recover if the first attempt is rejected too early. With raw TCP, the test hits the connection boundary directly, so the race is easier to expose.

@otoolep

otoolep commented Jun 2, 2026

Copy link
Copy Markdown
Member

Also, if you're concerned that calling Close() doesn't shut down the goroutines, yeah, that's an issue. The right way to handle that is done channels and so on. But we can fix that up later. But the unset call in the goroutine is not correct.

@otoolep

otoolep commented Jun 2, 2026

Copy link
Copy Markdown
Member

Keep the mock dialer for now, it's fine. The polling is definitely wrong, as you agree.

Reject excess cluster service connections instead of spawning unbounded handler goroutines under connection floods.
@shrtyk
shrtyk force-pushed the fix/service-connection-limit branch from f36e51b to 49e04f8 Compare June 2, 2026 10:22
@shrtyk

shrtyk commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

I kept the mock dialer and removed the polling from the test. I also moved s.open.Unset() out of the serve() goroutine, and trimmed the test so it focuses on the connection limiter rather than the returned node metadata.

@otoolep
otoolep merged commit 9d8c200 into rqlite:master Jun 2, 2026
@otoolep

otoolep commented Jun 2, 2026

Copy link
Copy Markdown
Member

Thanks for your contribution @shrtyk

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.

Service.serve has no connection limit

2 participants