From 613c895d75fd2b959df7169d93f828da1c698d41 Mon Sep 17 00:00:00 2001 From: Igor Artamonov Date: Tue, 21 Sep 2021 20:28:19 -0400 Subject: [PATCH] problem: may stick with reconnect if multiple errors happened at the same time --- .../dshackle/upstream/ethereum/EthereumWsFactory.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactory.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactory.kt index 6d24e20b..0321155f 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactory.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactory.kt @@ -49,6 +49,7 @@ import java.time.Duration import java.util.* import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger @@ -103,6 +104,7 @@ class EthereumWsFactory( private val sendExecutor = Executors.newSingleThreadExecutor() private var keepConnection = true private var connection: Disposable? = null + private val reconnecting = AtomicBoolean(false) fun connect() { if (keepConnection) { @@ -114,9 +116,16 @@ class EthereumWsFactory( if (!keepConnection) { return } + val alreadyReconnecting = reconnecting.getAndSet(true) + if (alreadyReconnecting) { + return + } log.info("Reconnect to $uri in $retryInterval seconds...") Global.control.schedule( - { connectInternal() }, + { + reconnecting.set(false) + connectInternal() + }, retryInterval, TimeUnit.SECONDS) } @@ -125,6 +134,7 @@ class EthereumWsFactory( connection?.dispose() connection = HttpClient.create() .doOnDisconnected { + log.info("Disconnected from $uri") if (keepConnection) { tryReconnectLater() }