mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
syscall wrapper: don't do an allocation for every failed syscall
This commit is contained in:
1 parent
ba46fc0f46
commit
215620d7a6
4 files changed
+38
-21
No files matched your search
+31
-15
@@ -19,33 +19,40 @@ import Darwin
|
||||
|
||||
/// An `Error` for an IO operation.
|
||||
public struct IOError: Swift.Error {
|
||||
|
||||
public enum FailureDescription {
|
||||
case function(StaticString)
|
||||
case reason(String)
|
||||
}
|
||||
/// The `errno` that was set for the operation.
|
||||
public let errnoCode: Int32
|
||||
|
||||
// TODO: Fix me to lazy create
|
||||
/// The actual reason (in an human-readable form) for this `IOError`.
|
||||
public let reason: String
|
||||
public let reason: FailureDescription
|
||||
|
||||
/// Creates a new `IOError``
|
||||
///
|
||||
/// - note: At the moment, this constructor is more expensive than `IOError(errnoCode:function:)` as the `String` will incur reference counting
|
||||
///
|
||||
/// - parameters:
|
||||
/// - errorCode: the `errno` that was set for the operation.
|
||||
/// - reason: the actual reason (in an human-readable form).
|
||||
public init(errnoCode: Int32, reason: String) {
|
||||
self.errnoCode = errnoCode
|
||||
self.reason = reason
|
||||
self.reason = .reason(reason)
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new IOError for a function call
|
||||
///
|
||||
/// - parameters:
|
||||
/// - errorCode: the `errno` that was set for the operation.
|
||||
/// - function: the function / syscall that caused the error.
|
||||
/// - returns: error that was created.
|
||||
func ioError(errnoCode: Int32, function: String) -> IOError {
|
||||
return IOError(errnoCode: errnoCode, reason: reasonForError(errnoCode: errno, function: function))
|
||||
/// Creates a new `IOError``
|
||||
///
|
||||
/// - note: This constructor is the cheapest way to create an `IOError`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - errorCode: the `errno` that was set for the operation.
|
||||
/// - function: The function the error happened in, the human readable description will be generated automatically when needed.
|
||||
public init(errnoCode: Int32, function: StaticString) {
|
||||
self.errnoCode = errnoCode
|
||||
self.reason = .function(function)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reason to use when constructing a `IOError`.
|
||||
@@ -54,7 +61,7 @@ func ioError(errnoCode: Int32, function: String) -> IOError {
|
||||
/// - errorCode: the `errno` that was set for the operation.
|
||||
/// - function: the function / syscall that caused the error.
|
||||
/// -returns: the constructed reason.
|
||||
private func reasonForError(errnoCode: Int32, function: String) -> String {
|
||||
private func reasonForError(errnoCode: Int32, function: StaticString) -> String {
|
||||
if let errorDescC = strerror(errnoCode) {
|
||||
let errorDescLen = strlen(errorDescC)
|
||||
return errorDescC.withMemoryRebound(to: UInt8.self, capacity: errorDescLen) { ptr in
|
||||
@@ -66,9 +73,18 @@ private func reasonForError(errnoCode: Int32, function: String) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
extension IOError {
|
||||
extension IOError: CustomStringConvertible {
|
||||
public var description: String {
|
||||
return self.localizedDescription
|
||||
}
|
||||
|
||||
public var localizedDescription: String {
|
||||
return self.reason
|
||||
switch self.reason {
|
||||
case .reason(let reason):
|
||||
return reason
|
||||
case .function(let function):
|
||||
return reasonForError(errnoCode: self.errnoCode, function: function)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ final class Socket : BaseSocket {
|
||||
func finishConnect() throws {
|
||||
let result: Int32 = try getOption(level: SOL_SOCKET, name: SO_ERROR)
|
||||
if result != 0 {
|
||||
throw ioError(errnoCode: result, function: "getsockopt")
|
||||
throw IOError(errnoCode: result, function: "getsockopt")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ private func isBlacklistedErrno(_ code: Int32) -> Bool {
|
||||
|
||||
/* Sorry, we really try hard to not use underscored attributes. In this case however we seem to break the inlining threshold which makes a system call take twice the time, ie. we need this exception. */
|
||||
@inline(__always)
|
||||
private func wrapSyscallMayBlock<T: FixedWidthInteger>(_ fn: () throws -> T , where: StaticString = #function) throws -> IOResult<T> {
|
||||
private func wrapSyscallMayBlock<T: FixedWidthInteger>(_ fn: () throws -> T , where function: StaticString = #function) throws -> IOResult<T> {
|
||||
while true {
|
||||
let res = try fn()
|
||||
if res == -1 {
|
||||
@@ -49,7 +49,7 @@ private func wrapSyscallMayBlock<T: FixedWidthInteger>(_ fn: () throws -> T , wh
|
||||
return .wouldBlock(0)
|
||||
default:
|
||||
assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(strerror(err)!)")
|
||||
throw ioError(errnoCode: err, function: `where`.withUTF8Buffer { String(decoding: $0, as: UTF8.self) })
|
||||
throw IOError(errnoCode: err, function: function)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -59,7 +59,7 @@ private func wrapSyscallMayBlock<T: FixedWidthInteger>(_ fn: () throws -> T , wh
|
||||
|
||||
/* Sorry, we really try hard to not use underscored attributes. In this case however we seem to break the inlining threshold which makes a system call take twice the time, ie. we need this exception. */
|
||||
@inline(__always)
|
||||
private func wrapSyscall<T: FixedWidthInteger>(_ fn: () throws -> T, where: StaticString = #function) throws -> T {
|
||||
private func wrapSyscall<T: FixedWidthInteger>(_ fn: () throws -> T, where function: StaticString = #function) throws -> T {
|
||||
while true {
|
||||
let res = try fn()
|
||||
if res == -1 {
|
||||
@@ -68,7 +68,7 @@ private func wrapSyscall<T: FixedWidthInteger>(_ fn: () throws -> T, where: Stat
|
||||
continue
|
||||
}
|
||||
assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(strerror(err)!)")
|
||||
throw ioError(errnoCode: err, function: `where`.withUTF8Buffer { String(decoding: $0, as: UTF8.self) })
|
||||
throw IOError(errnoCode: err, function: function)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -37,7 +37,8 @@ class SystemTest: XCTestCase {
|
||||
XCTFail("success even though the call was invalid")
|
||||
} catch let e as IOError {
|
||||
XCTAssertEqual(ENOTSOCK, e.errnoCode)
|
||||
XCTAssert(e.reason.contains("\(ENOTSOCK)"))
|
||||
XCTAssert(e.description.contains("setsockopt"))
|
||||
XCTAssert(e.description.contains("\(ENOTSOCK)"))
|
||||
XCTAssert(e.localizedDescription.contains("\(ENOTSOCK)"), "\(e.localizedDescription)")
|
||||
} catch let e {
|
||||
XCTFail("wrong error thrown: \(e)")
|
||||
|
||||
Reference in new issue
Block a user