-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// swift-tools-version:5.2 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "connection-limiter", | ||
dependencies: [ | ||
.package(name: "swift-nio", url: "https://github.com/apple/swift-nio", from: "2.16.0") | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages which this package depends on. | ||
.target( | ||
name: "connection-limiter", | ||
dependencies: [ | ||
.product(name: "NIO", package: "swift-nio") | ||
]), | ||
.testTarget( | ||
name: "connection-limiterTests", | ||
dependencies: ["connection-limiter"]), | ||
] | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# connection-limiter | ||
|
||
A description of this package. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the license header. |
||
import NIO | ||
|
||
class LimitHandler: ChannelDuplexHandler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
typealias OutboundIn = Channel | ||
typealias InboundIn = Channel | ||
|
||
let connectionLimit: Int | ||
var currentConnections = 0 | ||
|
||
init(connectionLimit: Int) { | ||
self.connectionLimit = connectionLimit | ||
} | ||
|
||
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 commentThe 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.closeFuture.whenSuccess { | ||
context.read() | ||
} | ||
} | ||
|
||
func read(context: ChannelHandlerContext) { | ||
guard self.currentConnections < self.connectionLimit else { | ||
return | ||
} | ||
context.read() | ||
} | ||
|
||
} | ||
|
||
class EchoChannelHandler: ChannelInboundHandler { | ||
|
||
typealias InboundIn = ByteBuffer | ||
typealias InboundOut = ByteBuffer | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
var input = self.unwrapInboundIn(data) | ||
var buffer = context.channel.allocator.buffer(capacity: input.readableBytes + 6) | ||
buffer.writeString("Echo: ") | ||
buffer.writeBuffer(&input) | ||
let output = self.wrapInboundOut(buffer) | ||
context.writeAndFlush(output, promise: nil) | ||
} | ||
|
||
} | ||
|
||
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
try ServerBootstrap(group: group) | ||
.serverChannelInitializer({ (channel) -> EventLoopFuture<Void> in | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Setting this to |
||
.childChannelInitializer { (channel) -> EventLoopFuture<Void> in | ||
channel.pipeline.addHandler(EchoChannelHandler()) | ||
} | ||
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) | ||
.bind(host: "127.0.0.1", port: 4321) | ||
.wait() | ||
.closeFuture | ||
.wait() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import XCTest | ||
import class Foundation.Bundle | ||
|
||
final class connection_limiterTests: XCTestCase { | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May as well just remove the tests entirely. |
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?