evennia.server.throttle

class evennia.server.throttle.Throttle(**kwargs)[source]

Bases: object

Keeps a running count of failed actions per IP address.

Available methods indicate whether or not the number of failures exceeds a particular threshold.

This version of the throttle is usable by both the terminal server as well as the web server, imposes limits on memory consumption by using deques with length limits instead of open-ended lists, and uses native Django caches for automatic key eviction and persistence configurability.

error_msg = 'Too many failed attempts; you must wait a few minutes before trying again.'
__init__(**kwargs)[source]

Allows setting of throttle parameters.

Keyword Arguments
  • name (str) – Name of this throttle.

  • limit (int) – Max number of failures before imposing limiter. If None, the throttle is disabled.

  • timeout (int) – number of timeout seconds after max number of tries has been reached.

  • cache_size (int) – Max number of attempts to record per IP within a rolling window; this is NOT the same as the limit after which the throttle is imposed!

get_cache_key(*args, **kwargs)[source]

Creates a ‘prefixed’ key containing arbitrary terms to prevent key collisions in the same namespace.

touch(key, *args, **kwargs)[source]

Refreshes the timeout on a given key and ensures it is recorded in the key register.

Parameters

key (str) – Key of entry to renew.

get(ip=None)[source]

Convenience function that returns the storage table, or part of.

Parameters

ip (str, optional) – IP address of requestor

Returns

storage (dict)

When no IP is provided, returns a dict of all

current IPs being tracked and the timestamps of their recent failures.

timestamps (deque): When an IP is provided, returns a deque of

timestamps of recent failures only for that IP.

update(ip, failmsg='Exceeded threshold.')[source]

Store the time of the latest failure.

Parameters
  • ip (str) – IP address of requestor

  • failmsg (str, optional) – Message to display in logs upon activation of throttle.

Returns

None

remove(ip, *args, **kwargs)[source]

Clears data stored for an IP from the throttle.

Parameters

ip (str) – IP to clear.

record_ip(ip, *args, **kwargs)[source]

Tracks keys as they are added to the cache (since there is no way to get a list of keys after-the-fact).

Parameters

ip (str) – IP being added to cache. This should be the original IP, not the cache-prefixed key.

unrecord_ip(ip, *args, **kwargs)[source]

Forces removal of a key from the key registry.

Parameters

ip (str) – IP to remove from list of keys.

check(ip)[source]

This will check the session’s address against the storage dictionary to check they haven’t spammed too many fails recently.

Parameters

ip (str) – IP address of requestor

Returns

throttled (bool)

True if throttling is active,

False otherwise.