problem: can keep several subscription for the same head
This commit is contained in:
@@ -2,6 +2,7 @@ package io.emeraldpay.dshackle.upstream
|
|||||||
|
|
||||||
import io.emeraldpay.grpc.Chain
|
import io.emeraldpay.grpc.Chain
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.io.Closeable
|
||||||
import java.lang.IllegalStateException
|
import java.lang.IllegalStateException
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
|
|
||||||
@@ -12,13 +13,17 @@ class ChainUpstreams (
|
|||||||
|
|
||||||
private val log = LoggerFactory.getLogger(ChainUpstreams::class.java)
|
private val log = LoggerFactory.getLogger(ChainUpstreams::class.java)
|
||||||
private var seq = 0
|
private var seq = 0
|
||||||
private var head: EthereumHead
|
private var head: EthereumHead?
|
||||||
|
|
||||||
init {
|
init {
|
||||||
head = updateHead()
|
head = updateHead()
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun updateHead(): EthereumHead {
|
internal fun updateHead(): EthereumHead {
|
||||||
|
val current = head
|
||||||
|
if (current != null && Closeable::class.java.isAssignableFrom(current.javaClass)) {
|
||||||
|
(current as Closeable).close()
|
||||||
|
}
|
||||||
return if (upstreams.size == 1) {
|
return if (upstreams.size == 1) {
|
||||||
upstreams.first().getHead()
|
upstreams.first().getHead()
|
||||||
} else {
|
} else {
|
||||||
@@ -48,13 +53,13 @@ class ChainUpstreams (
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getHead(): EthereumHead {
|
override fun getHead(): EthereumHead {
|
||||||
return head
|
return head!!
|
||||||
}
|
}
|
||||||
|
|
||||||
fun printStatus() {
|
fun printStatus() {
|
||||||
var height: Long = -1
|
var height: Long = -1
|
||||||
try {
|
try {
|
||||||
height = head.getHead().block(Duration.ofSeconds(1))?.number ?: -1
|
height = head!!.getHead().block(Duration.ofSeconds(1))?.number ?: -1
|
||||||
} catch (e: IllegalStateException) {
|
} catch (e: IllegalStateException) {
|
||||||
//timout
|
//timout
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|||||||
@@ -3,18 +3,20 @@ package io.emeraldpay.dshackle.upstream
|
|||||||
import io.infinitape.etherjar.domain.TransactionId
|
import io.infinitape.etherjar.domain.TransactionId
|
||||||
import io.infinitape.etherjar.rpc.json.BlockJson
|
import io.infinitape.etherjar.rpc.json.BlockJson
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
import reactor.core.Disposable
|
||||||
import reactor.core.publisher.Flux
|
import reactor.core.publisher.Flux
|
||||||
import reactor.core.publisher.Mono
|
import reactor.core.publisher.Mono
|
||||||
import reactor.core.scheduler.Schedulers
|
import java.io.Closeable
|
||||||
import java.util.concurrent.atomic.AtomicReference
|
import java.util.concurrent.atomic.AtomicReference
|
||||||
|
|
||||||
class EthereumHeadMerge(
|
class EthereumHeadMerge(
|
||||||
private val upstreams: List<EthereumHead>
|
private val upstreams: List<EthereumHead>
|
||||||
): EthereumHead {
|
): EthereumHead, Closeable {
|
||||||
|
|
||||||
private val log = LoggerFactory.getLogger(EthereumHeadMerge::class.java)
|
private val log = LoggerFactory.getLogger(EthereumHeadMerge::class.java)
|
||||||
private val flux: Flux<BlockJson<TransactionId>>
|
private val flux: Flux<BlockJson<TransactionId>>
|
||||||
private val head = AtomicReference<BlockJson<TransactionId>>(null)
|
private val head = AtomicReference<BlockJson<TransactionId>>(null)
|
||||||
|
private val subscription: Disposable
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val fluxes = upstreams.map { it.getFlux() }
|
val fluxes = upstreams.map { it.getFlux() }
|
||||||
@@ -26,7 +28,8 @@ class EthereumHeadMerge(
|
|||||||
.publish()
|
.publish()
|
||||||
.autoConnect()
|
.autoConnect()
|
||||||
|
|
||||||
Flux.from(flux).subscribe {
|
|
||||||
|
subscription = Flux.from(flux).subscribe {
|
||||||
head.set(it)
|
head.set(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,11 +39,18 @@ class EthereumHeadMerge(
|
|||||||
if (curr != null) {
|
if (curr != null) {
|
||||||
return Mono.just(curr)
|
return Mono.just(curr)
|
||||||
}
|
}
|
||||||
return Mono.from(getFlux())
|
return getFlux().next()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getFlux(): Flux<BlockJson<TransactionId>> {
|
override fun getFlux(): Flux<BlockJson<TransactionId>> {
|
||||||
return Flux.from(this.flux)
|
return Flux.from(this.flux)
|
||||||
.onBackpressureLatest()
|
.onBackpressureLatest()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun close() {
|
||||||
|
if (!subscription.isDisposed) {
|
||||||
|
subscription.dispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user