前文:
《Tomcat源码:启动类Bootstrap与Catalina的加载》
《Tomcat源码:StandardServer与StandardService》
《Tomcat源码:StandardEngine、StandardHost、StandardContext、StandardWrapper》
《Tomcat源码:连接器与Executor、Connector》
《Tomcat源码:ProtocolHandler与Endpoint》
前言
前文中我们介绍到NioEndpoint的start方法启动了Acceptor与Poller这两个异步线程来处理连接请求,本文我们就来接着介绍这两个组件。
目录
一、Acceptor
在前文的Endpoint的startAcceptorThread方法中,创建了Acceptor对象,并使用异步线程调用了其run方法,下面我们就来介绍下Acceptor对象。
1、构造方法
Acceptor的构造方法传入了一个endpoint对象,看过前文的我们知道这里是一个NioEndpoint对象。
public abstract class AbstractEndpoint<S,U> {
protected void startAcceptorThread() {
acceptor = new Acceptor<>(this);
String threadName = getName() + "-Acceptor";
acceptor.setThreadName(threadName);
Thread t = new Thread(acceptor, threadName);
t.setPriority(getAcceptorThreadPriority());
t.setDaemon(getDaemon());
t.start();
}
}
public class Acceptor<U> implements Runnable {
private final AbstractEndpoint<?,U> endpoint;
public Acceptor(AbstractEndpoint<?,U> endpoint) {
this.endpoint = endpoint;
}
}
2、run方法
由于run方法较长,这里只展示下核心步骤,首先是接收到请求后判断是否已达最大连接数,如果允许则分配SocketChannel,最后进行连接的配置以及处理器的分配。
@Override
public void run() {
int errorDelay = 0;
long pauseStart = 0;
try {
// Loop until we receive a shutdown command
while (!stopCalled) {
// ...
try {
//if we have reached max connections, wait
endpoint.countUpOrAwaitConnection();
U socket = null;
try {
// Accept the next incoming connection from the server
// socket
socket = endpoint.serverSocketAccept();
} catch (Exception ioe) {
// We didn't get a socket
// ...
}
// Configure the socket
if (!stopCalled && !endpoint.isPaused()) {
// setSocketOptions() will hand the socket off to
// an appropriate processor if successful
if (!endpoint.setSocketOptions(socket)) {
endpoint.closeSocket(socket);
}
} else {
endpoint.destroySocket(socket);
}
} catch (Throwable t) {
// ...
}
}
} finally {
stopLatch.countDown();
}
state = AcceptorState.ENDED;
}
countUpOrAwaitConnection
countUpOrAwaitConnection是endpoint中的方法,用于判断连接数是否已达最大,这里会获取我们在前文中介绍的endpoint时提及的connectionLimitLatch。
public abstract class AbstractEndpoint<S,U> {
private int maxConnections = 8*1024;
protected void countUpOrAwaitConnection() throws InterruptedException {
if (maxConnections==-1) {
return;
}
LimitLatch latch = connectionLimitLatch;
if (latch!=null) {
latch.countUpOrAwait();
}
}
}
LimitLatch
connectionLimitLatch的实现类为LimitLatch,通过其源码可以发现其定义了三个成员变量sync、count、limit分别用来管理并发锁、记录请求数与最大连接限制。
public abstract class AbstractEndpoint<S,U> {
protected LimitLatch initializeConnectionLatch() {
if (maxConnections==-1) {
return null;
}
if (connectionLimitLatch==null) {
connectionLimitLatch = new LimitLatch(getMaxConnections());
}
return connectionLimitLatch;
}
}
public class LimitLatch {
private final Sync sync;
private final AtomicLong count;
private volatile long limit;
public LimitLatch(long limit) {
this.limit = limit;
this.count = new AtomicLong(0);
this.sync = new Sync();
}
}
tryAcquireShared
回到上文中判断最大连接数的步骤latch.countUpOrAwait,我们进入到LimitLatch 中来看下。这里最终会进入tryAcquireShared方法,将count加1后判断是否已超出限制,然后再做进一步判断与处理。
public class LimitLatch {
public void countUpOrAwait() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
private class Sync extends AbstractQueuedSynchronizer {
@Override
protected int tryAcquireShared(int ignored) {
long newCount = count.incrementAndGet();
if (!released && newCount > limit) {
// Limit exceeded
count.decrementAndGet();
return -1;
} else {
return 1;
}
}
}
public abstract class AbstractQueuedSynchronizer extends AbstractOwnableSynchronizer
implements java.io.Serializable {
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)
doAcquireSharedInterruptibly(arg);
}
}
serverSocketAccept
这一步也是调用的endpoint中的方法,从下文的源码中可以看出这里是从endpoint执行init方法时创建的ServerSocketChannel中获取SocketChannel 连接。在nio中,可以认为一个 SocketChannel 对象代表一个服务端与客户端的连接。
public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel> {
@Override
protected SocketChannel serverSocketAccept() throws Exception {
SocketChannel result = serverSock.accept();
// ...
856



被折叠的 条评论
为什么被折叠?



