problem: double subscription to same upstream

This commit is contained in:
Igor Artamonov
2019-08-13 23:20:02 -04:00
parent 0b156d12e7
commit e2520f4dbf
2 changed files with 20 additions and 16 deletions

View File

@@ -149,12 +149,9 @@ open class ConfiguredUpstreams(
) )
log.info("Using ALL CHAINS (gRPC) upstream, at ${endpoint.host}:${endpoint.port}") log.info("Using ALL CHAINS (gRPC) upstream, at ${endpoint.host}:${endpoint.port}")
ds.start() ds.start()
.flatMapMany {
it.toFlux()
}
.subscribe { .subscribe {
log.info("Subscribed to $it through gRPC at ${endpoint.host}:${endpoint.port}") log.info("Subscribed to ${it.t1} through gRPC at ${endpoint.host}:${endpoint.port}")
addUpstream(it, ds.getOrCreate(it)) addUpstream(it.t1, it.t2)
} }
} }

View File

@@ -10,7 +10,11 @@ import io.grpc.netty.NettyChannelBuilder
import io.netty.handler.ssl.* import io.netty.handler.ssl.*
import org.apache.commons.lang3.StringUtils import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
import reactor.core.publisher.toFlux
import reactor.util.function.Tuple2
import reactor.util.function.Tuples
import java.io.File import java.io.File
import java.util.* import java.util.*
import java.util.concurrent.locks.ReentrantLock import java.util.concurrent.locks.ReentrantLock
@@ -29,7 +33,7 @@ class GrpcUpstreams(
private var known = HashMap<Chain, GrpcUpstream>() private var known = HashMap<Chain, GrpcUpstream>()
private val lock = ReentrantLock() private val lock = ReentrantLock()
fun start(): Mono<List<Chain>> { fun start(): Flux<Tuple2<Chain, GrpcUpstream>> {
val channel: ManagedChannelBuilder<*> = if (auth != null && StringUtils.isNotEmpty(auth.ca)) { val channel: ManagedChannelBuilder<*> = if (auth != null && StringUtils.isNotEmpty(auth.ca)) {
NettyChannelBuilder.forAddress(host, port) NettyChannelBuilder.forAddress(host, port)
.useTransportSecurity() .useTransportSecurity()
@@ -45,17 +49,18 @@ class GrpcUpstreams(
val loaded = client.describe(BlockchainOuterClass.DescribeRequest.newBuilder().build()) val loaded = client.describe(BlockchainOuterClass.DescribeRequest.newBuilder().build())
.map { value -> .map { value ->
val chains = ArrayList<Chain>() val chains = ArrayList<Chain>()
value.chainsList.forEach { chainDetails -> value.chainsList.filter {
Chain.byId(it.chain.number) != Chain.UNSPECIFIED
}.map { chainDetails ->
val chain = Chain.byId(chainDetails.chain.number) val chain = Chain.byId(chainDetails.chain.number)
if (chain != Chain.UNSPECIFIED) { val up = getOrCreate(chain)
getOrCreate(chain) up.init(chainDetails)
.init(chainDetails) chains.add(chain)
chains.add(chain) Tuples.of(chain, up)
}
} }
chains as List<Chain> }.flatMapMany {
} it.toFlux()
.doOnError { t -> }.doOnError { t ->
log.error("Failed to get description from $host:$port", t) log.error("Failed to get description from $host:$port", t)
} }
//TODO subscribe only after receiving details //TODO subscribe only after receiving details
@@ -93,7 +98,6 @@ class GrpcUpstreams(
return if (current == null) { return if (current == null) {
val created = GrpcUpstream(chain, client!!, objectMapper, upstreams.targetFor(chain)) val created = GrpcUpstream(chain, client!!, objectMapper, upstreams.targetFor(chain))
known[chain] = created known[chain] = created
upstreams.addUpstream(chain, created)
created.start() created.start()
created created
} else { } else {
@@ -102,4 +106,7 @@ class GrpcUpstreams(
} }
} }
fun get(chain: Chain): GrpcUpstream {
return known[chain]!!
}
} }