netty注册流程

												//1.        	//	2    //3
   ChannelFuture regFuture = config().group().register(channel);
        if (regFuture.cause() != null) {
            if (channel.isRegistered()) {
                channel.close();
            } else {
                channel.unsafe().closeForcibly();
            }
        }
          // If we are here and the promise is not failed, it's one of the following cases:
        // 1) If we attempted registration from the event loop, the registration has been completed at this point.
        //    i.e. It's safe to attempt bind() or connect() now because the channel has been registered.
        // 2) If we attempted registration from the other thread, the registration request has been successfully
        //    added to the event loop's task queue for later execution.
        //    i.e. It's safe to attempt bind() or connect() now:
        //         because bind() or connect() will be executed *after* the scheduled registration task is executed
        //         because register(), bind(), and connect() are all bound to the same thread.

        return regFuture;

 1. 
 
    @Override
    public final ServerBootstrapConfig config() {
        return config;
    }
private final ServerBootstrapConfig config = new ServerBootstrapConfig(this);

2. 
public abstract class AbstractBootstrapConfig<B extends AbstractBootstrap<B, C>, C extends Channel>
 public final EventLoopGroup group() {
        return bootstrap.group();
    }
       protected final B bootstrap;
public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel> implements Cloneable {

    volatile EventLoopGroup group; (NioEventLoopGroup)

3.

@Override
    public ChannelFuture register(Channel channel) {
    // 4.
        return next().register(channel);
    }
    4.
    @Override
    public EventLoop next() {
        return (EventLoop) super.next();
    }
      private final EventExecutorChooserFactory.EventExecutorChooser chooser;
 @Override
    public EventExecutor next() {
        return chooser.next();
    }
chooser = chooserFactory.newChooser(children); 
/**
 		 														//随机请求算法
 * Default implementation which uses simple **round-robin** to choose next {@link EventExecutor}.
 */
public final class DefaultEventExecutorChooserFactory implements EventExecutorChooserFactory {

    @SuppressWarnings("unchecked")
    @Override
    public EventExecutorChooser newChooser(EventExecutor[] executors) {
        if (isPowerOfTwo(executors.length)) {
            return new PowerOfTwoEventExecutorChooser(executors);
        } else {
            return new GenericEventExecutorChooser(executors);
        }
    }

4中的super.next()返回值其实是

public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop


    @Override
    protected void doRegister() throws Exception {
        boolean selected = false;
        for (;;) {
            try {
                selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
                return;
            } catch (CancelledKeyException e) {
                if (!selected) {
                    // Force the Selector to select now as the "canceled" SelectionKey may still be
                    // cached and not removed because no Select.select(..) operation was called yet.
                    eventLoop().selectNow();
                    selected = true;
                } else {
                    // We forced a select operation on the selector before but the SelectionKey is still cached
                    // for whatever reason. JDK bug ?
                    throw e;
                }
            }
        }
    }

 protected SelectableChannel javaChannel() {
        return ch;
    }



            if (eventLoop.inEventLoop()) {
                register0(promise);
            } else {
                try {
                    eventLoop.execute(new Runnable() {
                        @Override
                        public void run() {
                            register0(promise);
                        }
                    });
                } catch (Throwable t) {
                    logger.warn(
                            "Force-closing a channel whose registration task was not accepted by an event loop: {}",
                            AbstractChannel.this, t);
                    closeForcibly();
                    closeFuture.setClosed();
                    safeSetFailure(promise, t);
                }
            }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值