Skip to content

Commit

Permalink
fix(predictions): Serialize the dispatch of web socket events (#3558)
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisabhash authored Mar 11, 2024
1 parent 30728eb commit c49d836
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public final class FaceLivenessSession: LivenessService {
let baseURL: URL
var serverEventListeners: [LivenessEventKind.Server: (FaceLivenessSession.SessionConfiguration) -> Void] = [:]
var onComplete: (ServerDisconnection) -> Void = { _ in }

private let livenessServiceDispatchQueue = DispatchQueue(
label: "com.amazon.aws.amplify.liveness.service",
target: .global()
)

init(
websocket: WebSocketSession,
Expand Down Expand Up @@ -77,36 +82,38 @@ public final class FaceLivenessSession: LivenessService {

public func send<T>(
_ event: LivenessEvent<T>,
eventDate: () -> Date = Date.init
eventDate: @escaping () -> Date = Date.init
) {
let encodedPayload = eventStreamEncoder.encode(
payload: event.payload,
headers: [
":content-type": .string("application/json"),
":event-type": .string(event.eventTypeHeader),
":message-type": .string("event")
]
)

let eventDate = eventDate()

let signedPayload = signer.signWithPreviousSignature(
payload: encodedPayload,
dateHeader: (key: ":date", value: eventDate)
)

let encodedEvent = eventStreamEncoder.encode(
payload: encodedPayload,
headers: [
":date": .timestamp(eventDate),
":chunk-signature": .data(signedPayload)
]
)

websocket.send(
message: .data(encodedEvent),
onError: { _ in }
)
livenessServiceDispatchQueue.async {
let encodedPayload = self.eventStreamEncoder.encode(
payload: event.payload,
headers: [
":content-type": .string("application/json"),
":event-type": .string(event.eventTypeHeader),
":message-type": .string("event")
]
)

let eventDate = eventDate()

let signedPayload = self.signer.signWithPreviousSignature(
payload: encodedPayload,
dateHeader: (key: ":date", value: eventDate)
)

let encodedEvent = self.eventStreamEncoder.encode(
payload: encodedPayload,
headers: [
":date": .timestamp(eventDate),
":chunk-signature": .data(signedPayload)
]
)

self.websocket.send(
message: .data(encodedEvent),
onError: { _ in }
)
}
}

private func fallbackDecoding(_ message: EventStream.Message) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Amplify
public protocol LivenessService {
func send<T>(
_ event: LivenessEvent<T>,
eventDate: () -> Date
eventDate: @escaping () -> Date
)

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

0 comments on commit c49d836

Please sign in to comment.