TTokenCoppice
Workflows

Coding workflows · Token bucket admission

03
Built around your work

A concrete starting point

Decide token-bucket admission from an explicit timestamp and return the next state. Check exact token exhaustion and capped refill, rejecting backward time, invalid costs and non-finite state values.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Token bucket admission

A concrete starting point
from math import isfinite
def admit(tokens, last, now, capacity, rate, cost):
    values = (tokens, last, now, capacity, rate, cost)
    if not all(type(x) in (int, float) and isfinite(x) for x in values):
        raise ValueError("finite numbers required")
    if not (capacity > 0 and rate > 0 and 0 < cost <= capacity
            and 0 <= tokens <= capacity and now >= last):
        raise ValueError("invalid bucket state")
    refill = min(capacity, tokens + (now - last) * rate)
    allowed = refill >= cost
    return (refill - cost if allowed else refill, now), allowed

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗