-
Notifications
You must be signed in to change notification settings - Fork 62
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
Basic working connection limiter #50
base: main
Are you sure you want to change the base?
Conversation
Made this a while ago, might as well get it reviewed or let it die. Original use case was to limit the number of IMAP connections. |
@@ -0,0 +1,3 @@ | |||
# connection-limiter | |||
|
|||
A description of this package. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wanna throw together some actual text here?
@@ -0,0 +1,64 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the license header.
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
let channel = self.unwrapInboundIn(data) | ||
context.fireChannelRead(data) | ||
self.currentConnections += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This value is never decremented. That doesn't seem likely to be right.
channel.pipeline.addHandler(LimitHandler(connectionLimit: 1)) | ||
}) | ||
.serverChannelOption(ChannelOptions.maxMessagesPerRead, value: 1) | ||
.serverChannelOption(ChannelOptions.backlog, value: 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting this to 1
is not something we'd recommend in a real app and it doesn't affect the correctness of the example, so let's remove this.
|
||
final class connection_limiterTests: XCTestCase { | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May as well just remove the tests entirely.
|
||
import NIO | ||
|
||
class LimitHandler: ChannelDuplexHandler { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these examples are often helpful for people who know they want to do something but don't quite know how: as such I think the documentation and comments here should be comprehensive.
Updated this PR to target |
Example of a (very) simple echo server that only allows
n
active connections at any one time.