package org.glassfish.grizzly.http2;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.WriteHandler;
import org.glassfish.grizzly.WriteResult;
import org.glassfish.grizzly.asyncqueue.MessageCloner;
import org.glassfish.grizzly.asyncqueue.TaskQueue;
import org.glassfish.grizzly.http2.frames.DataFrame;
import org.glassfish.grizzly.http2.frames.ErrorCode;
import org.glassfish.grizzly.http2.frames.Http2Frame;
public class Http2SessionOutputSink {
protected final Http2Session http2Session;
private static final Logger LOGGER = Grizzly.logger(Http2SessionOutputSink.class);
private static final int MAX_FRAME_PAYLOAD_SIZE = 16383;
private static final int MAX_OUTPUT_QUEUE_SIZE = 65536;
private final TaskQueue<Http2OutputQueueRecord> outputQueue = TaskQueue.createTaskQueue(new TaskQueue.MutableMaxQueueSize() {
@Override
public int getMaxQueueSize() {
return MAX_OUTPUT_QUEUE_SIZE;
}
});
private final AtomicInteger availConnectionWindowSize;
private final List<Http2Frame> tmpFramesList = new LinkedList<>();
private final AtomicBoolean writerLock = new AtomicBoolean();
public Http2SessionOutputSink(Http2Session session) {
this.http2Session = session;
availConnectionWindowSize = new AtomicInteger(http2Session.getDefaultConnectionWindowSize());
}
protected Http2FrameCodec frameCodec() {
return http2Session.handlerFilter.frameCodec;
}
protected void writeDownStream(final Http2Frame frame) {
http2Session.getHttp2SessionChain().write(
http2Session.getConnection(), null,
frameCodec().serializeAndRecycle(http2Session, frame),
null, (MessageCloner) null);
}
protected void writeDownStream(final List<Http2Frame> frames) {
http2Session.getHttp2SessionChain().write(
http2Session.getConnection(), null,
frameCodec().serializeAndRecycle(http2Session, frames),
null, (MessageCloner) null);
}
@SuppressWarnings("unchecked")
protected <K> void writeDownStream(final K anyMessage,
final CompletionHandler<WriteResult> completionHandler,
final MessageCloner<Buffer> messageCloner) {
final Object msg;
if (anyMessage instanceof List) {
msg = frameCodec().serializeAndRecycle(http2Session, (List<Http2Frame>) anyMessage);
} else if (anyMessage instanceof Http2Frame) {
msg = frameCodec().serializeAndRecycle(http2Session, (Http2Frame) anyMessage);
} else {
msg = anyMessage;
}
http2Session.getHttp2SessionChain().write(http2Session.getConnection(), null, msg, completionHandler, messageCloner);
}
protected int getAvailablePeerConnectionWindowSize() {
return availConnectionWindowSize.get();
}
protected boolean canWrite() {
return outputQueue.size() < MAX_OUTPUT_QUEUE_SIZE;
}
protected void notifyCanWrite(final WriteHandler writeHandler) {
outputQueue.notifyWritePossible(writeHandler, MAX_OUTPUT_QUEUE_SIZE);
}
protected void onPeerWindowUpdate(final int delta) throws Http2SessionException {
final int currentWindow = availConnectionWindowSize.get();
if (delta > 0 && currentWindow > 0 && currentWindow + delta < 0) {
throw new Http2SessionException(ErrorCode.FLOW_CONTROL_ERROR, "Session flow-control window overflow.");
}
final int newWindowSize = availConnectionWindowSize.addAndGet(delta);
LOGGER.log(Level.FINE,
"Http2Session. Expand connection window size by {0} bytes. Current connection window size is: {1}",
new Object[] {delta, newWindowSize});
flushOutputQueue();
}
protected void writeDataDownStream(final Http2Stream stream, final List<Http2Frame> headerFrames, Buffer data,
final CompletionHandler<WriteResult> completionHandler, final MessageCloner<Buffer> messageCloner, final boolean isLast) {
if (data == null || !data.hasRemaining() && stream.getUnflushedWritesCount() == 1) {
if (data == null) {
writeDownStream(headerFrames, completionHandler, messageCloner);
return;
}
final DataFrame dataFrame = DataFrame.builder().streamId(stream.getId()).data(data).endStream(isLast).build();
final Object msg;
if (headerFrames != null && !headerFrames.isEmpty()) {
headerFrames.add(dataFrame);
msg = headerFrames;
} else {
msg = dataFrame;
}
writeDownStream(msg, completionHandler, messageCloner);
return;
} else if (headerFrames != null && !headerFrames.isEmpty()) {
writeDownStream(headerFrames);
}
final int dataSize = data.remaining();
if (messageCloner != null) {
data = messageCloner.clone(http2Session.getConnection(), data);
}
final Http2OutputQueueRecord record = new Http2OutputQueueRecord(stream.getId(), data, completionHandler, isLast);
outputQueue.offer(record);
outputQueue.reserveSpace(record.isZeroSizeData() ? 1 : dataSize);
flushOutputQueue();
}
private void flushOutputQueue() {
int backoffDelay = 0;
int availWindowSize;
int queueSize;
boolean needToNotifyQueueManagement = false;
while (availConnectionWindowSize.get() > 0 && !outputQueue.isEmpty() && writerLock.compareAndSet(false, true)) {
availWindowSize = availConnectionWindowSize.get();
queueSize = outputQueue.size();
AggrCompletionHandler completionHandlers = null;
CompletionHandler<WriteResult> writeCompletionHandler = null;
int writeCompletionHandlerBytes = 0;
int bytesToTransfer = 0;
int queueSizeToFree = 0;
boolean breakNow = false;
while (availWindowSize > bytesToTransfer && queueSize > queueSizeToFree) {
final Http2OutputQueueRecord record = outputQueue.poll();
if (record == null) {
LOGGER.log(Level.WARNING, "UNEXPECTED NULL RECORD. Queue-size: {0} "
+ "byteToTransfer={1} queueSizeToFree={2} queueSize={3}",
new Object[]{outputQueue.size(), bytesToTransfer, queueSizeToFree, queueSize});
breakNow = true;
break;
}
final int serializedBytes = record.serializeTo(tmpFramesList,
Math.min(MAX_FRAME_PAYLOAD_SIZE, availWindowSize - bytesToTransfer));
bytesToTransfer += serializedBytes;
queueSizeToFree += serializedBytes;
if (record.isFinished()) {
if (record.isZeroSizeData()) {
queueSizeToFree++;
}
} else {
outputQueue.setCurrentElement(record);
}
final CompletionHandler<WriteResult> recordCompletionHandler = record.getCompletionHandler();
if (recordCompletionHandler != null) {
if (completionHandlers != null) {
completionHandlers.register(recordCompletionHandler, serializedBytes);
} else if (writeCompletionHandler == null) {
writeCompletionHandler = recordCompletionHandler;
writeCompletionHandlerBytes = serializedBytes;
} else {
completionHandlers = new AggrCompletionHandler();
completionHandlers.register(writeCompletionHandler, writeCompletionHandlerBytes);
completionHandlers.register(recordCompletionHandler, serializedBytes);
writeCompletionHandler = completionHandlers;
}
}
}
if (queueSizeToFree > 0) {
assert !tmpFramesList.isEmpty();
writeDownStream(tmpFramesList, writeCompletionHandler, null);
final int newWindowSize = availConnectionWindowSize.addAndGet(-bytesToTransfer);
outputQueue.releaseSpace(queueSizeToFree);
needToNotifyQueueManagement = true;
LOGGER.log(Level.FINE,
"Http2Session. Shrink connection window size by {0} bytes. Current connection window size is: {1}",
new Object[] {bytesToTransfer, newWindowSize});
}
writerLock.set(false);
if (breakNow) {
break;
}
LockSupport.parkNanos(backoffDelay++);
}
if (needToNotifyQueueManagement) {
outputQueue.doNotify();
}
}
public void close() {
outputQueue.onClose();
}
}