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

Limit connection earlier #244

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions Sources/KituraNet/HTTP/HTTPRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle
if errorResponseSent { return }
switch request {
case .head(let header):
_ = server.connectionCount.add(1)
if let connectionLimit = server.options.connectionLimit,
server.connectionCount.load() > connectionLimit {
// Reaching connection limit: closing now.
do {
if let (httpStatus, response) = server.options.connectionResponseGenerator(connectionLimit,serverRequest?.remoteAddress ?? "") {
serverResponse = HTTPServerResponse(channel: context.channel, handler: self)
errorResponseSent = true
try serverResponse?.end(with: httpStatus, message: response)
}
} catch {
Log.error("Failed to send error response")
}
context.close(promise: nil)
return
}
serverRequest = HTTPServerRequest(channel: context.channel, requestHead: header, enableSSL: enableSSLVerification)
if let requestSizeLimit = server.options.requestSizeLimit,
let contentLength = header.headers["Content-Length"].first,
Expand All @@ -93,7 +109,7 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle
} catch {
Log.error("Failed to send error response")
}
context.close()
context.close(promise: nil)
}
}
serverRequest = HTTPServerRequest(channel: context.channel, requestHead: header, enableSSL: enableSSLVerification)
Expand Down Expand Up @@ -125,20 +141,6 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle

case .end:
requestSize = 0
server.connectionCount.add(1)
if let connectionLimit = server.options.connectionLimit {
if server.connectionCount.load() > connectionLimit {
do {
if let (httpStatus, response) = server.options.connectionResponseGenerator(connectionLimit,serverRequest?.remoteAddress ?? "") {
serverResponse = HTTPServerResponse(channel: context.channel, handler: self)
errorResponseSent = true
try serverResponse?.end(with: httpStatus, message: response)
}
} catch {
Log.error("Failed to send error response")
}
}
}
serverResponse = HTTPServerResponse(channel: context.channel, handler: self)
//Make sure we use the latest delegate registered with the server
DispatchQueue.global().async {
Expand Down Expand Up @@ -201,6 +203,6 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle
}

func channelInactive(context: ChannelHandlerContext) {
server.connectionCount.sub(1)
_ = server.connectionCount.sub(1)
}
}