RateLimitCounter class
Rate limit counts shared by all isolates of the app.
The main isolate owns the counts. The other isolates send each
increment to it through a SendPort and wait for the new count.
incrementAndRead also reads a second key in the same message.
DoxIsolate gives every isolate it starts the port of the main
isolate, and the isolate calls connect with it.
In the main isolate (and in an app with one isolate) the count is made in place, with no message.
Properties
- calls → PortCalls
-
the calls to the owner, for tests
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- length → int
-
number of keys kept now, for tests
no setter
- maxKeys ↔ int?
-
see defaultMaxKeys; null is no cap. Only changed in tests.
getter/setter pair
- port → SendPort
-
The port other isolates send their increments to. Opened on the
first call, in the isolate that owns the counts (the main isolate).
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
) → void - close the ports and forget all counts
-
connect(
SendPort? owner) → void -
count in the isolate that owns
owner(a port) from now on. null counts in this isolate. -
increment(
String key, Duration ttl) → Future< int> -
Adds one to
keyand gives the new count. A new key, or a key older thanttl, starts at 1. -
incrementAndRead(
String key, String other, Duration ttl) → Future< (int, int)> -
Adds one to
keyand also readsotherwith no change to it. Gives the new count ofkeyand the count ofother(0 when it is missing or older than its ttl). It is one message to the owner. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → RateLimitCounter
-
one per isolate
final
Constants
- defaultMaxKeys → const int
- The most keys the counter keeps. One key is about 150 bytes, so this is about 15 MB. Without a cap, a client that sends from many ips (an IPv6 /64 has 2^64) could grow the map with no end.
- timeout → const Duration
- the most time to wait for the main isolate. After it, the count is made in this isolate, so a request never hangs (eg. during shutdown).