use of reactor.netty.ConnectionObserver in project reactor-netty by reactor.
the class NewConnectionProvider method acquire.
@Override
public Mono<? extends Connection> acquire(TransportConfig config, ConnectionObserver observer, @Nullable Supplier<? extends SocketAddress> remoteAddress, @Nullable AddressResolverGroup<?> resolverGroup) {
return Mono.create(sink -> {
SocketAddress remote = null;
if (remoteAddress != null) {
remote = Objects.requireNonNull(remoteAddress.get(), "Remote Address supplier returned null");
}
ConnectionObserver connectionObserver = new NewConnectionObserver(sink, observer);
DisposableConnect disposableConnect = new DisposableConnect(sink, config.bindAddress());
if (remote != null && resolverGroup != null) {
ChannelInitializer<Channel> channelInitializer = config.channelInitializer(connectionObserver, remote, false);
TransportConnector.connect(config, remote, resolverGroup, channelInitializer).subscribe(disposableConnect);
} else {
Objects.requireNonNull(config.bindAddress(), "bindAddress");
SocketAddress local = Objects.requireNonNull(config.bindAddress().get(), "Bind Address supplier returned null");
if (local instanceof InetSocketAddress) {
InetSocketAddress localInet = (InetSocketAddress) local;
if (localInet.isUnresolved()) {
local = AddressUtils.createResolved(localInet.getHostName(), localInet.getPort());
}
}
ChannelInitializer<Channel> channelInitializer = config.channelInitializer(connectionObserver, null, true);
TransportConnector.bind(config, channelInitializer, local, local instanceof DomainSocketAddress).subscribe(disposableConnect);
}
});
}
use of reactor.netty.ConnectionObserver 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));
});
}
use of reactor.netty.ConnectionObserver in project reactor-netty by reactor.
the class ContextAwareChannelMetricsHandler method recordException.
@Override
protected void recordException(ChannelHandlerContext ctx, SocketAddress address) {
Connection connection = Connection.from(ctx.channel());
ChannelOperations<?, ?> ops = connection.as(ChannelOperations.class);
if (ops != null) {
recorder().incrementErrorsCount(ops.currentContext(), address);
} else if (connection instanceof ConnectionObserver) {
recorder().incrementErrorsCount(((ConnectionObserver) connection).currentContext(), address);
} else {
super.recordException(ctx, address);
}
}
use of reactor.netty.ConnectionObserver in project reactor-netty by reactor.
the class ClientTransport method connect.
/**
* Connect the {@link ClientTransport} and return a {@link Mono} of {@link Connection}. If
* {@link Mono} is cancelled, the underlying connection will be aborted. Once the
* {@link Connection} has been emitted and is not necessary anymore, disposing must be
* done by the user via {@link Connection#dispose()}.
*
* @return a {@link Mono} of {@link Connection}
*/
protected Mono<? extends Connection> connect() {
CONF config = configuration();
ConnectionObserver observer = config.defaultConnectionObserver().then(config.observer);
AddressResolverGroup<?> resolver = config.resolverInternal();
Mono<? extends Connection> mono = config.connectionProvider().acquire(config, observer, config.remoteAddress, resolver);
if (config.doOnConnect != null) {
mono = mono.doOnSubscribe(s -> config.doOnConnect.accept(config));
}
return mono;
}
use of reactor.netty.ConnectionObserver in project reactor-netty by reactor.
the class ServerTransport method bind.
/**
* Binds the {@link ServerTransport} and returns a {@link Mono} of {@link DisposableServer}. If
* {@link Mono} is cancelled, the underlying binding will be aborted. Once the {@link
* DisposableServer} has been emitted and is not necessary anymore, disposing the main server
* loop must be done by the user via {@link DisposableServer#dispose()}.
*
* @return a {@link Mono} of {@link DisposableServer}
*/
public Mono<? extends DisposableServer> bind() {
CONF config = configuration();
Objects.requireNonNull(config.bindAddress(), "bindAddress");
Mono<? extends DisposableServer> mono = Mono.create(sink -> {
SocketAddress local = Objects.requireNonNull(config.bindAddress().get(), "Bind Address supplier returned null");
if (local instanceof InetSocketAddress) {
InetSocketAddress localInet = (InetSocketAddress) local;
if (localInet.isUnresolved()) {
local = AddressUtils.createResolved(localInet.getHostName(), localInet.getPort());
}
}
boolean isDomainSocket = false;
DisposableBind disposableServer;
if (local instanceof DomainSocketAddress) {
isDomainSocket = true;
disposableServer = new UdsDisposableBind(sink, config, local);
} else {
disposableServer = new InetDisposableBind(sink, config, local);
}
ConnectionObserver childObs = new ChildObserver(config.defaultChildObserver().then(config.childObserver()));
Acceptor acceptor = new Acceptor(config.childEventLoopGroup(), config.channelInitializer(childObs, null, true), config.childOptions, config.childAttrs, isDomainSocket);
TransportConnector.bind(config, new AcceptorInitializer(acceptor), local, isDomainSocket).subscribe(disposableServer);
});
if (config.doOnBind() != null) {
mono = mono.doOnSubscribe(s -> config.doOnBind().accept(config));
}
return mono;
}
Aggregations