-
Notifications
You must be signed in to change notification settings - Fork 24
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 support for unix domain sockets #187
Conversation
Codecov Report
@@ Coverage Diff @@
## master #187 +/- ##
==========================================
+ Coverage 68.35% 68.63% +0.27%
==========================================
Files 20 20
Lines 1324 1358 +34
==========================================
+ Hits 905 932 +27
- Misses 419 426 +7
Continue to review full report at Codecov.
|
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.
Although the initial purpose was to provide client-side support, I think it's worth making the server-side facility public also (since this implements it). We need to get the -net and -NIO implementations in sync with respect to this (and naming), then merge and tag both.
I'll work on the corresponding Kitura PR.
Sources/KituraNet/HTTP/HTTP.swift
Outdated
@@ -48,8 +48,8 @@ public class HTTP { | |||
return ClientRequest(url: url, callback: callback) | |||
} | |||
|
|||
public static func request(_ options: [ClientRequest.Options], callback: @escaping ClientRequest.Callback) -> ClientRequest { | |||
return ClientRequest(options: options, callback: callback) | |||
public static func request(_ options: [ClientRequest.Options], socketPath: String? = nil, callback: @escaping ClientRequest.Callback) -> ClientRequest { |
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 whole function seems to be missing API documentation... Kitura-net's doc is far from exemplary in this instance, but does attempt to describe the params.
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.
A recently merged PR has copied all of the documentation comments from Kitura-net to Kitura-NIO :)
@@ -207,8 +210,9 @@ public class ClientRequest { | |||
/// | |||
/// - Parameter options: An array of `Options' describing the request | |||
/// - Parameter callback: The closure of type `Callback` to be used for the callback. | |||
init(options: [Options], callback: @escaping Callback) { | |||
init(options: [Options], socketPath: String? = nil, callback: @escaping Callback) { |
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.
Let's call this parameter unixDomainSocketPath
to be consistent.
@@ -34,6 +34,8 @@ public class HTTPServer: Server { | |||
/// Port number for listening for new connections. | |||
public private(set) var port: Int? | |||
|
|||
var unixDomainSocketPath: String? |
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.
In Kitura/Kitura-net#296, I went with
/// The Unix domain socket path on which this server listens for new connections. If `nil`, this server does not listen on a Unix socket.
public private(set) var unixDomainSocketPath: String?
...as we provide a similar property for accessing the port. I also updated the API doc for port
to include:
If
nil
, this server does not listen on a TCP socket.
@@ -160,11 +162,25 @@ public class HTTPServer: Server { | |||
return nil | |||
} | |||
|
|||
enum SocketType { |
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.
Should this be private
?
case unixDomainSocket(String) | ||
} | ||
|
||
func listen(unixDomainSocketPath: String) throws { |
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 should be public
to match public func listen(on port: Int)
. It could also do with documentation (could be a copy-paste of Kitura/Kitura-net#296)
@@ -39,10 +39,25 @@ class LargePayloadTests: KituraNetTest { | |||
|
|||
private let delegate = TestServerDelegate() | |||
|
|||
private func uniqueTemporaryFilePath() -> String { |
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 all looks fine, I used a slightly different implementation in the equivalent Kitura-net PR - take a look at my setUp()
and tearDown()
in Tests/KituraNetTests/UnixSocketTests.swift
- in particular, it would be nice to not leave a bunch of temporary files lying around after running the tests.
Also, is it possible for the test to assert that the client is connecting over a Unix socket? I did this in the test I referred to above, but I'm not sure if this is feasible with NIO.
7602ff8
to
05370fe
Compare
Thanks for the review @djones6 I've addressed all of your comments. |
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.
Changes look good to me now, however, I'd like to understand why the CI for Kitura is failing here: Kitura/Kitura#1434
I'm trying to run the whole Kitura test suite over Unix sockets. This works for Kitura-net, but the tests that send a redirect seem to be failing with NIO when using Unix sockets.
@pushkarnk I pushed f04d4eb because it is otherwise not possible to start, stop and then restart the server. This also makes it consistent with BlueSocket, which removes the file at the socket path if it already exists. |
serverChannel = try bootstrap.bind(host: "0.0.0.0", port: port).wait() | ||
switch socket { | ||
case SocketType.tcp(let port): | ||
listenerDescription = "port \(port)" |
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 just realized this isn't quite right: this will result in port 0
when listening on an ephemeral port.
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've fixed this in 450882a
@djones6 Redirection creates new This commit handles the case of the With this the Kitura tests must pass. |
This is a replication of the unix domain socket support on Kitura-net. This pull request proposes an additive change to the
HTTP.request()
andClientRequest.init(options:)
APIs via the addition of a new optional argument namedsocketPath
, which defaults to nil to ensure zero breakage of ClientRequest. That's about the client side support. For the sake of testing, we also have an internallisten(unixDomainSocket:)
that provides server-side support for listening on unix domain sockets. One of the existing Kitura-NIO teststestLargePosts
is modified to work with a unix socket.