Skip to content

Commit

Permalink
Sleep between every socket polling
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed May 15, 2023
1 parent 225d890 commit 489e63b
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions Sources/Socket/SocketManager/AsyncSocketManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,8 @@ private extension AsyncSocketManager {
}
tasks.removeAll(keepingCapacity: true)
// sleep
if hasEvents == false {
try await Task.sleep(nanoseconds: state.configuration.monitorInterval)
}
let sleepInterval = state.configuration.monitorInterval * (hasEvents ? 1 : 2)
try await Task.sleep(nanoseconds: sleepInterval)
}
catch {
log("Socket monitoring failed. \(error.localizedDescription)")
Expand Down Expand Up @@ -321,20 +320,17 @@ private extension AsyncSocketManager {
preconditionFailure()
continue
}
process(poll, socket: state, tasks: &tasks)
let task = process(poll, socket: state)
tasks.append(task)
}
}
return hasEvents
}

func process(_ poll: SocketDescriptor.Poll, socket: AsyncSocketManager.SocketState, tasks: inout [Task<Void, Never>]) {
let task = Task {
func process(_ poll: SocketDescriptor.Poll, socket: AsyncSocketManager.SocketState) -> Task<Void, Never> {
Task(priority: state.configuration.monitorPriority) {
if poll.returnedEvents.contains(.read) {
if await socket.isListening {
await socket.event(.read, notification: .connection)
} else {
await socket.event(.read, notification: .read)
}
await socket.event(.read, notification: socket.isListening ? .connection : .read)
}
if poll.returnedEvents.contains(.write) {
await socket.event(.write, notification: .write)
Expand All @@ -349,7 +345,6 @@ private extension AsyncSocketManager {
hangup(poll.socket)
}
}
tasks.append(task)
}

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

0 comments on commit 489e63b

Please sign in to comment.