Basic working connection limiter#50
Basic working connection limiter#50Davidde94 wants to merge 2 commits intoapple:mainfrom Davidde94:de/connection-limit-example
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.
Wanna throw together some actual text here?
| @@ -0,0 +1,64 @@ | |||
|
|
|||
| func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
| let channel = self.unwrapInboundIn(data) | ||
| context.fireChannelRead(data) | ||
| self.currentConnections += 1 |
There was a problem hiding this comment.
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.
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.
May as well just remove the tests entirely.
|
|
||
| import NIO | ||
|
|
||
| class LimitHandler: ChannelDuplexHandler { |
There was a problem hiding this comment.
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
nactive connections at any one time.