Package io.grpc.servlet
Class AsyncServletOutputStreamWriter
- java.lang.Object
-
- io.grpc.servlet.AsyncServletOutputStreamWriter
-
final class AsyncServletOutputStreamWriter extends java.lang.Object
Handles write actions from the container thread and the application thread.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static interface
AsyncServletOutputStreamWriter.ActionItem
Write actions, e.g.(package private) static interface
AsyncServletOutputStreamWriter.Log
private static class
AsyncServletOutputStreamWriter.WriteState
-
Field Summary
Fields Modifier and Type Field Description private AsyncServletOutputStreamWriter.ActionItem
completeAction
private AsyncServletOutputStreamWriter.ActionItem
flushAction
private java.util.function.BooleanSupplier
isReady
private AsyncServletOutputStreamWriter.Log
log
private java.lang.Thread
parkingThread
private java.util.function.BiFunction<byte[],java.lang.Integer,AsyncServletOutputStreamWriter.ActionItem>
writeAction
private java.util.Queue<AsyncServletOutputStreamWriter.ActionItem>
writeChain
New write actions will be buffered into this queue if the servlet output stream is not ready or the queue is not drained.private java.util.concurrent.atomic.AtomicReference<AsyncServletOutputStreamWriter.WriteState>
writeState
Memory boundary for write actions.
-
Constructor Summary
Constructors Constructor Description AsyncServletOutputStreamWriter(java.util.function.BiFunction<byte[],java.lang.Integer,AsyncServletOutputStreamWriter.ActionItem> writeAction, AsyncServletOutputStreamWriter.ActionItem flushAction, AsyncServletOutputStreamWriter.ActionItem completeAction, java.util.function.BooleanSupplier isReady, AsyncServletOutputStreamWriter.Log log)
Constructor without java.util.logging and javax.servlet.* dependency, so that Lincheck can run.AsyncServletOutputStreamWriter(javax.servlet.AsyncContext asyncContext, ServletServerStream.ServletTransportState transportState, InternalLogId logId)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
assureReadyAndDrainedTurnsFalse()
(package private) void
complete()
Called from application thread.(package private) void
flush()
Called from application thread.(package private) void
onWritePossible()
Called from the container threadWriteListener.onWritePossible()
.private void
runOrBuffer(AsyncServletOutputStreamWriter.ActionItem actionItem)
Either execute the write action directly, or buffer the action and let the container thread drain it.(package private) void
writeBytes(byte[] bytes, int numBytes)
Called from application thread.
-
-
-
Field Detail
-
writeState
private final java.util.concurrent.atomic.AtomicReference<AsyncServletOutputStreamWriter.WriteState> writeState
Memory boundary for write actions.WriteState curState = writeState.get(); // mark a boundary doSomething(); // do something within the boundary boolean successful = writeState.compareAndSet(curState, newState); // try to mark a boundary if (successful) { // state has not changed since return; } else { // state is changed by another thread while doSomething(), need recompute }
There are two threads, the container thread (calling
onWritePossible()
) and the application thread (callingrunOrBuffer()
) that read and update the writeState. Only onWritePossible() may turnreadyAndDrained
from false to true, and only runOrBuffer() may turn it from true to false.
-
log
private final AsyncServletOutputStreamWriter.Log log
-
writeAction
private final java.util.function.BiFunction<byte[],java.lang.Integer,AsyncServletOutputStreamWriter.ActionItem> writeAction
-
flushAction
private final AsyncServletOutputStreamWriter.ActionItem flushAction
-
completeAction
private final AsyncServletOutputStreamWriter.ActionItem completeAction
-
isReady
private final java.util.function.BooleanSupplier isReady
-
writeChain
private final java.util.Queue<AsyncServletOutputStreamWriter.ActionItem> writeChain
New write actions will be buffered into this queue if the servlet output stream is not ready or the queue is not drained.
-
parkingThread
@Nullable private volatile java.lang.Thread parkingThread
-
-
Constructor Detail
-
AsyncServletOutputStreamWriter
AsyncServletOutputStreamWriter(javax.servlet.AsyncContext asyncContext, ServletServerStream.ServletTransportState transportState, InternalLogId logId) throws java.io.IOException
- Throws:
java.io.IOException
-
AsyncServletOutputStreamWriter
AsyncServletOutputStreamWriter(java.util.function.BiFunction<byte[],java.lang.Integer,AsyncServletOutputStreamWriter.ActionItem> writeAction, AsyncServletOutputStreamWriter.ActionItem flushAction, AsyncServletOutputStreamWriter.ActionItem completeAction, java.util.function.BooleanSupplier isReady, AsyncServletOutputStreamWriter.Log log)
Constructor without java.util.logging and javax.servlet.* dependency, so that Lincheck can run.- Parameters:
writeAction
- Provides anAsyncServletOutputStreamWriter.ActionItem
to write given bytes with specified length.isReady
- Indicates whether the writer can write bytes at the moment (asynchronously).
-
-
Method Detail
-
writeBytes
void writeBytes(byte[] bytes, int numBytes) throws java.io.IOException
Called from application thread.- Throws:
java.io.IOException
-
flush
void flush() throws java.io.IOException
Called from application thread.- Throws:
java.io.IOException
-
complete
void complete()
Called from application thread.
-
onWritePossible
void onWritePossible() throws java.io.IOException
Called from the container threadWriteListener.onWritePossible()
.- Throws:
java.io.IOException
-
assureReadyAndDrainedTurnsFalse
private void assureReadyAndDrainedTurnsFalse()
-
runOrBuffer
private void runOrBuffer(AsyncServletOutputStreamWriter.ActionItem actionItem) throws java.io.IOException
Either execute the write action directly, or buffer the action and let the container thread drain it.Called from application thread.
- Throws:
java.io.IOException
-
-