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 interfaceWrite actions, e.g.(package private) static interfaceprivate static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AsyncServletOutputStreamWriter.ActionItemprivate final AsyncServletOutputStreamWriter.ActionItemprivate final BooleanSupplierprivate final AsyncServletOutputStreamWriter.Logprivate Threadprivate 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) voidcomplete()Called from application thread.(package private) voidflush()Called from application thread.(package private) voidCalled from the container threadWriteListener.onWritePossible().private voidrunOrBuffer(AsyncServletOutputStreamWriter.ActionItem actionItem) Either execute the write action directly, or buffer the action and let the container thread drain it.(package private) voidwriteBytes(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 turnreadyAndDrainedfrom 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.ActionItemto 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
-