This commit is contained in:
Redkale
2018-12-20 11:51:12 +08:00
parent 2921478a0a
commit c82c0bc680
2 changed files with 40 additions and 5 deletions

View File

@@ -61,7 +61,7 @@ public class PrepareRunner implements Runnable {
@Override @Override
public void completed(Integer count, ByteBuffer buffer) { public void completed(Integer count, ByteBuffer buffer) {
if (count < 1) { if (count < 1) {
channel.offerBuffer(buffer); response.request.offerReadBuffer(buffer);
channel.dispose();// response.init(channel); 在调用之前异常 channel.dispose();// response.init(channel); 在调用之前异常
response.removeChannel(); response.removeChannel();
response.finish(true); response.finish(true);
@@ -85,7 +85,7 @@ public class PrepareRunner implements Runnable {
@Override @Override
public void failed(Throwable exc, ByteBuffer buffer) { public void failed(Throwable exc, ByteBuffer buffer) {
channel.offerBuffer(buffer); response.request.offerReadBuffer(buffer);
channel.dispose();// response.init(channel); 在调用之前异常 channel.dispose();// response.init(channel); 在调用之前异常
response.removeChannel(); response.removeChannel();
response.finish(true); response.finish(true);
@@ -116,7 +116,7 @@ public class PrepareRunner implements Runnable {
if (buffer.hasRemaining()) { if (buffer.hasRemaining()) {
request.setMoredata(buffer); request.setMoredata(buffer);
} else { } else {
channel.offerBuffer(buffer); response.request.offerReadBuffer(buffer);
} }
preparer.prepare(request, response); preparer.prepare(request, response);
} else { } else {
@@ -137,7 +137,7 @@ public class PrepareRunner implements Runnable {
if (attachment.hasRemaining()) { if (attachment.hasRemaining()) {
request.setMoredata(attachment); request.setMoredata(attachment);
} else { } else {
channel.offerBuffer(attachment); response.request.offerReadBuffer(attachment);
} }
try { try {
preparer.prepare(request, response); preparer.prepare(request, response);
@@ -151,7 +151,7 @@ public class PrepareRunner implements Runnable {
@Override @Override
public void failed(Throwable exc, ByteBuffer attachment) { public void failed(Throwable exc, ByteBuffer attachment) {
preparer.illRequestCounter.incrementAndGet(); preparer.illRequestCounter.incrementAndGet();
channel.offerBuffer(attachment); response.request.offerReadBuffer(attachment);
response.finish(true); response.finish(true);
if (exc != null) request.context.logger.log(Level.FINER, "Servlet read channel erroneous, force to close channel ", exc); if (exc != null) request.context.logger.log(Level.FINER, "Servlet read channel erroneous, force to close channel ", exc);
} }
@@ -175,4 +175,19 @@ public class PrepareRunner implements Runnable {
return response.removeChannel(); return response.removeChannel();
} }
protected ByteBuffer pollReadBuffer(Request request) {
return request.pollReadBuffer();
}
protected ByteBuffer pollReadBuffer(Response response) {
return response.request.pollReadBuffer();
}
protected void offerReadBuffer(Request request, ByteBuffer buffer) {
request.offerReadBuffer(buffer);
}
protected void offerReadBuffer(Response response, ByteBuffer buffer) {
response.request.offerReadBuffer(buffer);
}
} }

View File

@@ -37,6 +37,8 @@ public abstract class Request<C extends Context> {
protected AsyncConnection channel; protected AsyncConnection channel;
protected ByteBuffer readBuffer;
/** /**
* properties 与 attributes 的区别在于调用recycle时 attributes会被清空而properties会保留; * properties 与 attributes 的区别在于调用recycle时 attributes会被清空而properties会保留;
* properties 通常存放需要永久绑定在request里的一些对象 * properties 通常存放需要永久绑定在request里的一些对象
@@ -47,6 +49,7 @@ public abstract class Request<C extends Context> {
protected Request(C context) { protected Request(C context) {
this.context = context; this.context = context;
this.readBuffer = context.pollBuffer();
this.bsonConvert = context.getBsonConvert(); this.bsonConvert = context.getBsonConvert();
this.jsonConvert = context.getJsonConvert(); this.jsonConvert = context.getJsonConvert();
} }
@@ -61,6 +64,23 @@ public abstract class Request<C extends Context> {
return rs; return rs;
} }
protected ByteBuffer pollReadBuffer() {
ByteBuffer buffer = this.readBuffer;
this.readBuffer = null;
if (buffer == null) buffer = context.pollBuffer();
return buffer;
}
protected void offerReadBuffer(ByteBuffer buffer) {
if (buffer == null) return;
if (this.readBuffer == null) {
buffer.clear();
this.readBuffer = buffer;
} else {
context.offerBuffer(buffer);
}
}
/** /**
* 返回值Integer.MIN_VALUE: 帧数据; -1数据不合法 0解析完毕 &gt;0: 需再读取的字节数。 * 返回值Integer.MIN_VALUE: 帧数据; -1数据不合法 0解析完毕 &gt;0: 需再读取的字节数。
* *