Skip to content

Commit 5434cda

Browse files
committed
pending の取りこぼしを防ぐ
1 parent 31769e0 commit 5434cda

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

Sora/RPC.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,25 +144,35 @@ public final class RPCChannel {
144144
return false
145145
}
146146

147-
Logger.debug(type: .dataChannel, message: "send rpc: \(payload)")
148-
let sent = dataChannel.send(data)
149-
if !sent {
150-
completion?(.failure(SoraError.rpcTransportClosed(reason: "failed to send rpc message")))
151-
return false
152-
}
153-
147+
var pending: Pending?
154148
if !isNotificationRequest, let identifier {
155149
let workItem = DispatchWorkItem { [weak self] in
156150
self?.finishPending(id: identifier, result: .failure(SoraError.rpcTimeout))
157151
}
158-
let pending = Pending(
152+
let createdPending = Pending(
159153
completion: { result in completion?(result) },
160154
timeoutWorkItem: workItem)
155+
pending = createdPending
156+
queue.sync(flags: .barrier) {
157+
self.pendings[identifier] = createdPending
158+
}
159+
}
161160

162-
queue.async(flags: .barrier) { [weak self] in
163-
self?.pendings[identifier] = pending
161+
Logger.debug(type: .dataChannel, message: "send rpc: \(payload)")
162+
let sent = dataChannel.send(data)
163+
if !sent {
164+
if let identifier {
165+
finishPending(
166+
id: identifier,
167+
result: .failure(SoraError.rpcTransportClosed(reason: "failed to send rpc message")))
168+
} else {
169+
completion?(.failure(SoraError.rpcTransportClosed(reason: "failed to send rpc message")))
164170
}
165-
DispatchQueue.global().asyncAfter(deadline: .now() + timeout, execute: workItem)
171+
return false
172+
}
173+
174+
if !isNotificationRequest, let pending {
175+
DispatchQueue.global().asyncAfter(deadline: .now() + timeout, execute: pending.timeoutWorkItem)
166176
} else {
167177
completion?(.success(nil))
168178
}

0 commit comments

Comments
 (0)