Search in sources :

Example 1 with Metrics

use of reactor.netty.internal.util.Metrics in project reactor-netty by reactor.

the class PooledConnectionProvider method acquire.

@Override
public final Mono<? extends Connection> acquire(TransportConfig config, ConnectionObserver connectionObserver, @Nullable Supplier<? extends SocketAddress> remote, @Nullable AddressResolverGroup<?> resolverGroup) {
    Objects.requireNonNull(config, "config");
    Objects.requireNonNull(connectionObserver, "connectionObserver");
    Objects.requireNonNull(remote, "remoteAddress");
    Objects.requireNonNull(resolverGroup, "resolverGroup");
    return Mono.create(sink -> {
        SocketAddress remoteAddress = Objects.requireNonNull(remote.get(), "Remote Address supplier returned null");
        PoolKey holder = new PoolKey(remoteAddress, config.channelHash());
        PoolFactory<T> poolFactory = poolFactory(remoteAddress);
        InstrumentedPool<T> pool = MapUtils.computeIfAbsent(channelPools, holder, poolKey -> {
            if (log.isDebugEnabled()) {
                log.debug("Creating a new [{}] client pool [{}] for [{}]", name, poolFactory, remoteAddress);
            }
            InstrumentedPool<T> newPool = createPool(config, poolFactory, remoteAddress, resolverGroup);
            if (poolFactory.metricsEnabled || config.metricsRecorder() != null) {
                // registrar is null when metrics are enabled on HttpClient level or
                // with the `metrics(boolean metricsEnabled)` method on ConnectionProvider
                String id = poolKey.hashCode() + "";
                if (poolFactory.registrar != null) {
                    poolFactory.registrar.get().registerMetrics(name, id, remoteAddress, new DelegatingConnectionPoolMetrics(newPool.metrics()));
                } else if (Metrics.isInstrumentationAvailable()) {
                    // work directly with the pool otherwise a weak reference is needed to ConnectionPoolMetrics
                    // we don't want to keep another map with weak references
                    registerDefaultMetrics(id, remoteAddress, newPool.metrics());
                }
            }
            return newPool;
        });
        EventLoop eventLoop = config.loopResources().onClient(config.isPreferNative()).next();
        pool.acquire(Duration.ofMillis(poolFactory.pendingAcquireTimeout)).contextWrite(ctx -> ctx.put(CONTEXT_CALLER_EVENTLOOP, eventLoop)).subscribe(createDisposableAcquire(config, connectionObserver, poolFactory.pendingAcquireTimeout, pool, sink));
    });
}
Also used : ReactorNetty(reactor.netty.ReactorNetty) SamplingAllocationStrategy(reactor.pool.introspection.SamplingAllocationStrategy) Disposable(reactor.core.Disposable) SocketAddress(java.net.SocketAddress) ReactorNetty.format(reactor.netty.ReactorNetty.format) InstrumentedPool(reactor.pool.InstrumentedPool) MonoSink(reactor.core.publisher.MonoSink) HashMap(java.util.HashMap) Nullable(reactor.util.annotation.Nullable) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) InetAddress(java.net.InetAddress) ConnectionObserver(reactor.netty.ConnectionObserver) BiPredicate(java.util.function.BiPredicate) CoreSubscriber(reactor.core.CoreSubscriber) TransportConfig(reactor.netty.transport.TransportConfig) AllocationStrategy(reactor.pool.AllocationStrategy) Loggers(reactor.util.Loggers) Metrics(reactor.netty.internal.util.Metrics) Logger(reactor.util.Logger) Duration(java.time.Duration) Map(java.util.Map) Schedulers(reactor.core.scheduler.Schedulers) MapUtils(reactor.netty.internal.util.MapUtils) PoolConfig(reactor.pool.PoolConfig) PENDING_ACQUIRE_MAX_COUNT_NOT_SPECIFIED(reactor.netty.resources.ConnectionProvider.ConnectionPoolSpec.PENDING_ACQUIRE_MAX_COUNT_NOT_SPECIFIED) Connection(reactor.netty.Connection) PooledRef(reactor.pool.PooledRef) Publisher(org.reactivestreams.Publisher) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PoolBuilder(reactor.pool.PoolBuilder) Mono(reactor.core.publisher.Mono) Pool(reactor.pool.Pool) GracefulShutdownInstrumentedPool(reactor.pool.decorators.GracefulShutdownInstrumentedPool) EventLoop(io.netty.channel.EventLoop) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Channel(io.netty.channel.Channel) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) List(java.util.List) Clock(java.time.Clock) PooledRefMetadata(reactor.pool.PooledRefMetadata) InstrumentedPoolDecorators(reactor.pool.decorators.InstrumentedPoolDecorators) EventLoop(io.netty.channel.EventLoop) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with Metrics

use of reactor.netty.internal.util.Metrics in project reactor-netty by reactor.

the class HttpClientNoMicrometerTest method clientCreatedWithMetricsDoesntLoadGaugeHttp2.

@Test
void clientCreatedWithMetricsDoesntLoadGaugeHttp2() throws Exception {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    Http2SslContextSpec serverCtx = Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey());
    Http2SslContextSpec clientCtx = Http2SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
    ConnectionProvider provider = ConnectionProvider.builder("foo2").metrics(true, NoOpMeterRegistrar::new).build();
    try {
        doTestClientCreatedWithMetricsDoesntLoadGauge(HttpServer.create().protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(serverCtx)), HttpClient.create(provider).protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(clientCtx)));
    } finally {
        provider.disposeLater().block(Duration.ofSeconds(5));
    }
}
Also used : Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) SocketAddress(java.net.SocketAddress) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) ConnectionPoolMetrics(reactor.netty.resources.ConnectionPoolMetrics) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Flux(reactor.core.publisher.Flux) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) HttpServer(reactor.netty.http.server.HttpServer) ChannelMetricsRecorder(reactor.netty.channel.ChannelMetricsRecorder) Metrics(reactor.netty.internal.util.Metrics) Duration(java.time.Duration) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) ConnectionProvider(reactor.netty.resources.ConnectionProvider) Test(org.junit.jupiter.api.Test)

Aggregations

SocketAddress (java.net.SocketAddress)2 Duration (java.time.Duration)2 List (java.util.List)2 Metrics (reactor.netty.internal.util.Metrics)2 Channel (io.netty.channel.Channel)1 EventLoop (io.netty.channel.EventLoop)1 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 AddressResolverGroup (io.netty.resolver.AddressResolverGroup)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Clock (java.time.Clock)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 BiPredicate (java.util.function.BiPredicate)1