Commit fa45a9f3 by Bertrand Renuart Committed by Spencer Gibb

Use a fixed size buffer for the copy operation (#1538)

Set the default buffer size to 8192 to match Tomcat and Jetty defaults Use a buffer per worker thread and use it for subsequent requests
parent 98cd592f
...@@ -48,7 +48,7 @@ public class SendResponseFilter extends ZuulFilter { ...@@ -48,7 +48,7 @@ public class SendResponseFilter extends ZuulFilter {
private static DynamicIntProperty INITIAL_STREAM_BUFFER_SIZE = DynamicPropertyFactory private static DynamicIntProperty INITIAL_STREAM_BUFFER_SIZE = DynamicPropertyFactory
.getInstance() .getInstance()
.getIntProperty(ZuulConstants.ZUUL_INITIAL_STREAM_BUFFER_SIZE, 1024); .getIntProperty(ZuulConstants.ZUUL_INITIAL_STREAM_BUFFER_SIZE, 8192);
private static DynamicBooleanProperty SET_CONTENT_LENGTH = DynamicPropertyFactory private static DynamicBooleanProperty SET_CONTENT_LENGTH = DynamicPropertyFactory
.getInstance() .getInstance()
...@@ -65,6 +65,13 @@ public class SendResponseFilter extends ZuulFilter { ...@@ -65,6 +65,13 @@ public class SendResponseFilter extends ZuulFilter {
} }
} }
private ThreadLocal<byte[]> buffers = new ThreadLocal<byte[]>() {
@Override
protected byte[] initialValue() {
return new byte[INITIAL_STREAM_BUFFER_SIZE.get()];
}
};
@Override @Override
public String filterType() { public String filterType() {
return "post"; return "post";
...@@ -177,21 +184,16 @@ public class SendResponseFilter extends ZuulFilter { ...@@ -177,21 +184,16 @@ public class SendResponseFilter extends ZuulFilter {
} }
private void writeResponse(InputStream zin, OutputStream out) throws Exception { private void writeResponse(InputStream zin, OutputStream out) throws Exception {
byte[] bytes = new byte[INITIAL_STREAM_BUFFER_SIZE.get()]; try {
int bytesRead = -1; byte[] bytes = buffers.get();
while ((bytesRead = zin.read(bytes)) != -1) { int bytesRead = -1;
try { while ((bytesRead = zin.read(bytes)) != -1) {
out.write(bytes, 0, bytesRead); out.write(bytes, 0, bytesRead);
out.flush();
}
catch (IOException ex) {
// ignore
}
// doubles buffer size if previous read filled it
if (bytesRead == bytes.length) {
bytes = new byte[bytes.length * 2];
} }
} }
catch(IOException ioe) {
log.warn("Error while sending response to client: "+ioe.getMessage());
}
} }
private void addResponseHeaders() { private void addResponseHeaders() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment