Skip to content

Commit

Permalink
fix(predictions): use serial queue for WebSocketSession delegate queue
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisabhash committed Jan 3, 2025
1 parent e4a0bff commit 2cdaf7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public final class FaceLivenessSession: LivenessService {
}

public func closeSocket(with code: URLSessionWebSocketTask.CloseCode) {
websocket.close(with: code)
livenessServiceDispatchQueue.async {
self.websocket.close(with: code)
}
}

public func initializeLivenessStream(withSessionID sessionID: String, userAgent: String = "") throws {
Expand All @@ -89,14 +91,16 @@ public final class FaceLivenessSession: LivenessService {

savedURLForReconnect = url
let signedConnectionURL = signer.sign(url: url)
websocket.open(url: signedConnectionURL)
livenessServiceDispatchQueue.async {
self.websocket.open(url: signedConnectionURL)
}
}

public func send<T>(
_ event: LivenessEvent<T>,
eventDate: @escaping () -> Date = Date.init
) {
livenessServiceDispatchQueue.sync {
livenessServiceDispatchQueue.async {
let encodedPayload = self.eventStreamEncoder.encode(
payload: event.payload,
headers: [
Expand All @@ -107,7 +111,7 @@ public final class FaceLivenessSession: LivenessService {
)

let dateForSigning: Date
if let serverDate = serverDate {
if let serverDate = self.serverDate {
dateForSigning = serverDate
} else {
dateForSigning = eventDate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ final class WebSocketSession {
private var receiveMessage: ((Result<URLSessionWebSocketTask.Message, Error>) -> WebSocketMessageResult)?
private var onSocketClosed: ((URLSessionWebSocketTask.CloseCode) -> Void)?
private var onServerDateReceived: ((Date?) -> Void)?
private let delegateQueue: OperationQueue

init() {
self.delegateQueue = OperationQueue()
self.delegateQueue.maxConcurrentOperationCount = 1
self.delegateQueue.qualityOfService = .userInteractive

self.urlSessionWebSocketDelegate = Delegate()

self.session = URLSession(
configuration: .default,
delegate: urlSessionWebSocketDelegate,
delegateQueue: .init()
delegateQueue: delegateQueue
)
}

Expand Down

0 comments on commit 2cdaf7f

Please sign in to comment.