Skip to content

Commit

Permalink
Only sleep if no events returned
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Oct 5, 2022
1 parent 257a81d commit d4d04af
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions Sources/Socket/SocketManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ internal final class SocketManager {
Task.detached(priority: Socket.configuration.monitorPriority) { [unowned self] in
while await self.isMonitoring {
do {
try await Task.sleep(nanoseconds: Socket.configuration.monitorInterval)
try await storage.update {
let hasEvents = try await storage.update {
// poll
try $0.poll()
let hasEvents = try $0.poll()
// stop monitoring if no sockets
if $0.pollDescriptors.isEmpty {
$0.isMonitoring = false
}
return hasEvents
}
if hasEvents == false {
try await Task.sleep(nanoseconds: Socket.configuration.monitorInterval)
}
}
catch {
Expand Down Expand Up @@ -221,7 +224,9 @@ extension SocketManager.ManagerState {
return poll.returnedEvents
}

mutating func poll() throws {
/// Has events.
@discardableResult
mutating func poll() throws -> Bool {
// build poll descriptor array
let sockets = self.sockets
.lazy
Expand All @@ -242,24 +247,28 @@ extension SocketManager.ManagerState {
throw error
}
// wait for concurrent handling
for poll in pollDescriptors {
if poll.returnedEvents.contains(.write) {
canWrite(poll.socket)
}
if poll.returnedEvents.contains(.read) {
shouldRead(poll.socket)
}
if poll.returnedEvents.contains(.invalidRequest) {
assertionFailure("Polled for invalid socket \(poll.socket)")
error(.badFileDescriptor, for: poll.socket)
}
if poll.returnedEvents.contains(.hangup) {
error(.connectionReset, for: poll.socket)
}
if poll.returnedEvents.contains(.error) {
error(.connectionAbort, for: poll.socket)
let hasEvents = pollDescriptors.contains(where: { $0.returnedEvents.isEmpty == false })
if hasEvents {
for poll in pollDescriptors {
if poll.returnedEvents.contains(.write) {
canWrite(poll.socket)
}
if poll.returnedEvents.contains(.read) {
shouldRead(poll.socket)
}
if poll.returnedEvents.contains(.invalidRequest) {
assertionFailure("Polled for invalid socket \(poll.socket)")
error(.badFileDescriptor, for: poll.socket)
}
if poll.returnedEvents.contains(.hangup) {
error(.connectionReset, for: poll.socket)
}
if poll.returnedEvents.contains(.error) {
error(.connectionAbort, for: poll.socket)
}
}
}
return hasEvents
}

mutating func error(_ error: Errno, for fileDescriptor: SocketDescriptor) {
Expand Down

0 comments on commit d4d04af

Please sign in to comment.