TokenBucket

class TokenBucket(val capacity: Int, val windowMillis: Long, startMillis: Long = 0)

A classic token bucket: capacity tokens that refill smoothly over windowMillis.

This is the "max per chunk of time" primitive — a player allowed 60 relocations per minute may burst all 60 at once, then trickles back in at one per second, rather than being hard-cut at a window boundary (which would let a player double their rate by straddling one).

Deliberately clock-injected rather than reading System.currentTimeMillis() internally: tests drive time forward explicitly, so rate behaviour is verified deterministically instead of by sleeping.

Constructors

Link copied to clipboard
constructor(capacity: Int, windowMillis: Long, startMillis: Long = 0)

Properties

Link copied to clipboard

Tokens currently available, for tests and diagnostics.

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun isFull(now: Long): Boolean

True when the bucket is completely full — i.e. the actor is idle and evictable.

Link copied to clipboard
fun refill(now: Long)

Refills without consuming — used when peeking at a budget.

Link copied to clipboard

Consumes one token if any is available, refilling first based on elapsed time. Returns true when the caller may proceed.