From 6c0aa6ef5913bcd819cd058821efef56d461c4a1 Mon Sep 17 00:00:00 2001 From: HenrikJannsen Date: Sat, 7 Dec 2024 12:46:12 +0700 Subject: [PATCH] Make class thread safe. --- .../websocket/RequestResponseHandler.kt | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/shared/domain/src/commonMain/kotlin/network/bisq/mobile/client/websocket/RequestResponseHandler.kt b/shared/domain/src/commonMain/kotlin/network/bisq/mobile/client/websocket/RequestResponseHandler.kt index 0823591e..a3ae7a9d 100644 --- a/shared/domain/src/commonMain/kotlin/network/bisq/mobile/client/websocket/RequestResponseHandler.kt +++ b/shared/domain/src/commonMain/kotlin/network/bisq/mobile/client/websocket/RequestResponseHandler.kt @@ -30,27 +30,30 @@ class RequestResponseHandler(private val sendFunction: suspend (WebSocketMessage webSocketRequest: WebSocketRequest, timeoutMillis: Long = 10_000 ): WebSocketResponse? { - require(requestId == null) { "RequestResponseHandler is designed to be used only once per request ID" } log.i { "Sending request with ID: ${webSocketRequest.requestId}" } - requestId = webSocketRequest.requestId - mutex.withLock { deferredWebSocketResponse = CompletableDeferred() } + mutex.withLock { + require(requestId == null) { "RequestResponseHandler is designed to be used only once per request ID" } + requestId = webSocketRequest.requestId + deferredWebSocketResponse = CompletableDeferred() - try { - sendFunction.invoke(webSocketRequest) - } catch (e: Exception) { - mutex.withLock { deferredWebSocketResponse?.completeExceptionally(e) } - throw e + try { + sendFunction.invoke(webSocketRequest) + } catch (e: Exception) { + deferredWebSocketResponse?.completeExceptionally(e) + throw e + } } - return withTimeout(timeoutMillis) { deferredWebSocketResponse?.await() } } suspend fun onWebSocketResponse(webSocketResponse: WebSocketResponse) { - require(webSocketResponse.requestId == requestId) { "Request ID of response does not match our request ID" } log.i { "Received response for request ID: ${webSocketResponse.requestId}" } - mutex.withLock { deferredWebSocketResponse?.complete(webSocketResponse) } + mutex.withLock { + require(webSocketResponse.requestId == requestId) { "Request ID of response does not match our request ID" } + deferredWebSocketResponse?.complete(webSocketResponse) + } } suspend fun dispose() { @@ -58,7 +61,7 @@ class RequestResponseHandler(private val sendFunction: suspend (WebSocketMessage mutex.withLock { deferredWebSocketResponse?.cancel() deferredWebSocketResponse = null + requestId = null } - requestId = null } } \ No newline at end of file