Skip to content

Commit 742352d

Browse files
committed
Adding locks when manipulating the _session_cache object in case of multithreading
Signed-off-by: Harmouch101 <[email protected]>
1 parent cfd97bd commit 742352d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

web3/_utils/request.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
from collections import (
2+
defaultdict,
3+
)
4+
from functools import _make_key # type: ignore
5+
from functools import lru_cache
6+
from threading import (
7+
RLock,
8+
)
19
from typing import (
210
Any,
11+
Callable,
312
)
413

514
from eth_typing import (
@@ -13,6 +22,22 @@
1322
)
1423

1524

25+
def thread_safe_lru(func: Callable[..., Any]) -> Callable[..., Any]:
26+
"""
27+
A thread-safe cache decorator that prevents concurrent invocations
28+
and allow reusing from cache.
29+
"""
30+
caching_func = lru_cache(maxsize=8, typed=False)(func)
31+
_keyed_locks = defaultdict(RLock) # type: ignore
32+
33+
def inner(*args: Any, **kwargs: Any) -> Callable[..., Any]:
34+
key = _make_key(args, kwargs, typed=False)
35+
with _keyed_locks[key]:
36+
return caching_func(*args, **kwargs)
37+
return inner
38+
39+
40+
@thread_safe_lru
1641
def cache_session(endpoint_uri: URI, session: requests.Session) -> None:
1742
cache_key = generate_cache_key(endpoint_uri)
1843
_session_cache[cache_key] = session
@@ -32,6 +57,7 @@ def _get_session(endpoint_uri: URI) -> requests.Session:
3257
return _session_cache[cache_key]
3358

3459

60+
@thread_safe_lru
3561
def make_post_request(endpoint_uri: URI, data: bytes, *args: Any, **kwargs: Any) -> bytes:
3662
kwargs.setdefault('timeout', 10)
3763
session = _get_session(endpoint_uri)

0 commit comments

Comments
 (0)