This commit is contained in:
@@ -26,24 +26,23 @@ public final class PrepareRunner implements Runnable {
|
||||
|
||||
private final Context context;
|
||||
|
||||
private final boolean keepAlive;
|
||||
|
||||
private ByteBuffer data;
|
||||
|
||||
private Response response;
|
||||
|
||||
public PrepareRunner(Context context, AsyncConnection channel, ByteBuffer data, Response response) {
|
||||
public PrepareRunner(Context context, AsyncConnection channel, ByteBuffer data, boolean keepAlive) {
|
||||
this.context = context;
|
||||
this.channel = channel;
|
||||
this.data = data;
|
||||
this.response = response;
|
||||
this.keepAlive = keepAlive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
final boolean keepalive = response != null;
|
||||
final PrepareServlet prepare = context.prepare;
|
||||
final ObjectPool<? extends Response> responsePool = context.responsePool;
|
||||
final Response response = responsePool.get();
|
||||
if (data != null) { //BIO模式的UDP连接创建AsyncConnection时已经获取到ByteBuffer数据了
|
||||
if (response == null) response = responsePool.get();
|
||||
try {
|
||||
response.init(channel);
|
||||
prepare.prepare(data, response.request, response);
|
||||
@@ -53,10 +52,9 @@ public final class PrepareRunner implements Runnable {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (response == null) response = responsePool.get();
|
||||
final ByteBuffer buffer = response.request.pollReadBuffer();
|
||||
try {
|
||||
channel.read(buffer, keepalive ? context.getAliveTimeoutSeconds() : 0, TimeUnit.SECONDS, null,
|
||||
channel.read(buffer, keepAlive ? context.getAliveTimeoutSeconds() : 0, TimeUnit.SECONDS, null,
|
||||
new CompletionHandler<Integer, Void>() {
|
||||
@Override
|
||||
public void completed(Integer count, Void attachment1) {
|
||||
|
||||
@@ -150,7 +150,7 @@ public abstract class ProtocolServer {
|
||||
SocketAddress address = serchannel.receive(buffer);
|
||||
buffer.flip();
|
||||
AsyncConnection conn = AsyncConnection.create(serchannel, address, false, readTimeoutSeconds, writeTimeoutSeconds);
|
||||
context.runAsync(new PrepareRunner(context, conn, buffer, null));
|
||||
context.runAsync(new PrepareRunner(context, conn, buffer, false));
|
||||
} catch (Exception e) {
|
||||
context.offerBuffer(buffer);
|
||||
}
|
||||
@@ -244,7 +244,7 @@ public abstract class ProtocolServer {
|
||||
AsyncConnection conn = AsyncConnection.create(channel, null, context);
|
||||
conn.livingCounter = livingCounter;
|
||||
conn.closedCounter = closedCounter;
|
||||
context.runAsync(new PrepareRunner(context, conn, null, null));
|
||||
context.runAsync(new PrepareRunner(context, conn, null, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -170,21 +170,13 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
protected boolean recycle() {
|
||||
if (!inited) return false;
|
||||
boolean keepAlive = request.keepAlive;
|
||||
if (recycleListener != null) {
|
||||
try {
|
||||
recycleListener.accept(request, this);
|
||||
} catch (Exception e) {
|
||||
context.logger.log(Level.WARNING, "Response.recycleListener error, request = " + request, e);
|
||||
}
|
||||
recycleListener = null;
|
||||
}
|
||||
this.output = null;
|
||||
this.filter = null;
|
||||
this.servlet = null;
|
||||
request.recycle();
|
||||
if (channel != null) {
|
||||
if (keepAlive) {
|
||||
this.context.runAsync(new PrepareRunner(context, channel, null, null));
|
||||
this.context.runAsync(new PrepareRunner(context, channel, null, keepAlive));
|
||||
} else {
|
||||
try {
|
||||
if (channel.isOpen()) channel.close();
|
||||
@@ -255,6 +247,14 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
if (!this.inited) return; //避免重复关闭
|
||||
//System.println("耗时: " + (System.currentTimeMillis() - request.createtime));
|
||||
if (kill) refuseAlive();
|
||||
if (this.recycleListener != null) {
|
||||
try {
|
||||
this.recycleListener.accept(request, this);
|
||||
} catch (Exception e) {
|
||||
context.logger.log(Level.WARNING, "Response.recycleListener error, request = " + request, e);
|
||||
}
|
||||
this.recycleListener = null;
|
||||
}
|
||||
this.context.responsePool.accept(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -179,7 +179,6 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
boolean rs = super.recycle();
|
||||
this.status = 200;
|
||||
this.contentLength = -1;
|
||||
this.contentType = null;
|
||||
@@ -187,7 +186,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
this.headsended = false;
|
||||
this.header.clear();
|
||||
this.bufferHandler = null;
|
||||
return rs;
|
||||
return super.recycle();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user