Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max connections to netty configuration #3053

Merged
merged 5 commits into from
Jul 22, 2023

Conversation

wydra98
Copy link
Contributor

@wydra98 wydra98 commented Jul 20, 2023

@wydra98 wydra98 marked this pull request as ready for review July 20, 2023 07:05
@wydra98 wydra98 requested review from kciesielski and adamw July 20, 2023 07:05

import java.util.concurrent.atomic.AtomicInteger

@Sharable case class NettyConnectionCounter(maxConnections: Int) extends ChannelInboundHandlerAdapter {
Copy link
Member

Choose a reason for hiding this comment

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

do we need the annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, according documentation with this annotation in Netty class can be safely shared among multiple connections and it prevent race condition.

@@ -22,10 +22,13 @@ object NettyBootstrap {
.childHandler(new ChannelInitializer[Channel] {
override def initChannel(ch: Channel): Unit = {
val nettyConfigBuilder = nettyConfig.initPipeline(nettyConfig)
val connectionCounter = NettyConnectionCounter(nettyConfig.maxConnections)
val pipelineWithConnectionCounter = ch.pipeline().addFirst(connectionCounter)
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we modify the pipeline after calling initPipeline to reliably add this as the first handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so initPipeline return a function which is always call with pipelineWithConnectionCounter parameter. This parameter ensures that connectionCounter is added as the first handler, so i think that code do what you mentioned.

Copy link
Member

Choose a reason for hiding this comment

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

but then we pass it to nettyConfigBuilder which might add other handlers ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but i add connectionCounter by .addFirst method, which ensure that connectionCounter will be execute before any other handlers

Copy link
Member

Choose a reason for hiding this comment

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

But can't code in nettyConfigBuilder add another handler using .addFirst and effectively install an even earlier handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, in that case it is possible, so do you have any ideas how to prevent this situation?

Copy link
Member

Choose a reason for hiding this comment

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

yes - call .addFirst after calling nettyConfigBuilder - won't that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nettyConfigBuilder returns Unit, I need add .addFirst to the parameter in nettyConfigBuilder

Copy link
Member

Choose a reason for hiding this comment

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

I think it's a side-effecting operation, so you can simply grab the pipeline from ch.pipelie() and modify it

@wydra98 wydra98 requested a review from adamw July 20, 2023 09:34

override def channelActive(ctx: ChannelHandlerContext): Unit = {
val counter = connections.incrementAndGet
if (counter <= maxConnections) super.channelActive(ctx) else ctx.close
Copy link
Member

@kciesielski kciesielski Jul 20, 2023

Choose a reason for hiding this comment

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

suggestion: Before closing, let's log a logger.warning(s"Max connections exceeded: $maxConnections")
You can create a logger with

import io.netty.util.internal.logging.InternalLoggerFactory
// ...

private lazy val logger = InternalLoggerFactory.getInstance(getClass)

Afterwards, please test this locally with a low number, like 2. You can have an endpoint which sleeps for a few seconds, then call it many times, in order to generate exceeding max connections. Please verify if this counter works as expected in such a case and if the warning is indeed logged.

@wydra98 wydra98 requested a review from kciesielski July 21, 2023 11:29
Copy link
Member

@kciesielski kciesielski left a comment

Choose a reason for hiding this comment

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

We have tested this together with Adrian using Apache Benchmark (terminal ab tool for sending concurrent requests). The counter seems to be growing and shrinking according to expectations, and exceeding it closes the channel.
One more improvement we may consider is returning an HTTP 503 or 509 (for some reason 509 is not present in Netty constants) instead of breaking the connection, but this may be problematic, as some of my attempts are showing.

@adamw
Copy link
Member

adamw commented Jul 22, 2023

Looks good, thanks!

@adamw adamw merged commit 8e4c180 into master Jul 22, 2023
@mergify mergify bot deleted the add-max-connections-to-netty-configuration branch July 22, 2023 07:48
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.

None yet

3 participants