Using Callback.Adapter.INSTANCE to avoid unneeded allocations.

This commit is contained in:
Simone Bordet 2014-05-19 18:49:07 +02:00
parent 1386f20cf5
commit 7918a58c71
15 changed files with 32 additions and 27 deletions

View File

@ -85,8 +85,7 @@ import org.eclipse.jetty.util.Callback;
*/ */
public class DeferredContentProvider implements AsyncContentProvider, Closeable public class DeferredContentProvider implements AsyncContentProvider, Closeable
{ {
private static final Callback EMPTY_CALLBACK = new Callback.Adapter(); private static final AsyncChunk CLOSE = new AsyncChunk(BufferUtil.EMPTY_BUFFER, Callback.Adapter.INSTANCE);
private static final AsyncChunk CLOSE = new AsyncChunk(BufferUtil.EMPTY_BUFFER, EMPTY_CALLBACK);
private final Object lock = this; private final Object lock = this;
private final Queue<AsyncChunk> chunks = new ArrayQueue<>(4, 64, lock); private final Queue<AsyncChunk> chunks = new ArrayQueue<>(4, 64, lock);
@ -130,7 +129,7 @@ public class DeferredContentProvider implements AsyncContentProvider, Closeable
*/ */
public boolean offer(ByteBuffer buffer) public boolean offer(ByteBuffer buffer)
{ {
return offer(buffer, EMPTY_CALLBACK); return offer(buffer, Callback.Adapter.INSTANCE);
} }
public boolean offer(ByteBuffer buffer, Callback callback) public boolean offer(ByteBuffer buffer, Callback callback)

View File

@ -99,7 +99,7 @@ public class HttpSenderOverFCGI extends HttpSender
int id = getHttpChannel().getRequest(); int id = getHttpChannel().getRequest();
boolean hasContent = content.hasContent(); boolean hasContent = content.hasContent();
Generator.Result headersResult = generator.generateRequestHeaders(id, fcgiHeaders, Generator.Result headersResult = generator.generateRequestHeaders(id, fcgiHeaders,
hasContent ? callback : new Callback.Adapter()); hasContent ? callback : Callback.Adapter.INSTANCE);
if (hasContent) if (hasContent)
{ {
getHttpChannel().flush(headersResult); getHttpChannel().flush(headersResult);

View File

@ -58,7 +58,7 @@ public class HttpTransportOverFCGI implements HttpTransport
if (lastContent) if (lastContent)
{ {
Generator.Result headersResult = generator.generateResponseHeaders(request, info.getStatus(), info.getReason(), Generator.Result headersResult = generator.generateResponseHeaders(request, info.getStatus(), info.getReason(),
info.getHttpFields(), new Callback.Adapter()); info.getHttpFields(), Callback.Adapter.INSTANCE);
Generator.Result contentResult = generator.generateResponseContent(request, BufferUtil.EMPTY_BUFFER, lastContent, aborted, callback); Generator.Result contentResult = generator.generateResponseContent(request, BufferUtil.EMPTY_BUFFER, lastContent, aborted, callback);
flusher.flush(headersResult, contentResult); flusher.flush(headersResult, contentResult);
} }
@ -72,7 +72,7 @@ public class HttpTransportOverFCGI implements HttpTransport
else else
{ {
Generator.Result headersResult = generator.generateResponseHeaders(request, info.getStatus(), info.getReason(), Generator.Result headersResult = generator.generateResponseHeaders(request, info.getStatus(), info.getReason(),
info.getHttpFields(), new Callback.Adapter()); info.getHttpFields(), Callback.Adapter.INSTANCE);
Generator.Result contentResult = generator.generateResponseContent(request, content, lastContent, aborted, callback); Generator.Result contentResult = generator.generateResponseContent(request, content, lastContent, aborted, callback);
flusher.flush(headersResult, contentResult); flusher.flush(headersResult, contentResult);
} }

View File

@ -380,7 +380,7 @@ public class SPDYClient
private void closeConnections() private void closeConnections()
{ {
for (Session session : sessions) for (Session session : sessions)
session.goAway(new GoAwayInfo(), new Callback.Adapter()); session.goAway(new GoAwayInfo(), Callback.Adapter.INSTANCE);
sessions.clear(); sessions.clear();
} }

View File

@ -167,7 +167,7 @@ public class SPDYConnection extends AbstractConnection implements Controller, Id
protected void goAway(ISession session) protected void goAway(ISession session)
{ {
if (session != null) if (session != null)
session.goAway(new GoAwayInfo(), new Callback.Adapter()); session.goAway(new GoAwayInfo(), Callback.Adapter.INSTANCE);
} }
private void shutdown(ISession session) private void shutdown(ISession session)

View File

@ -84,7 +84,7 @@ public class SPDYv3FlowControlStrategy implements FlowControlStrategy
if (dataInfo.consumed() == length && !stream.isClosed() && length > 0) if (dataInfo.consumed() == length && !stream.isClosed() && length > 0)
{ {
WindowUpdateFrame windowUpdateFrame = new WindowUpdateFrame(session.getVersion(), stream.getId(), length); WindowUpdateFrame windowUpdateFrame = new WindowUpdateFrame(session.getVersion(), stream.getId(), length);
session.control(stream, windowUpdateFrame, 0, TimeUnit.MILLISECONDS, new Callback.Adapter()); session.control(stream, windowUpdateFrame, 0, TimeUnit.MILLISECONDS, Callback.Adapter.INSTANCE);
} }
} }
} }

View File

@ -431,7 +431,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
{ {
RstInfo rstInfo = new RstInfo(streamId, StreamStatus.INVALID_STREAM); RstInfo rstInfo = new RstInfo(streamId, StreamStatus.INVALID_STREAM);
LOG.debug("Unknown stream {}", rstInfo); LOG.debug("Unknown stream {}", rstInfo);
rst(rstInfo, new Callback.Adapter()); rst(rstInfo, Callback.Adapter.INSTANCE);
} }
else else
{ {
@ -471,7 +471,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
public void onStreamException(StreamException x) public void onStreamException(StreamException x)
{ {
notifyOnFailure(listener, x); // TODO: notify StreamFrameListener if exists? notifyOnFailure(listener, x); // TODO: notify StreamFrameListener if exists?
rst(new RstInfo(x.getStreamId(), x.getStreamStatus()), new Callback.Adapter()); rst(new RstInfo(x.getStreamId(), x.getStreamStatus()), Callback.Adapter.INSTANCE);
} }
@Override @Override
@ -479,7 +479,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
{ {
Throwable cause = x.getCause(); Throwable cause = x.getCause();
notifyOnFailure(listener, cause == null ? x : cause); notifyOnFailure(listener, cause == null ? x : cause);
goAway(x.getSessionStatus(), 0, TimeUnit.SECONDS, new Callback.Adapter()); goAway(x.getSessionStatus(), 0, TimeUnit.SECONDS, Callback.Adapter.INSTANCE);
} }
private void onSyn(final SynStreamFrame frame) private void onSyn(final SynStreamFrame frame)
@ -570,7 +570,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
} }
RstInfo rstInfo = new RstInfo(streamId, StreamStatus.PROTOCOL_ERROR); RstInfo rstInfo = new RstInfo(streamId, StreamStatus.PROTOCOL_ERROR);
LOG.debug("Duplicate stream, {}", rstInfo); LOG.debug("Duplicate stream, {}", rstInfo);
rst(rstInfo, new Callback.Adapter()); // We don't care (too much) if the reset fails. rst(rstInfo, Callback.Adapter.INSTANCE); // We don't care (too much) if the reset fails.
return null; return null;
} }
else else
@ -653,7 +653,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
{ {
RstInfo rstInfo = new RstInfo(streamId, StreamStatus.INVALID_STREAM); RstInfo rstInfo = new RstInfo(streamId, StreamStatus.INVALID_STREAM);
LOG.debug("Unknown stream {}", rstInfo); LOG.debug("Unknown stream {}", rstInfo);
rst(rstInfo, new Callback.Adapter()); rst(rstInfo, Callback.Adapter.INSTANCE);
} }
else else
{ {
@ -712,7 +712,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
} }
else else
{ {
control(null, frame, 0, TimeUnit.MILLISECONDS, new Callback.Adapter()); control(null, frame, 0, TimeUnit.MILLISECONDS, Callback.Adapter.INSTANCE);
} }
} }
@ -736,7 +736,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
{ {
RstInfo rstInfo = new RstInfo(streamId, StreamStatus.INVALID_STREAM); RstInfo rstInfo = new RstInfo(streamId, StreamStatus.INVALID_STREAM);
LOG.debug("Unknown stream, {}", rstInfo); LOG.debug("Unknown stream, {}", rstInfo);
rst(rstInfo, new Callback.Adapter()); rst(rstInfo, Callback.Adapter.INSTANCE);
} }
else else
{ {
@ -1238,7 +1238,7 @@ public class StandardSession implements ISession, Parser.Listener, Dumpable
{ {
private CloseFrameBytes() private CloseFrameBytes()
{ {
super(null, new Callback.Adapter()); super(null, Callback.Adapter.INSTANCE);
} }
@Override @Override

View File

@ -272,7 +272,7 @@ public class StandardStream extends IdleTimeout implements IStream
if (!canReceive()) if (!canReceive())
{ {
LOG.debug("Protocol error receiving {}, resetting", dataInfo); LOG.debug("Protocol error receiving {}, resetting", dataInfo);
session.rst(new RstInfo(getId(), StreamStatus.PROTOCOL_ERROR), new Adapter()); session.rst(new RstInfo(getId(), StreamStatus.PROTOCOL_ERROR), Callback.Adapter.INSTANCE);
return; return;
} }
@ -547,7 +547,7 @@ public class StandardStream extends IdleTimeout implements IStream
private StreamCallback() private StreamCallback()
{ {
this(new Adapter()); this(Callback.Adapter.INSTANCE);
} }
private StreamCallback(Callback callback) private StreamCallback(Callback callback)

View File

@ -63,7 +63,7 @@ public class HttpConnectionOverSPDY extends HttpConnection
// First close then abort, to be sure that the connection cannot be reused // First close then abort, to be sure that the connection cannot be reused
// from an onFailure() handler or by blocking code waiting for completion. // from an onFailure() handler or by blocking code waiting for completion.
getHttpDestination().close(this); getHttpDestination().close(this);
session.goAway(new GoAwayInfo(), new Callback.Adapter()); session.goAway(new GoAwayInfo(), Callback.Adapter.INSTANCE);
abort(new AsynchronousCloseException()); abort(new AsynchronousCloseException());
} }

View File

@ -102,7 +102,7 @@ public class HttpReceiverOverSPDY extends HttpReceiver implements StreamFrameLis
public StreamFrameListener onPush(Stream stream, PushInfo pushInfo) public StreamFrameListener onPush(Stream stream, PushInfo pushInfo)
{ {
// SPDY push not supported // SPDY push not supported
getHttpChannel().getSession().rst(new RstInfo(stream.getId(), StreamStatus.REFUSED_STREAM), new Callback.Adapter()); getHttpChannel().getSession().rst(new RstInfo(stream.getId(), StreamStatus.REFUSED_STREAM), Callback.Adapter.INSTANCE);
return null; return null;
} }

View File

@ -154,7 +154,7 @@ public class ProxyEngineSelector extends ServerSessionFrameListener.Adapter
private void rst(Stream stream) private void rst(Stream stream)
{ {
RstInfo rstInfo = new RstInfo(stream.getId(), StreamStatus.REFUSED_STREAM); RstInfo rstInfo = new RstInfo(stream.getId(), StreamStatus.REFUSED_STREAM);
stream.getSession().rst(rstInfo, new Callback.Adapter()); stream.getSession().rst(rstInfo, Callback.Adapter.INSTANCE);
} }
public static class ProxyServerInfo public static class ProxyServerInfo

View File

@ -201,7 +201,7 @@ public class ProxyHTTPSPDYConnection extends HttpConnection implements HttpParse
{ {
HttpGenerator.ResponseInfo info = new HttpGenerator.ResponseInfo(HttpVersion.fromString(headers.get HttpGenerator.ResponseInfo info = new HttpGenerator.ResponseInfo(HttpVersion.fromString(headers.get
("version").getValue()), null, 0, 502, "SPDY reset received from upstream server", false); ("version").getValue()), null, 0, 502, "SPDY reset received from upstream server", false);
send(info, null, true, new Callback.Adapter()); send(info, null, true, Callback.Adapter.INSTANCE);
} }
@Override @Override

View File

@ -189,7 +189,7 @@ public class SPDYProxyEngine extends ProxyEngine implements StreamFrameListener
Session existing = serverSessions.putIfAbsent(host, session); Session existing = serverSessions.putIfAbsent(host, session);
if (existing != null) if (existing != null)
{ {
session.goAway(new GoAwayInfo(), new Callback.Adapter()); session.goAway(new GoAwayInfo(), Callback.Adapter.INSTANCE);
session = existing; session = existing;
} }
} }
@ -222,7 +222,7 @@ public class SPDYProxyEngine extends ProxyEngine implements StreamFrameListener
private void rst(Stream stream) private void rst(Stream stream)
{ {
RstInfo rstInfo = new RstInfo(stream.getId(), StreamStatus.REFUSED_STREAM); RstInfo rstInfo = new RstInfo(stream.getId(), StreamStatus.REFUSED_STREAM);
stream.getSession().rst(rstInfo, new Callback.Adapter()); stream.getSession().rst(rstInfo, Callback.Adapter.INSTANCE);
} }
private class ProxyPushStreamFrameListener implements StreamFrameListener private class ProxyPushStreamFrameListener implements StreamFrameListener
@ -581,7 +581,7 @@ public class SPDYProxyEngine extends ProxyEngine implements StreamFrameListener
{ {
Session clientSession = clientStream.getSession(); Session clientSession = clientStream.getSession();
RstInfo clientRstInfo = new RstInfo(clientStream.getId(), serverRstInfo.getStreamStatus()); RstInfo clientRstInfo = new RstInfo(clientStream.getId(), serverRstInfo.getStreamStatus());
clientSession.rst(clientRstInfo, new Callback.Adapter()); clientSession.rst(clientRstInfo, Callback.Adapter.INSTANCE);
} }
} }
} }

View File

@ -182,7 +182,7 @@ public class SPDYServerConnectionFactory extends AbstractConnectionFactory
void closeSessions() void closeSessions()
{ {
for (Session session : sessions) for (Session session : sessions)
session.goAway(new GoAwayInfo(), new Callback.Adapter()); session.goAway(new GoAwayInfo(), Callback.Adapter.INSTANCE);
sessions.clear(); sessions.clear();
} }

View File

@ -60,6 +60,12 @@ public interface Callback
*/ */
public static class Adapter implements Callback public static class Adapter implements Callback
{ {
/**
* Instance of Adapter that can be used when the callback methods need an empty
* implementation without incurring in the cost of allocating a new Adapter object.
*/
public static final Adapter INSTANCE = new Adapter();
@Override @Override
public void succeeded() public void succeeded()
{ {