All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.cosmos.implementation.directconnectivity.rntbd.RntbdClientChannelHandler Maven / Gradle / Ivy

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.cosmos.implementation.directconnectivity.rntbd;

import com.azure.cosmos.implementation.directconnectivity.rntbd.RntbdEndpoint.Config;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoop;
import io.netty.channel.pool.ChannelHealthChecker;
import io.netty.channel.pool.ChannelPool;
import io.netty.channel.pool.ChannelPoolHandler;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkNotNull;

public class RntbdClientChannelHandler extends ChannelInitializer implements ChannelPoolHandler {

    private static final AttributeKey REQUEST_MANAGER = AttributeKey.newInstance("requestManager");
    private static final Logger logger = LoggerFactory.getLogger(RntbdClientChannelHandler.class);
    private final ChannelHealthChecker healthChecker;
    private final Config config;

    RntbdClientChannelHandler(final Config config, final ChannelHealthChecker healthChecker) {
        checkNotNull(healthChecker, "expected non-null healthChecker");
        checkNotNull(config, "expected non-null config");
        this.healthChecker = healthChecker;
        this.config = config;
    }

    /**
     * Called by {@link ChannelPool#acquire} after a {@link Channel} is acquired
     * 

* This method is called within the {@link EventLoop} of the {@link Channel}. * * @param channel a channel that was just acquired */ @Override public void channelAcquired(final Channel channel) { logger.debug("{} CHANNEL ACQUIRED", channel); } /** * Called by {@link ChannelPool#release} after a {@link Channel} is created *

* This method is called within the {@link EventLoop} of the {@link Channel}. * * @param channel a channel that was just created */ @Override public void channelCreated(final Channel channel) { logger.debug("{} CHANNEL CREATED", channel); this.initChannel(channel); } /** * Called by {@link ChannelPool#release} after a {@link Channel} is released *

* This method is called within the {@link EventLoop} of the {@link Channel}. * * @param channel a channel that was just released */ @Override public void channelReleased(final Channel channel) { logger.debug("{} CHANNEL RELEASED", channel); } /** * Called by @{ChannelPipeline} initializer after the current channel is registered to an event loop. *

* This method constructs this pipeline: *

{@code
     * ChannelPipeline {
     *     (SslHandler#0 = io.netty.handler.ssl.SslHandler),
     *     (IdleTimeoutHandler#0 = io.netty.handler.timeout.IdleTimeoutHandler),
     *     (LoggingHandler#0 = io.netty.handler.logging.LoggingHandler),  // iff RntbdClientChannelHandler.config.wireLogLevel != null
     *     (RntbdContextNegotiator#0 = com.azure.cosmos.internal.directconnectivity.rntbd.RntbdContextNegotiator),
     *     (RntbdResponseDecoder#0 = com.azure.cosmos.internal.directconnectivity.rntbd.RntbdResponseDecoder),
     *     (RntbdRequestEncoder#0 = com.azure.cosmos.internal.directconnectivity.rntbd.RntbdRequestEncoder),
     *     (RntbdRequestManager#0 = com.azure.cosmos.internal.directconnectivity.rntbd.RntbdRequestManager),
     * }
     * }
* * @param channel a channel that was just registered with an event loop */ @Override protected void initChannel(final Channel channel) { checkNotNull(channel); final RntbdRequestManager requestManager = new RntbdRequestManager(this.healthChecker, this.config.maxRequestsPerChannel()); final long readerIdleTime = this.config.receiveHangDetectionTimeInNanos(); final long writerIdleTime = this.config.sendHangDetectionTimeInNanos(); final long allIdleTime = this.config.idleConnectionTimeoutInNanos(); final ChannelPipeline pipeline = channel.pipeline(); pipeline.addFirst( new RntbdContextNegotiator(requestManager, this.config.userAgent()), new RntbdResponseDecoder(), new RntbdRequestEncoder(), requestManager ); if (this.config.wireLogLevel() != null) { pipeline.addFirst(new LoggingHandler(this.config.wireLogLevel())); } pipeline.addFirst( this.config.sslContext().newHandler(channel.alloc()), new IdleStateHandler(readerIdleTime, writerIdleTime, allIdleTime, TimeUnit.NANOSECONDS) ); channel.attr(REQUEST_MANAGER).set(requestManager); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy