Repository navigation
Service connection limiter - #2686
Conversation
There was a problem hiding this comment.
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.
|
This looks reasonable -- thanks. |
5515875 to
bb22399
Compare
bb22399 to
a23bddc
Compare
otoolep
left a comment
There was a problem hiding this comment.
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.
a23bddc to
f36e51b
Compare
| } | ||
|
|
||
| func (s *Service) serve() error { | ||
| defer s.open.Unset() |
There was a problem hiding this comment.
Only unset in Close(). Unsetting here is not what someone would expect.
| // 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 { |
There was a problem hiding this comment.
Why does this need to be in a polling function? It either succeeds or it doesn't.
|
|
||
| // 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 { |
There was a problem hiding this comment.
Ditto. There is no reason for this to poll.
There was a problem hiding this comment.
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.
| } | ||
| } | ||
|
|
||
| type mockDialer struct { |
There was a problem hiding this comment.
This seems to be getting very complicated. Why not just use a standard dialer?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
OK, let me take a look at the code again.
There was a problem hiding this comment.
And yes, I jumped the gun when I said writeCommandReadResponse should be used. This can definitely be done more simply, without overcomplicating it.
|
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. |
|
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 Stepping back, I see two options:
One caveat: the race does not really disappear with |
|
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 |
|
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.
f36e51b to
49e04f8
Compare
|
I kept the mock dialer and removed the polling from the test. I also moved |
|
Thanks for your contribution @shrtyk |
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
maxConcurrentConnsto1024for 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