Skip to content

Commit

Permalink
Merge branch 'envoy:master' into bugfix/crash-atomics-value
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudWurmel authored Jun 17, 2021
2 parents 1e11704 + e87be01 commit 6f84ef7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ Then you can visit `http://[::1]:8080/foo-bar` in the browser and see
the path you're visiting is "/foo-bar"
```

By default, the server will be bound only to the localhost interface (::1) for security reasons. If you want to access your server over an external network, you'll need to change the bind interface to all addresses:

```Swift
let server = DefaultHTTPServer(eventLoop: loop, interface: "::", port: 8080)
```

## Async Event Loop

To use the async event loop, you can get it via key `embassy.event_loop` in `environ` dictionary and cast it to `EventLoop`. For example, you can create an SWSGI app which delays `sendBody` call like this
Expand Down
52 changes: 26 additions & 26 deletions Sources/KqueueSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ public final class KqueueSelector: Selector {
}

// register events to kqueue

// Notice: we need to get the event count before we go into
// `withUnsafeMutableBufferPointer`, as we cannot rely on it inside the closure
// (you can read the offical document)
let keventCount = kevents.count
guard kevents.withUnsafeMutableBufferPointer({ pointer in
kevent(kqueue, pointer.baseAddress, Int32(keventCount), nil, Int32(0), nil) >= 0
kevent(kqueue, pointer.baseAddress, Int32(pointer.count), nil, Int32(0), nil) >= 0
}) else {
throw OSError.lastIOError()
}
Expand Down Expand Up @@ -107,13 +102,8 @@ public final class KqueueSelector: Selector {
}

// unregister events from kqueue

// Notice: we need to get the event count before we go into
// `withUnsafeMutableBufferPointer`, as we cannot rely on it inside the closure
// (you can read the offical document)
let keventCount = kevents.count
guard kevents.withUnsafeMutableBufferPointer({ pointer in
kevent(kqueue, pointer.baseAddress, Int32(keventCount), nil, Int32(0), nil) >= 0
kevent(kqueue, pointer.baseAddress, Int32(pointer.count), nil, Int32(0), nil) >= 0
}) else {
throw OSError.lastIOError()
}
Expand All @@ -126,7 +116,6 @@ public final class KqueueSelector: Selector {

public func select(timeout: TimeInterval?) throws -> [(SelectorKey, Set<IOEvent>)] {
var timeSpec: timespec?
let timeSpecPointer: UnsafePointer<timespec>?
if let timeout = timeout {
if timeout > 0 {
var integer = 0.0
Expand All @@ -135,21 +124,20 @@ public final class KqueueSelector: Selector {
} else {
timeSpec = timespec()
}
timeSpecPointer = withUnsafePointer(to: &timeSpec!) { $0 }
} else {
timeSpecPointer = nil
}

var kevents = Array<Darwin.kevent>(repeating: Darwin.kevent(), count: selectMaximumEvent)
let eventCount = kevents.withUnsafeMutableBufferPointer { pointer in
return kevent(
kqueue,
nil,
0,
pointer.baseAddress,
Int32(selectMaximumEvent),
timeSpecPointer
)
let eventCount:Int32 = kevents.withUnsafeMutableBufferPointer { pointer in
return withUnsafeOptionalPointer(to: &timeSpec) { timeSpecPointer in
return kevent(
kqueue,
nil,
0,
pointer.baseAddress,
Int32(selectMaximumEvent),
timeSpecPointer
)
}
}
guard eventCount >= 0 else {
throw OSError.lastIOError()
Expand All @@ -167,14 +155,26 @@ public final class KqueueSelector: Selector {
}
fileDescriptorIOEvents[fileDescriptor] = ioEvents
}
return Array(fileDescriptorIOEvents.map { (fileDescriptorMap[$0.0]!, $0.1) })
let fdMap = fileDescriptorMap
return fileDescriptorIOEvents.compactMap { [weak self] event in
fdMap[event.0].map { ($0, event.1) } ?? nil
}
}

public subscript(fileDescriptor: Int32) -> SelectorKey? {
get {
return fileDescriptorMap[fileDescriptor]
}
}

private func withUnsafeOptionalPointer<T, Result>(to: inout T?, body: (UnsafePointer<T>?) throws -> Result) rethrows -> Result {
if to != nil {
return try withUnsafePointer(to: &to!) { try body($0) }
} else {
return try body(nil)
}
}

}

#endif

0 comments on commit 6f84ef7

Please sign in to comment.