evennia.server.throttle

class evennia.server.throttle.Throttle(**kwargs)[源代码]

基类: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)[源代码]

Allows setting of throttle parameters.

关键字参数
  • 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)[源代码]

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

touch(key, *args, **kwargs)[源代码]

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

参数

key (str) – Key of entry to renew.

get(ip=None)[源代码]

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

参数

ip (str, optional) – IP address of requestor

返回

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.')[源代码]

Store the time of the latest failure.

参数
  • ip (str) – IP address of requestor

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

返回

None

remove(ip, *args, **kwargs)[源代码]

Clears data stored for an IP from the throttle.

参数

ip (str) – IP to clear.

record_ip(ip, *args, **kwargs)[源代码]

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

参数

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

unrecord_ip(ip, *args, **kwargs)[源代码]

Forces removal of a key from the key registry.

参数

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

check(ip)[源代码]

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

参数

ip (str) – IP address of requestor

返回

throttled (bool)

True if throttling is active,

False otherwise.