Skip to content

Commit

Permalink
Merge pull request #1381 from WalletConnect/remove-org-fallback
Browse files Browse the repository at this point in the history
Remove org fallback
  • Loading branch information
llbartekll authored Jul 10, 2024
2 parents 5314c59 + 96a1ae8 commit d7dafbc
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 88 deletions.
4 changes: 1 addition & 3 deletions Example/RelayIntegrationTests/RelayClientEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions Sources/WalletConnectRelay/Misc/NetworkConstants.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
enum NetworkConstants {
static var defaultUrl = "relay.walletconnect.com"
static var fallbackUrl = "relay.walletconnect.org"
static var defaultUrl = "relay.walletconnect.org"
}
11 changes: 3 additions & 8 deletions Sources/WalletConnectRelay/RelayClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 2 additions & 6 deletions Sources/WalletConnectRelay/RelayURLFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 60
private let logger: ConsoleLogging
private var socketUrlFallbackHandler: SocketUrlFallbackHandler

private var publishers = Set<AnyCancellable>()
private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.automatic_socket_connection", qos: .utility, attributes: .concurrent)
Expand All @@ -26,25 +25,17 @@ class AutomaticSocketConnectionHandler {
networkMonitor: NetworkMonitoring = NetworkMonitor(),
appStateObserver: AppStateObserving = AppStateObserver(),
backgroundTaskRegistrar: BackgroundTaskRegistering = BackgroundTaskRegistrar(),
logger: ConsoleLogging,
socketUrlFallbackHandler: SocketUrlFallbackHandler
logger: ConsoleLogging
) {
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()

}
Expand All @@ -62,8 +53,8 @@ class AutomaticSocketConnectionHandler {
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()
}
Expand Down Expand Up @@ -99,6 +90,12 @@ class AutomaticSocketConnectionHandler {
socket.disconnect()
}

private func retryToConnect() {
if !socket.isConnected {
connect()
}
}

private func reconnectIfNeeded() {
if !socket.isConnected {
socket.connect()
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 60
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 {
Expand All @@ -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()
}
Expand All @@ -51,7 +42,7 @@ class ManualSocketConnectionHandler: SocketConnectionHandler {
// ManualSocketConnectionHandler does not support reconnection logic
}

func tryReconect() async {
private func retryToConnect() {
if !socket.isConnected {
socket.connect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ protocol SocketConnectionHandler {
func handleConnect() throws
func handleDisconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws
func handleDisconnection() async
func tryReconect() async
}
29 changes: 0 additions & 29 deletions Sources/WalletConnectRelay/SocketUrlFallbackHandler.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}

Expand Down
3 changes: 1 addition & 2 deletions Tests/RelayerTests/DispatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions Tests/RelayerTests/ManualSocketConnectionHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d7dafbc

Please sign in to comment.