From cb4eaa244f60226ed30025dda0907bef1a091855 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 9 Jul 2024 19:09:29 +0200 Subject: [PATCH 1/4] savepoint --- .../Misc/NetworkConstants.swift | 3 +- .../WalletConnectRelay/RelayURLFactory.swift | 8 ++--- .../AutomaticSocketConnectionHandler.swift | 24 +++++---------- .../ManualSocketConnectionHandler.swift | 19 ++++-------- .../SocketConnectionHandler.swift | 1 - .../SocketUrlFallbackHandler.swift | 29 ------------------- 6 files changed, 16 insertions(+), 68 deletions(-) delete mode 100644 Sources/WalletConnectRelay/SocketUrlFallbackHandler.swift diff --git a/Sources/WalletConnectRelay/Misc/NetworkConstants.swift b/Sources/WalletConnectRelay/Misc/NetworkConstants.swift index 5a23a21c8..9f2d6f259 100644 --- a/Sources/WalletConnectRelay/Misc/NetworkConstants.swift +++ b/Sources/WalletConnectRelay/Misc/NetworkConstants.swift @@ -1,4 +1,3 @@ enum NetworkConstants { - static var defaultUrl = "relay.walletconnect.com" - static var fallbackUrl = "relay.walletconnect.org" + static var defaultUrl = "relay.walletconnect.org" } diff --git a/Sources/WalletConnectRelay/RelayURLFactory.swift b/Sources/WalletConnectRelay/RelayURLFactory.swift index 20290fd24..5809c89fa 100644 --- a/Sources/WalletConnectRelay/RelayURLFactory.swift +++ b/Sources/WalletConnectRelay/RelayURLFactory.swift @@ -18,19 +18,15 @@ class RelayUrlFactory { self.socketAuthenticator = socketAuthenticator } - func setFallback() { - self.fallback = true - } - func create() -> URL { var components = URLComponents() components.scheme = "wss" - components.host = fallback ? NetworkConstants.fallbackUrl : relayHost + components.host = relayHost components.queryItems = [ URLQueryItem(name: "projectId", value: projectId) ] do { - let authToken = try socketAuthenticator.createAuthToken(url: fallback ? "wss://" + NetworkConstants.fallbackUrl : "wss://" + relayHost) + let authToken = try socketAuthenticator.createAuthToken(url: "wss://" + relayHost) components.queryItems?.append(URLQueryItem(name: "auth", value: authToken)) } catch { // TODO: Handle token creation errors diff --git a/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift b/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift index 3be94af13..a757884c1 100644 --- a/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift +++ b/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift @@ -14,9 +14,8 @@ class AutomaticSocketConnectionHandler { private let appStateObserver: AppStateObserving private let networkMonitor: NetworkMonitoring private let backgroundTaskRegistrar: BackgroundTaskRegistering - private let defaultTimeout: Int = 5 + private let defaultTimeout: Int = 30 private let logger: ConsoleLogging - private var socketUrlFallbackHandler: SocketUrlFallbackHandler private var publishers = Set() private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.automatic_socket_connection", qos: .utility, attributes: .concurrent) @@ -27,24 +26,16 @@ class AutomaticSocketConnectionHandler { appStateObserver: AppStateObserving = AppStateObserver(), backgroundTaskRegistrar: BackgroundTaskRegistering = BackgroundTaskRegistrar(), logger: ConsoleLogging, - socketUrlFallbackHandler: SocketUrlFallbackHandler ) { self.appStateObserver = appStateObserver self.socket = socket self.networkMonitor = networkMonitor self.backgroundTaskRegistrar = backgroundTaskRegistrar self.logger = logger - self.socketUrlFallbackHandler = socketUrlFallbackHandler setUpStateObserving() setUpNetworkMonitoring() - socketUrlFallbackHandler.onTryReconnect = { [unowned self] in - Task(priority: .high) { - await tryReconect() - } - } - connect() } @@ -63,7 +54,7 @@ class AutomaticSocketConnectionHandler { } if !self.socket.isConnected { self.logger.debug("Connection timed out, initiating fallback...") - self.socketUrlFallbackHandler.handleFallbackIfNeeded(error: .connectionFailed) + retryToConnect() } timer.cancel() } @@ -99,6 +90,12 @@ class AutomaticSocketConnectionHandler { socket.disconnect() } + private func retryToConnect() { + if !socket.isConnected { + socket.connect() + } + } + private func reconnectIfNeeded() { if !socket.isConnected { socket.connect() @@ -109,11 +106,6 @@ class AutomaticSocketConnectionHandler { // MARK: - SocketConnectionHandler extension AutomaticSocketConnectionHandler: SocketConnectionHandler { - func tryReconect() async { - guard await appStateObserver.currentState == .foreground else { return } - reconnectIfNeeded() - } - func handleConnect() throws { throw Errors.manualSocketConnectionForbidden } diff --git a/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift b/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift index d0589ca9e..100f8bd3c 100644 --- a/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift +++ b/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift @@ -4,23 +4,14 @@ class ManualSocketConnectionHandler: SocketConnectionHandler { private let socket: WebSocketConnecting private let logger: ConsoleLogging - private let defaultTimeout: Int = 5 - private var socketUrlFallbackHandler: SocketUrlFallbackHandler + private let defaultTimeout: Int = 30 private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.manual_socket_connection", attributes: .concurrent) init( socket: WebSocketConnecting, - logger: ConsoleLogging, - socketUrlFallbackHandler: SocketUrlFallbackHandler) { + logger: ConsoleLogging) { self.socket = socket self.logger = logger - self.socketUrlFallbackHandler = socketUrlFallbackHandler - - socketUrlFallbackHandler.onTryReconnect = { [unowned self] in - Task(priority: .high) { - await tryReconect() - } - } } func handleConnect() throws { @@ -34,8 +25,8 @@ class ManualSocketConnectionHandler: SocketConnectionHandler { return } if !self.socket.isConnected { - self.logger.debug("Connection timed out, initiating fallback...") - self.socketUrlFallbackHandler.handleFallbackIfNeeded(error: .connectionFailed) + self.logger.debug("Connection timed out, will rety to connect...") + retryToConnect() } timer.cancel() } @@ -51,7 +42,7 @@ class ManualSocketConnectionHandler: SocketConnectionHandler { // ManualSocketConnectionHandler does not support reconnection logic } - func tryReconect() async { + private func retryToConnect() { if !socket.isConnected { socket.connect() } diff --git a/Sources/WalletConnectRelay/SocketConnectionHandler/SocketConnectionHandler.swift b/Sources/WalletConnectRelay/SocketConnectionHandler/SocketConnectionHandler.swift index 91284893b..4ac3046dd 100644 --- a/Sources/WalletConnectRelay/SocketConnectionHandler/SocketConnectionHandler.swift +++ b/Sources/WalletConnectRelay/SocketConnectionHandler/SocketConnectionHandler.swift @@ -4,5 +4,4 @@ protocol SocketConnectionHandler { func handleConnect() throws func handleDisconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws func handleDisconnection() async - func tryReconect() async } diff --git a/Sources/WalletConnectRelay/SocketUrlFallbackHandler.swift b/Sources/WalletConnectRelay/SocketUrlFallbackHandler.swift deleted file mode 100644 index dea30eecd..000000000 --- a/Sources/WalletConnectRelay/SocketUrlFallbackHandler.swift +++ /dev/null @@ -1,29 +0,0 @@ -import Foundation - -class SocketUrlFallbackHandler { - private let relayUrlFactory: RelayUrlFactory - private var logger: ConsoleLogging - private var socket: WebSocketConnecting - private let networkMonitor: NetworkMonitoring - var onTryReconnect: (()->())? - - init( - relayUrlFactory: RelayUrlFactory, - logger: ConsoleLogging, - socket: WebSocketConnecting, - networkMonitor: NetworkMonitoring) { - self.relayUrlFactory = relayUrlFactory - self.logger = logger - self.socket = socket - self.networkMonitor = networkMonitor - } - - func handleFallbackIfNeeded(error: NetworkError) { - if error == .connectionFailed && socket.request.url?.host == NetworkConstants.defaultUrl { - logger.debug("[WebSocket] - Fallback to \(NetworkConstants.fallbackUrl)") - relayUrlFactory.setFallback() - socket.request.url = relayUrlFactory.create() - onTryReconnect?() - } - } -} From b410943c4eadfd30c544bd9cfa5bb8b64bbb29cf Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 10 Jul 2024 08:08:58 +0200 Subject: [PATCH 2/4] remove fallback handler --- Sources/WalletConnectRelay/RelayClientFactory.swift | 11 +++-------- .../AutomaticSocketConnectionHandler.swift | 8 ++++---- .../ManualSocketConnectionHandler.swift | 2 +- Tests/RelayerTests/DispatcherTests.swift | 3 +-- .../ManualSocketConnectionHandlerTests.swift | 3 +-- 5 files changed, 10 insertions(+), 17 deletions(-) diff --git a/Sources/WalletConnectRelay/RelayClientFactory.swift b/Sources/WalletConnectRelay/RelayClientFactory.swift index 3a4097336..2120b720e 100644 --- a/Sources/WalletConnectRelay/RelayClientFactory.swift +++ b/Sources/WalletConnectRelay/RelayClientFactory.swift @@ -61,16 +61,11 @@ public struct RelayClientFactory { if let bundleId = Bundle.main.bundleIdentifier { socket.request.addValue(bundleId, forHTTPHeaderField: "Origin") } - let socketFallbackHandler = SocketUrlFallbackHandler( - relayUrlFactory: relayUrlFactory, - logger: logger, - socket: socket, - networkMonitor: networkMonitor - ) + var socketConnectionHandler: SocketConnectionHandler! switch socketConnectionType { - case .automatic: socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket, logger: logger, socketUrlFallbackHandler: socketFallbackHandler) - case .manual: socketConnectionHandler = ManualSocketConnectionHandler(socket: socket, logger: logger, socketUrlFallbackHandler: socketFallbackHandler) + case .automatic: socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket, logger: logger) + case .manual: socketConnectionHandler = ManualSocketConnectionHandler(socket: socket, logger: logger) } let dispatcher = Dispatcher( diff --git a/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift b/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift index a757884c1..c9ea12219 100644 --- a/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift +++ b/Sources/WalletConnectRelay/SocketConnectionHandler/AutomaticSocketConnectionHandler.swift @@ -14,7 +14,7 @@ class AutomaticSocketConnectionHandler { private let appStateObserver: AppStateObserving private let networkMonitor: NetworkMonitoring private let backgroundTaskRegistrar: BackgroundTaskRegistering - private let defaultTimeout: Int = 30 + private let defaultTimeout: Int = 60 private let logger: ConsoleLogging private var publishers = Set() @@ -25,7 +25,7 @@ class AutomaticSocketConnectionHandler { networkMonitor: NetworkMonitoring = NetworkMonitor(), appStateObserver: AppStateObserving = AppStateObserver(), backgroundTaskRegistrar: BackgroundTaskRegistering = BackgroundTaskRegistrar(), - logger: ConsoleLogging, + logger: ConsoleLogging ) { self.appStateObserver = appStateObserver self.socket = socket @@ -53,7 +53,7 @@ class AutomaticSocketConnectionHandler { return } if !self.socket.isConnected { - self.logger.debug("Connection timed out, initiating fallback...") + self.logger.debug("Connection timed out, will rety to connect...") retryToConnect() } timer.cancel() @@ -92,7 +92,7 @@ class AutomaticSocketConnectionHandler { private func retryToConnect() { if !socket.isConnected { - socket.connect() + connect() } } diff --git a/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift b/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift index 100f8bd3c..04152bd21 100644 --- a/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift +++ b/Sources/WalletConnectRelay/SocketConnectionHandler/ManualSocketConnectionHandler.swift @@ -4,7 +4,7 @@ class ManualSocketConnectionHandler: SocketConnectionHandler { private let socket: WebSocketConnecting private let logger: ConsoleLogging - private let defaultTimeout: Int = 30 + private let defaultTimeout: Int = 60 private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.manual_socket_connection", attributes: .concurrent) init( diff --git a/Tests/RelayerTests/DispatcherTests.swift b/Tests/RelayerTests/DispatcherTests.swift index 4a58cfd97..e8b0de168 100644 --- a/Tests/RelayerTests/DispatcherTests.swift +++ b/Tests/RelayerTests/DispatcherTests.swift @@ -71,8 +71,7 @@ final class DispatcherTests: XCTestCase { projectId: "1012db890cf3cfb0c1cdc929add657ba", socketAuthenticator: socketAuthenticator ) - let socketUrlFallbackHandler = SocketUrlFallbackHandler(relayUrlFactory: relayUrlFactory, logger: logger, socket: webSocket, networkMonitor: networkMonitor) - let socketConnectionHandler = ManualSocketConnectionHandler(socket: webSocket, logger: logger, socketUrlFallbackHandler: socketUrlFallbackHandler) + let socketConnectionHandler = ManualSocketConnectionHandler(socket: webSocket, logger: logger) sut = Dispatcher( socketFactory: webSocketFactory, relayUrlFactory: relayUrlFactory, diff --git a/Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift b/Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift index 6f8a939cb..d86fbc9cf 100644 --- a/Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift +++ b/Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift @@ -22,9 +22,8 @@ final class ManualSocketConnectionHandlerTests: XCTestCase { projectId: "1012db890cf3cfb0c1cdc929add657ba", socketAuthenticator: socketAuthenticator ) - let socketUrlFallbackHandler = SocketUrlFallbackHandler(relayUrlFactory: relayUrlFactory, logger: ConsoleLoggerMock(), socket: socket, networkMonitor: networkMonitor) - sut = ManualSocketConnectionHandler(socket: socket, logger: ConsoleLoggerMock(), socketUrlFallbackHandler: socketUrlFallbackHandler) + sut = ManualSocketConnectionHandler(socket: socket, logger: ConsoleLoggerMock()) } func testHandleDisconnect() { From dbbd7f9e423418cda6ff1625862d95a19d0fdd5e Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 10 Jul 2024 08:10:03 +0200 Subject: [PATCH 3/4] fix tests --- .../RelayerTests/AutomaticSocketConnectionHandlerTests.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift b/Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift index b29a830ba..368d25da4 100644 --- a/Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift +++ b/Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift @@ -28,14 +28,12 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase { socketAuthenticator: socketAuthenticator ) backgroundTaskRegistrar = BackgroundTaskRegistrarMock() - let socketUrlFallbackHandler = SocketUrlFallbackHandler(relayUrlFactory: relayUrlFactory, logger: ConsoleLoggerMock(), socket: webSocket, networkMonitor: networkMonitor) sut = AutomaticSocketConnectionHandler( socket: webSocketSession, networkMonitor: networkMonitor, appStateObserver: appStateObserver, backgroundTaskRegistrar: backgroundTaskRegistrar, - logger: ConsoleLoggerMock(), - socketUrlFallbackHandler: socketUrlFallbackHandler + logger: ConsoleLoggerMock() ) } From 96a1ae89dfa5ba75f576af36b70b6bc023d50b3c Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 10 Jul 2024 13:04:55 +0200 Subject: [PATCH 4/4] fix tests --- Example/RelayIntegrationTests/RelayClientEndToEndTests.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift index eef6758de..e3888e786 100644 --- a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift +++ b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift @@ -52,9 +52,7 @@ final class RelayClientEndToEndTests: XCTestCase { socketAuthenticator: socketAuthenticator ) - let socketUrlFallbackHandler = SocketUrlFallbackHandler(relayUrlFactory: relayUrlFactory, logger: logger, socket: socket, networkMonitor: networkMonitor) - - let socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket, logger: logger, socketUrlFallbackHandler: socketUrlFallbackHandler) + let socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket, logger: logger) let dispatcher = Dispatcher( socketFactory: webSocketFactory, relayUrlFactory: urlFactory,