Skip to content

Commit c49d836

Browse files
authored
fix(predictions): Serialize the dispatch of web socket events (#3558)
1 parent 30728eb commit c49d836

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

AmplifyPlugins/Predictions/AWSPredictionsPlugin/Liveness/Service/FaceLivenessSession.swift

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public final class FaceLivenessSession: LivenessService {
1717
let baseURL: URL
1818
var serverEventListeners: [LivenessEventKind.Server: (FaceLivenessSession.SessionConfiguration) -> Void] = [:]
1919
var onComplete: (ServerDisconnection) -> Void = { _ in }
20+
21+
private let livenessServiceDispatchQueue = DispatchQueue(
22+
label: "com.amazon.aws.amplify.liveness.service",
23+
target: .global()
24+
)
2025

2126
init(
2227
websocket: WebSocketSession,
@@ -77,36 +82,38 @@ public final class FaceLivenessSession: LivenessService {
7782

7883
public func send<T>(
7984
_ event: LivenessEvent<T>,
80-
eventDate: () -> Date = Date.init
85+
eventDate: @escaping () -> Date = Date.init
8186
) {
82-
let encodedPayload = eventStreamEncoder.encode(
83-
payload: event.payload,
84-
headers: [
85-
":content-type": .string("application/json"),
86-
":event-type": .string(event.eventTypeHeader),
87-
":message-type": .string("event")
88-
]
89-
)
90-
91-
let eventDate = eventDate()
92-
93-
let signedPayload = signer.signWithPreviousSignature(
94-
payload: encodedPayload,
95-
dateHeader: (key: ":date", value: eventDate)
96-
)
97-
98-
let encodedEvent = eventStreamEncoder.encode(
99-
payload: encodedPayload,
100-
headers: [
101-
":date": .timestamp(eventDate),
102-
":chunk-signature": .data(signedPayload)
103-
]
104-
)
105-
106-
websocket.send(
107-
message: .data(encodedEvent),
108-
onError: { _ in }
109-
)
87+
livenessServiceDispatchQueue.async {
88+
let encodedPayload = self.eventStreamEncoder.encode(
89+
payload: event.payload,
90+
headers: [
91+
":content-type": .string("application/json"),
92+
":event-type": .string(event.eventTypeHeader),
93+
":message-type": .string("event")
94+
]
95+
)
96+
97+
let eventDate = eventDate()
98+
99+
let signedPayload = self.signer.signWithPreviousSignature(
100+
payload: encodedPayload,
101+
dateHeader: (key: ":date", value: eventDate)
102+
)
103+
104+
let encodedEvent = self.eventStreamEncoder.encode(
105+
payload: encodedPayload,
106+
headers: [
107+
":date": .timestamp(eventDate),
108+
":chunk-signature": .data(signedPayload)
109+
]
110+
)
111+
112+
self.websocket.send(
113+
message: .data(encodedEvent),
114+
onError: { _ in }
115+
)
116+
}
110117
}
111118

112119
private func fallbackDecoding(_ message: EventStream.Message) -> Bool {

AmplifyPlugins/Predictions/AWSPredictionsPlugin/Liveness/Service/FaceLivenessSessionRepresentable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Amplify
1212
public protocol LivenessService {
1313
func send<T>(
1414
_ event: LivenessEvent<T>,
15-
eventDate: () -> Date
15+
eventDate: @escaping () -> Date
1616
)
1717

1818
var onServiceException: (FaceLivenessSessionError) -> Void { get set }

0 commit comments

Comments
 (0)