Package io.grpc.servlet
Class AsyncServletOutputStreamWriter
java.lang.Object
io.grpc.servlet.AsyncServletOutputStreamWriter
Handles write actions from the container thread and the application thread.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static interface
Write actions, e.g.(package private) static interface
private static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AsyncServletOutputStreamWriter.ActionItem
private final AsyncServletOutputStreamWriter.ActionItem
private final BooleanSupplier
private final AsyncServletOutputStreamWriter.Log
private Thread
private final BiFunction
<byte[], Integer, AsyncServletOutputStreamWriter.ActionItem> private final Queue
<AsyncServletOutputStreamWriter.ActionItem> New write actions will be buffered into this queue if the servlet output stream is not ready or the queue is not drained.private final AtomicReference
<AsyncServletOutputStreamWriter.WriteState> Memory boundary for write actions. -
Constructor Summary
ConstructorsConstructorDescriptionAsyncServletOutputStreamWriter
(BiFunction<byte[], Integer, AsyncServletOutputStreamWriter.ActionItem> writeAction, AsyncServletOutputStreamWriter.ActionItem flushAction, AsyncServletOutputStreamWriter.ActionItem completeAction, 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
Modifier and TypeMethodDescriptionprivate void
(package private) void
complete()
Called from application thread.(package private) void
flush()
Called from application thread.(package private) void
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 Details
-
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
-
writeAction
-
flushAction
-
completeAction
-
isReady
-
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
-
-
Constructor Details
-
AsyncServletOutputStreamWriter
AsyncServletOutputStreamWriter(javax.servlet.AsyncContext asyncContext, ServletServerStream.ServletTransportState transportState, InternalLogId logId) throws IOException - Throws:
IOException
-
AsyncServletOutputStreamWriter
AsyncServletOutputStreamWriter(BiFunction<byte[], Integer, AsyncServletOutputStreamWriter.ActionItem> writeAction, AsyncServletOutputStreamWriter.ActionItem flushAction, AsyncServletOutputStreamWriter.ActionItem completeAction, 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 Details
-
writeBytes
Called from application thread.- Throws:
IOException
-
flush
Called from application thread.- Throws:
IOException
-
complete
void complete()Called from application thread. -
onWritePossible
Called from the container threadWriteListener.onWritePossible()
.- Throws:
IOException
-
assureReadyAndDrainedTurnsFalse
private void assureReadyAndDrainedTurnsFalse() -
runOrBuffer
Either execute the write action directly, or buffer the action and let the container thread drain it.Called from application thread.
- Throws:
IOException
-