Package net.carcdr.yhocuspocus.redis
Class AwarenessThrottler
java.lang.Object
net.carcdr.yhocuspocus.redis.AwarenessThrottler
Throttles awareness updates to prevent excessive Redis traffic.
Awareness updates (cursor positions, user presence) are high-frequency and can overwhelm Redis with pub/sub messages. This throttler ensures that updates are not published more frequently than the configured interval.
Usage:
AwarenessThrottler throttler = new AwarenessThrottler(Duration.ofMillis(100));
// In awareness update handler:
if (throttler.tryAcquire(documentName)) {
// Publish awareness update to Redis
}
// else: skip this update, it's too soon
Thread-safe implementation using ConcurrentHashMap.
-
Constructor Summary
ConstructorsConstructorDescriptionAwarenessThrottler(Duration minInterval) Creates a new throttler with the specified minimum interval. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all tracking state.intGets the number of documents being tracked.voidRemoves tracking for a document.booleantryAcquire(String documentName) Attempts to acquire permission to send an awareness update.
-
Constructor Details
-
AwarenessThrottler
Creates a new throttler with the specified minimum interval.- Parameters:
minInterval- minimum interval between updates per document
-
-
Method Details
-
tryAcquire
Attempts to acquire permission to send an awareness update.Returns true if enough time has passed since the last update for the given document. If true is returned, the update timestamp is recorded and future calls will be throttled.
- Parameters:
documentName- the document name- Returns:
- true if the update should be sent, false if throttled
-
remove
Removes tracking for a document.Call this when a document is unloaded to free memory.
- Parameters:
documentName- the document name
-
clear
public void clear()Clears all tracking state. -
getTrackedCount
public int getTrackedCount()Gets the number of documents being tracked.- Returns:
- number of tracked documents
-