memcached.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. from __future__ import annotations
  2. import time
  3. import urllib.parse
  4. from collections.abc import Iterable
  5. from math import ceil, floor
  6. from deprecated.sphinx import versionadded
  7. from limits.aio.storage.base import SlidingWindowCounterSupport, Storage
  8. from limits.storage.base import TimestampedSlidingWindow
  9. from limits.typing import EmcacheClientP, ItemP
  10. @versionadded(version="2.1")
  11. class MemcachedStorage(Storage, SlidingWindowCounterSupport, TimestampedSlidingWindow):
  12. """
  13. Rate limit storage with memcached as backend.
  14. Depends on :pypi:`emcache`
  15. """
  16. STORAGE_SCHEME = ["async+memcached"]
  17. """The storage scheme for memcached to be used in an async context"""
  18. DEPENDENCIES = ["emcache"]
  19. def __init__(
  20. self,
  21. uri: str,
  22. wrap_exceptions: bool = False,
  23. **options: float | str | bool,
  24. ) -> None:
  25. """
  26. :param uri: memcached location of the form
  27. ``async+memcached://host:port,host:port``
  28. :param wrap_exceptions: Whether to wrap storage exceptions in
  29. :exc:`limits.errors.StorageError` before raising it.
  30. :param options: all remaining keyword arguments are passed
  31. directly to the constructor of :class:`emcache.Client`
  32. :raise ConfigurationError: when :pypi:`emcache` is not available
  33. """
  34. parsed = urllib.parse.urlparse(uri)
  35. self.hosts = []
  36. for host, port in (
  37. loc.split(":") for loc in parsed.netloc.strip().split(",") if loc.strip()
  38. ):
  39. self.hosts.append((host, int(port)))
  40. self._options = options
  41. self._storage = None
  42. super().__init__(uri, wrap_exceptions=wrap_exceptions, **options)
  43. self.dependency = self.dependencies["emcache"].module
  44. @property
  45. def base_exceptions(
  46. self,
  47. ) -> type[Exception] | tuple[type[Exception], ...]: # pragma: no cover
  48. return (
  49. self.dependency.ClusterNoAvailableNodes,
  50. self.dependency.CommandError,
  51. )
  52. async def get_storage(self) -> EmcacheClientP:
  53. if not self._storage:
  54. self._storage = await self.dependency.create_client(
  55. [self.dependency.MemcachedHostAddress(h, p) for h, p in self.hosts],
  56. **self._options,
  57. )
  58. assert self._storage
  59. return self._storage
  60. async def get(self, key: str) -> int:
  61. """
  62. :param key: the key to get the counter value for
  63. """
  64. item = await (await self.get_storage()).get(key.encode("utf-8"))
  65. return item and int(item.value) or 0
  66. async def get_many(self, keys: Iterable[str]) -> dict[bytes, ItemP]:
  67. """
  68. Return multiple counters at once
  69. :param keys: the keys to get the counter values for
  70. """
  71. return await (await self.get_storage()).get_many(
  72. [k.encode("utf-8") for k in keys]
  73. )
  74. async def clear(self, key: str) -> None:
  75. """
  76. :param key: the key to clear rate limits for
  77. """
  78. await (await self.get_storage()).delete(key.encode("utf-8"))
  79. async def decr(self, key: str, amount: int = 1, noreply: bool = False) -> int:
  80. """
  81. decrements the counter for a given rate limit key
  82. retursn 0 if the key doesn't exist or if noreply is set to True
  83. :param key: the key to decrement
  84. :param amount: the number to decrement by
  85. :param noreply: set to True to ignore the memcached response
  86. """
  87. storage = await self.get_storage()
  88. limit_key = key.encode("utf-8")
  89. try:
  90. value = await storage.decrement(limit_key, amount, noreply=noreply) or 0
  91. except self.dependency.NotFoundCommandError:
  92. value = 0
  93. return value
  94. async def incr(
  95. self,
  96. key: str,
  97. expiry: float,
  98. elastic_expiry: bool = False,
  99. amount: int = 1,
  100. set_expiration_key: bool = True,
  101. ) -> int:
  102. """
  103. increments the counter for a given rate limit key
  104. :param key: the key to increment
  105. :param expiry: amount in seconds for the key to expire in
  106. :param elastic_expiry: whether to keep extending the rate limit
  107. window every hit.
  108. :param amount: the number to increment by
  109. :param set_expiration_key: if set to False, the expiration time won't be stored but the key will still expire
  110. """
  111. storage = await self.get_storage()
  112. limit_key = key.encode("utf-8")
  113. expire_key = self._expiration_key(key).encode()
  114. value = None
  115. try:
  116. value = await storage.increment(limit_key, amount) or amount
  117. if elastic_expiry:
  118. await storage.touch(limit_key, exptime=ceil(expiry))
  119. if set_expiration_key:
  120. await storage.set(
  121. expire_key,
  122. str(expiry + time.time()).encode("utf-8"),
  123. exptime=ceil(expiry),
  124. noreply=False,
  125. )
  126. return value
  127. except self.dependency.NotFoundCommandError:
  128. # Incrementation failed because the key doesn't exist
  129. storage = await self.get_storage()
  130. try:
  131. await storage.add(limit_key, f"{amount}".encode(), exptime=ceil(expiry))
  132. if set_expiration_key:
  133. await storage.set(
  134. expire_key,
  135. str(expiry + time.time()).encode("utf-8"),
  136. exptime=ceil(expiry),
  137. noreply=False,
  138. )
  139. value = amount
  140. except self.dependency.NotStoredStorageCommandError:
  141. # Coult not add the key, probably because a concurrent call has added it
  142. storage = await self.get_storage()
  143. value = await storage.increment(limit_key, amount) or amount
  144. if elastic_expiry:
  145. await storage.touch(limit_key, exptime=ceil(expiry))
  146. if set_expiration_key:
  147. await storage.set(
  148. expire_key,
  149. str(expiry + time.time()).encode("utf-8"),
  150. exptime=ceil(expiry),
  151. noreply=False,
  152. )
  153. return value
  154. async def get_expiry(self, key: str) -> float:
  155. """
  156. :param key: the key to get the expiry for
  157. """
  158. storage = await self.get_storage()
  159. item = await storage.get(self._expiration_key(key).encode("utf-8"))
  160. return item and float(item.value) or time.time()
  161. def _expiration_key(self, key: str) -> str:
  162. """
  163. Return the expiration key for the given counter key.
  164. Memcached doesn't natively return the expiration time or TTL for a given key,
  165. so we implement the expiration time on a separate key.
  166. """
  167. return key + "/expires"
  168. async def check(self) -> bool:
  169. """
  170. Check if storage is healthy by calling the ``get`` command
  171. on the key ``limiter-check``
  172. """
  173. try:
  174. storage = await self.get_storage()
  175. await storage.get(b"limiter-check")
  176. return True
  177. except: # noqa
  178. return False
  179. async def reset(self) -> int | None:
  180. raise NotImplementedError
  181. async def acquire_sliding_window_entry(
  182. self,
  183. key: str,
  184. limit: int,
  185. expiry: int,
  186. amount: int = 1,
  187. ) -> bool:
  188. if amount > limit:
  189. return False
  190. now = time.time()
  191. previous_key, current_key = self.sliding_window_keys(key, expiry, now)
  192. (
  193. previous_count,
  194. previous_ttl,
  195. current_count,
  196. _,
  197. ) = await self._get_sliding_window_info(previous_key, current_key, expiry, now)
  198. t0 = time.time()
  199. weighted_count = previous_count * previous_ttl / expiry + current_count
  200. if floor(weighted_count) + amount > limit:
  201. return False
  202. else:
  203. # Hit, increase the current counter.
  204. # If the counter doesn't exist yet, set twice the theorical expiry.
  205. # We don't need the expiration key as it is estimated with the timestamps directly.
  206. current_count = await self.incr(
  207. current_key, 2 * expiry, amount=amount, set_expiration_key=False
  208. )
  209. t1 = time.time()
  210. actualised_previous_ttl = max(0, previous_ttl - (t1 - t0))
  211. weighted_count = (
  212. previous_count * actualised_previous_ttl / expiry + current_count
  213. )
  214. if floor(weighted_count) > limit:
  215. # Another hit won the race condition: revert the incrementation and refuse this hit
  216. # Limitation: during high concurrency at the end of the window,
  217. # the counter is shifted and cannot be decremented, so less requests than expected are allowed.
  218. await self.decr(current_key, amount, noreply=True)
  219. return False
  220. return True
  221. async def get_sliding_window(
  222. self, key: str, expiry: int
  223. ) -> tuple[int, float, int, float]:
  224. now = time.time()
  225. previous_key, current_key = self.sliding_window_keys(key, expiry, now)
  226. return await self._get_sliding_window_info(
  227. previous_key, current_key, expiry, now
  228. )
  229. async def _get_sliding_window_info(
  230. self, previous_key: str, current_key: str, expiry: int, now: float
  231. ) -> tuple[int, float, int, float]:
  232. result = await self.get_many([previous_key, current_key])
  233. raw_previous_count = result.get(previous_key.encode("utf-8"))
  234. raw_current_count = result.get(current_key.encode("utf-8"))
  235. current_count = raw_current_count and int(raw_current_count.value) or 0
  236. previous_count = raw_previous_count and int(raw_previous_count.value) or 0
  237. if previous_count == 0:
  238. previous_ttl = float(0)
  239. else:
  240. previous_ttl = (1 - (((now - expiry) / expiry) % 1)) * expiry
  241. current_ttl = (1 - ((now / expiry) % 1)) * expiry + expiry
  242. return previous_count, previous_ttl, current_count, current_ttl