Skip to content

Commit 6f84ef7

Browse files
authored
Merge branch 'envoy:master' into bugfix/crash-atomics-value
2 parents 1e11704 + e87be01 commit 6f84ef7

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Then you can visit `http://[::1]:8080/foo-bar` in the browser and see
5858
the path you're visiting is "/foo-bar"
5959
```
6060

61+
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:
62+
63+
```Swift
64+
let server = DefaultHTTPServer(eventLoop: loop, interface: "::", port: 8080)
65+
```
66+
6167
## Async Event Loop
6268

6369
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

Sources/KqueueSelector.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,8 @@ public final class KqueueSelector: Selector {
6666
}
6767

6868
// register events to kqueue
69-
70-
// Notice: we need to get the event count before we go into
71-
// `withUnsafeMutableBufferPointer`, as we cannot rely on it inside the closure
72-
// (you can read the offical document)
73-
let keventCount = kevents.count
7469
guard kevents.withUnsafeMutableBufferPointer({ pointer in
75-
kevent(kqueue, pointer.baseAddress, Int32(keventCount), nil, Int32(0), nil) >= 0
70+
kevent(kqueue, pointer.baseAddress, Int32(pointer.count), nil, Int32(0), nil) >= 0
7671
}) else {
7772
throw OSError.lastIOError()
7873
}
@@ -107,13 +102,8 @@ public final class KqueueSelector: Selector {
107102
}
108103

109104
// unregister events from kqueue
110-
111-
// Notice: we need to get the event count before we go into
112-
// `withUnsafeMutableBufferPointer`, as we cannot rely on it inside the closure
113-
// (you can read the offical document)
114-
let keventCount = kevents.count
115105
guard kevents.withUnsafeMutableBufferPointer({ pointer in
116-
kevent(kqueue, pointer.baseAddress, Int32(keventCount), nil, Int32(0), nil) >= 0
106+
kevent(kqueue, pointer.baseAddress, Int32(pointer.count), nil, Int32(0), nil) >= 0
117107
}) else {
118108
throw OSError.lastIOError()
119109
}
@@ -126,7 +116,6 @@ public final class KqueueSelector: Selector {
126116

127117
public func select(timeout: TimeInterval?) throws -> [(SelectorKey, Set<IOEvent>)] {
128118
var timeSpec: timespec?
129-
let timeSpecPointer: UnsafePointer<timespec>?
130119
if let timeout = timeout {
131120
if timeout > 0 {
132121
var integer = 0.0
@@ -135,21 +124,20 @@ public final class KqueueSelector: Selector {
135124
} else {
136125
timeSpec = timespec()
137126
}
138-
timeSpecPointer = withUnsafePointer(to: &timeSpec!) { $0 }
139-
} else {
140-
timeSpecPointer = nil
141127
}
142128

143129
var kevents = Array<Darwin.kevent>(repeating: Darwin.kevent(), count: selectMaximumEvent)
144-
let eventCount = kevents.withUnsafeMutableBufferPointer { pointer in
145-
return kevent(
146-
kqueue,
147-
nil,
148-
0,
149-
pointer.baseAddress,
150-
Int32(selectMaximumEvent),
151-
timeSpecPointer
152-
)
130+
let eventCount:Int32 = kevents.withUnsafeMutableBufferPointer { pointer in
131+
return withUnsafeOptionalPointer(to: &timeSpec) { timeSpecPointer in
132+
return kevent(
133+
kqueue,
134+
nil,
135+
0,
136+
pointer.baseAddress,
137+
Int32(selectMaximumEvent),
138+
timeSpecPointer
139+
)
140+
}
153141
}
154142
guard eventCount >= 0 else {
155143
throw OSError.lastIOError()
@@ -167,14 +155,26 @@ public final class KqueueSelector: Selector {
167155
}
168156
fileDescriptorIOEvents[fileDescriptor] = ioEvents
169157
}
170-
return Array(fileDescriptorIOEvents.map { (fileDescriptorMap[$0.0]!, $0.1) })
158+
let fdMap = fileDescriptorMap
159+
return fileDescriptorIOEvents.compactMap { [weak self] event in
160+
fdMap[event.0].map { ($0, event.1) } ?? nil
161+
}
171162
}
172163

173164
public subscript(fileDescriptor: Int32) -> SelectorKey? {
174165
get {
175166
return fileDescriptorMap[fileDescriptor]
176167
}
177168
}
169+
170+
private func withUnsafeOptionalPointer<T, Result>(to: inout T?, body: (UnsafePointer<T>?) throws -> Result) rethrows -> Result {
171+
if to != nil {
172+
return try withUnsafePointer(to: &to!) { try body($0) }
173+
} else {
174+
return try body(nil)
175+
}
176+
}
177+
178178
}
179179

180180
#endif

0 commit comments

Comments
 (0)