Skip to content

Commit c462381

Browse files
authored
Merge pull request #629 from Sistemium/development
SocketAckManager thread-safe acks removal
2 parents efefecd + 99faafe commit c462381

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Source/SocketAckManager.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,29 @@ private func ==(lhs: SocketAck, rhs: SocketAck) -> Bool {
5151

5252
struct SocketAckManager {
5353
private var acks = Set<SocketAck>(minimumCapacity: 1)
54+
private let ackSemaphore = DispatchSemaphore(value: 1)
5455

5556
mutating func addAck(_ ack: Int, callback: @escaping AckCallback) {
5657
acks.insert(SocketAck(ack: ack, callback: callback))
5758
}
5859

5960
/// Should be called on handle queue
6061
mutating func executeAck(_ ack: Int, with items: [Any], onQueue: DispatchQueue) {
62+
ackSemaphore.wait()
63+
defer { ackSemaphore.signal() }
6164
let ack = acks.remove(SocketAck(ack: ack))
6265

6366
onQueue.async() { ack?.callback(items) }
6467
}
6568

6669
/// Should be called on handle queue
6770
mutating func timeoutAck(_ ack: Int, onQueue: DispatchQueue) {
71+
ackSemaphore.wait()
72+
defer { ackSemaphore.signal() }
6873
let ack = acks.remove(SocketAck(ack: ack))
6974

70-
onQueue.async() { ack?.callback?(["NO ACK"]) }
75+
onQueue.async() {
76+
ack?.callback?(["NO ACK"])
77+
}
7178
}
7279
}

0 commit comments

Comments
 (0)