Class RpcRateLimiter

Namespace
Unity.Multiplayer.Core

The server's per-sender rate limiter for ToServerAttribute calls: one TokenBucket per (sender, RPC), so a client flooding one RPC is throttled without touching other clients or its other RPCs.

public sealed class RpcRateLimiter
Inheritance
RpcRateLimiter
Inherited Members

Remarks

The RPC key is any stable string; the adapter uses the declaring type and method name, so the limit is per sender across every entity of that type (spawning more entities buys no extra rate).

Not thread-safe; the adapter calls it from Unity's main thread. Call Forget(ulong) when a client leaves.

Properties

BucketCount

The number of live buckets (one per sender and limited RPC it has called).

public int BucketCount { get; }

Property Value

int

Methods

Clear()

Drops everything (the session ended).

public void Clear()

Forget(ulong)

Drops every bucket and count for sender (it disconnected).

public void Forget(ulong sender)

Parameters

sender ulong

RejectedCount(ulong)

How many calls from sender this limiter has refused since it last forgot the sender.

public long RejectedCount(ulong sender)

Parameters

sender ulong

Returns

long

TryAcquire(ulong, string, double, int, double)

Lets one rpc call from sender through at now, or not.

public bool TryAcquire(ulong sender, string rpc, double ratePerSecond, int burst, double now)

Parameters

sender ulong

The calling client id.

rpc string

A stable key for the RPC.

ratePerSecond double

The limit; 0 or less means unlimited (always true, no bucket kept).

burst int

The bucket size; 0 or less means ratePerSecond (at least 1).

now double

The current time in seconds.

Returns

bool

True when the call is within the limit.

Exceptions

ArgumentNullException

rpc is null.