Skip to content

Commit

Permalink
Set ChannelOptions.socket to reuse port and address
Browse files Browse the repository at this point in the history
  • Loading branch information
nethraravindran committed Aug 29, 2018
1 parent 8eca112 commit e319ba7
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-nio.git", .branch("master")),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "1.0.1"),
.package(url: "https://github.com/IBM-Swift/BlueSSLService.git", from: "1.0.0"),
.package(url: "https://github.com/IBM-Swift/LoggerAPI.git", from: "1.7.3"),
Expand Down
1 change: 1 addition & 0 deletions Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public class HTTPServer : Server {

let bootstrap = ServerBootstrap(group: eventLoopGroup)
.serverChannelOption(ChannelOptions.backlog, value: BacklogOption.OptionType(self.maxPendingConnections))
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), value: allowPortReuse ? 1 : 0)
.childChannelInitializer { channel in
let httpHandler = HTTPHandler(for: self)
Expand Down
14 changes: 7 additions & 7 deletions Tests/KituraNetTests/ClientE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ClientE2ETests: KituraNetTest {
let delegate = TestServerDelegate()

func testHeadRequests() {
performServerTest(delegate, allowPortReuse: true) { expectation in
performServerTest(delegate) { expectation in
self.performRequest("head", path: "/headtest", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
do {
Expand All @@ -69,7 +69,7 @@ class ClientE2ETests: KituraNetTest {
}

func testKeepAlive() {
performServerTest(delegate, allowPortReuse: true, asyncTasks: { expectation in
performServerTest(delegate, asyncTasks: { expectation in
self.performRequest("get", path: "/posttest", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .OK was \(String(describing: response?.statusCode))")
if let connectionHeader = response?.headers["Connection"] {
Expand Down Expand Up @@ -117,7 +117,7 @@ class ClientE2ETests: KituraNetTest {
}

func testPostRequests() {
performServerTest(delegate, allowPortReuse: true, asyncTasks: { expectation in
performServerTest(delegate, asyncTasks: { expectation in
self.performRequest("post", path: "/posttest", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
do {
Expand Down Expand Up @@ -164,7 +164,7 @@ class ClientE2ETests: KituraNetTest {
}

func testPutRequests() {
performServerTest(delegate, allowPortReuse: true, asyncTasks: { expectation in
performServerTest(delegate, asyncTasks: { expectation in
self.performRequest("put", path: "/puttest", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
do {
Expand Down Expand Up @@ -211,7 +211,7 @@ class ClientE2ETests: KituraNetTest {
}

func testPatchRequests() {
performServerTest(delegate, allowPortReuse: true, asyncTasks: { expectation in
performServerTest(delegate, asyncTasks: { expectation in
self.performRequest("patch", path: "/patchtest", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
do {
Expand Down Expand Up @@ -258,7 +258,7 @@ class ClientE2ETests: KituraNetTest {
}

func testErrorRequests() {
performServerTest(delegate, allowPortReuse: true, asyncTasks: { expectation in
performServerTest(delegate, asyncTasks: { expectation in
self.performRequest("plover", path: "/xzzy", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.badRequest, "Status code wasn't .badrequest was \(String(describing: response?.statusCode))")
expectation.fulfill()
Expand All @@ -267,7 +267,7 @@ class ClientE2ETests: KituraNetTest {
}

func testUrlURL() {
performServerTest(TestURLDelegate(), allowPortReuse: true) { expectation in
performServerTest(TestURLDelegate()) { expectation in
self.performRequest("post", path: ClientE2ETests.urlPath, callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
expectation.fulfill()
Expand Down
4 changes: 2 additions & 2 deletions Tests/KituraNetTests/LargePayloadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LargePayloadTests: KituraNetTest {
private let delegate = TestServerDelegate()

func testLargePosts() {
performServerTest(delegate, useSSL: false, allowPortReuse: true, asyncTasks: { expectation in
performServerTest(delegate, useSSL: false, asyncTasks: { expectation in
let payload = "[" + contentTypesString + "," + contentTypesString + contentTypesString + "," + contentTypesString + "]"
self.performRequest("post", path: "/largepost", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
Expand Down Expand Up @@ -68,7 +68,7 @@ class LargePayloadTests: KituraNetTest {
}

func testLargeGets() {
performServerTest(delegate, useSSL: false, allowPortReuse: true, asyncTasks: { expectation in
performServerTest(delegate, useSSL: false, asyncTasks: { expectation in
// This test is NOT using self.performRequest, in order to test an extra signature of HTTP.request
let request = HTTP.request("http://localhost:\(self.port)/largepost") {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
Expand Down
3 changes: 0 additions & 3 deletions Tests/KituraNetTests/LifecycleListenerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class LifecycleListenerTests: KituraNetTest {
let startExpectation = self.expectation(description: "start")

let server = HTTP.createServer()
server.allowPortReuse = true
server.started {
started = true
startExpectation.fulfill()
Expand Down Expand Up @@ -123,7 +122,6 @@ class LifecycleListenerTests: KituraNetTest {
let startExpectation = self.expectation(description: "start")

let server = HTTP.createServer()
server.allowPortReuse = true
server.started {
startExpectation.fulfill()
}
Expand Down Expand Up @@ -152,7 +150,6 @@ class LifecycleListenerTests: KituraNetTest {
let failedCallbackExpectation = self.expectation(description: "failedCallback")

let server = HTTP.createServer()
server.allowPortReuse = true
server.failed(callback: { error in
failedCallbackExpectation.fulfill()
})
Expand Down
1 change: 0 additions & 1 deletion Tests/KituraNetTests/MonitoringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class MonitoringTests: KituraNetTest {

let server = HTTP.createServer()
server.delegate = TestServerDelegate()
server.allowPortReuse = true
if KituraNetTest.useSSLDefault {
server.sslConfig = KituraNetTest.sslConfig
}
Expand Down
2 changes: 0 additions & 2 deletions Tests/KituraNetTests/PipeliningTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class PipeliningTests : KituraNetTest {
func testPipelining() {
let server = HTTPServer()
server.delegate = Delegate()
server.allowPortReuse = true
try! server.listen(on: 0)
let expectation = self.expectation(description: "test pipelining")
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
Expand Down Expand Up @@ -74,7 +73,6 @@ class PipeliningTests : KituraNetTest {
func testPipeliningSpanningPackets() {
let server = HTTPServer()
server.delegate = Delegate()
server.allowPortReuse = true
try! server.listen(on: 0)
let expectation = self.expectation(description: "test pipelining spanning packets")
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
Expand Down
4 changes: 2 additions & 2 deletions Tests/KituraNetTests/RegressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RegressionTests: KituraNetTest {
do {
let server: HTTPServer
let serverPort: Int
(server, serverPort) = try startEphemeralServer(ClientE2ETests.TestServerDelegate(), useSSL: true, allowPortReuse: true)
(server, serverPort) = try startEphemeralServer(ClientE2ETests.TestServerDelegate(), useSSL: true)
defer {
server.stop()
}
Expand Down Expand Up @@ -85,7 +85,7 @@ class RegressionTests: KituraNetTest {
do {
let server: HTTPServer
let serverPort: Int
(server, serverPort) = try startEphemeralServer(ClientE2ETests.TestServerDelegate(), useSSL: false, allowPortReuse: true)
(server, serverPort) = try startEphemeralServer(ClientE2ETests.TestServerDelegate(), useSSL: false)
defer {
server.stop()
}
Expand Down

0 comments on commit e319ba7

Please sign in to comment.