Package io.grpc.servlet.jakarta
Class AsyncServletOutputStreamWriter
- java.lang.Object
-
- io.grpc.servlet.jakarta.AsyncServletOutputStreamWriter
-
final class AsyncServletOutputStreamWriter extends java.lang.ObjectHandles write actions from the container thread and the application thread.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static interfaceAsyncServletOutputStreamWriter.ActionItemWrite actions, e.g.(package private) static interfaceAsyncServletOutputStreamWriter.Logprivate static classAsyncServletOutputStreamWriter.WriteState
-
Field Summary
Fields Modifier and Type Field Description private AsyncServletOutputStreamWriter.ActionItemcompleteActionprivate AsyncServletOutputStreamWriter.ActionItemflushActionprivate java.util.function.BooleanSupplierisReadyprivate AsyncServletOutputStreamWriter.Loglogprivate java.lang.ThreadparkingThreadprivate java.util.function.BiFunction<byte[],java.lang.Integer,AsyncServletOutputStreamWriter.ActionItem>writeActionprivate java.util.Queue<AsyncServletOutputStreamWriter.ActionItem>writeChainNew 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>writeStateMemory boundary for write actions.
-
Constructor Summary
Constructors Constructor Description AsyncServletOutputStreamWriter(jakarta.servlet.AsyncContext asyncContext, ServletServerStream.ServletTransportState transportState, InternalLogId logId)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 jakarta.servlet.* dependency, so that Lincheck can run.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidassureReadyAndDrainedTurnsFalse()(package private) voidcomplete()Called from application thread.(package private) voidflush()Called from application thread.(package private) voidonWritePossible()Called 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 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 turnreadyAndDrainedfrom 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(jakarta.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 jakarta.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 Detail
-
writeBytes
void writeBytes(byte[] bytes, int numBytes) throws java.io.IOExceptionCalled from application thread.- Throws:
java.io.IOException
-
flush
void flush() throws java.io.IOExceptionCalled from application thread.- Throws:
java.io.IOException
-
complete
void complete()
Called from application thread.
-
onWritePossible
void onWritePossible() throws java.io.IOExceptionCalled 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
-
-