Class CloudOutbox
ack { ids } names it, and is resent after
every reconnect.
Bounded. The buffer never holds more than its capacity (default DEFAULT_CAPACITY). Enqueueing into
a full buffer, lowering the capacity, or loading more entries than fit drops the oldest entries and adds them to
droppedSinceLastReport(), which the session reports once as outbox.overflow { dropped }. The count
is reduced by the number actually reported (markOverflowReported(long)), so drops that happen while a
report is in flight are kept for the next report.
Thread safety. Every method is synchronized on this buffer, and every eviction runs inside the same
synchronized mutation that caused it. The monitor is a leaf lock: no other lock is taken while it is held.
Dirty tracking. Every mutation sets a dirty flag and increments a modification count; CloudStateStore
clears the flag after a successful write only if nothing changed since its snapshot
(clearDirtyIfUnchanged(long)), so a mutation that races a flush is never lost.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classOne unacknowledged reliable event.(package private) static final classThe persisted view of the buffer, read atomically bysnapshot(). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate final LongSupplier(package private) static final intThe default capacity;welcome.limits.outboxMaxoverrides it per connection (D-13, plan 22-08).private booleanprivate longprivate final LinkedHashMap<String, CloudOutbox.Entry> private long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) intack(Collection<String> ids) Removes the entries the gateway acknowledged.(package private) voidclear()Removes every entry and resets the dropped count.(package private) voidClears the dirty flag unconditionally.(package private) voidclearDirtyIfUnchanged(long expectedModCount) Clears the dirty flag only if nothing changed sinceexpectedModCountwas read.(package private) long(package private) CloudOutbox.EntryAdds an event with a new ULID id and the current time, dropping the oldest entry when full.private boolean(package private) booleanisDirty()(package private) voidmarkOverflowReported(long reported) Records that anoutbox.overflowcarryingreportedwas sent.(package private) longmodCount()private static intrequirePositive(int capacity) (package private) voidrestore(List<CloudOutbox.Entry> loaded, long loadedDropped) Replaces the content with entries loaded from the state file.(package private) voidsetCapacity(int newCapacity) Changes the capacity; a smaller capacity drops the oldest entries at once and counts them.(package private) CloudOutbox.Snapshotsnapshot()private voidtouch()(package private) List<CloudOutbox.Entry> unacked()
-
Field Details
-
DEFAULT_CAPACITY
static final int DEFAULT_CAPACITYThe default capacity;welcome.limits.outboxMaxoverrides it per connection (D-13, plan 22-08).- See Also:
-
clock
-
entries
-
capacity
private int capacity -
dropped
private long dropped -
dirty
private boolean dirty -
modCount
private long modCount
-
-
Constructor Details
-
CloudOutbox
CloudOutbox(int capacity, LongSupplier clock) - Parameters:
capacity- the maximum number of entries, at least 1clock- milliseconds since the Unix epoch, used for each event'sts
-
-
Method Details
-
requirePositive
private static int requirePositive(int capacity) -
enqueue
Adds an event with a new ULID id and the current time, dropping the oldest entry when full.- Parameters:
type- the envelope type, for exampleplayer.joinpayload- the event payload; copied, so later changes by the caller do not reach the buffer- Returns:
- the stored entry
-
unacked
List<CloudOutbox.Entry> unacked()- Returns:
- a snapshot of every unacknowledged entry, oldest first; later mutations do not change it
-
ack
Removes the entries the gateway acknowledged.- Parameters:
ids- the acknowledged envelope ids; unknown ids are ignored- Returns:
- the number of entries removed; the dirty flag changes only if it is positive
-
droppedSinceLastReport
long droppedSinceLastReport()- Returns:
- the number of entries dropped for lack of space since the last reported overflow
-
markOverflowReported
void markOverflowReported(long reported) Records that anoutbox.overflowcarryingreportedwas sent. Only that many are subtracted, so drops counted after the report was built remain for the next one.- Parameters:
reported- thedroppedvalue that was sent
-
setCapacity
void setCapacity(int newCapacity) Changes the capacity; a smaller capacity drops the oldest entries at once and counts them.- Parameters:
newCapacity- the new maximum, at least 1
-
clear
void clear()Removes every entry and resets the dropped count. -
restore
Replaces the content with entries loaded from the state file. Entries beyond the capacity keep the newest and add the rest to the dropped count. The buffer is marked dirty only when the loaded content had to be trimmed and clean otherwise, so a load alone does not cause a rewrite.- Parameters:
loaded- the entries, oldest firstloadedDropped- the persisted dropped count
-
isDirty
boolean isDirty()- Returns:
- whether anything changed since the last successful write
-
clearDirty
void clearDirty()Clears the dirty flag unconditionally. -
modCount
long modCount()- Returns:
- the modification count, for
clearDirtyIfUnchanged(long)
-
clearDirtyIfUnchanged
void clearDirtyIfUnchanged(long expectedModCount) Clears the dirty flag only if nothing changed sinceexpectedModCountwas read.- Parameters:
expectedModCount- themodCount()captured with the persisted snapshot
-
snapshot
CloudOutbox.Snapshot snapshot()- Returns:
- the entries, dropped count and modification count, read together under this buffer's monitor
-
evictToCapacity
private boolean evictToCapacity() -
touch
private void touch()
-