def acquire_and_get(self, host: str, port: int) -> bool | None: # By the end of this block we know that # _cache_[values,locks] is available.
value = None with self._lock:
key = (host, port) try:
value = self._cache_values[key] # If it's a known value we return right away. if value isnotNone: return value except KeyError:
self._cache_locks[key] = threading.RLock()
self._cache_values[key] = None
# If the value is unknown, we acquire the lock to signal # to the requesting thread that the probe is in progress # or that the current thread needs to return their findings.
key_lock = self._cache_locks[key]
key_lock.acquire() try: # If the by the time we get the lock the value has been # updated we want to return the updated value.
value = self._cache_values[key]
# In case an exception like KeyboardInterrupt is raised here. except BaseException as e: # Defensive: assertnot isinstance(e, KeyError) # KeyError shouldn't be possible.
key_lock.release() raise
return value
def set_and_release(
self, host: str, port: int, supports_http2: bool | None
) -> None:
key = (host, port)
key_lock = self._cache_locks[key] with key_lock: # Uses an RLock, so can be locked again from same thread. if supports_http2 isNoneand self._cache_values[key] isnotNone: raise ValueError( "Cannot reset HTTP/2 support for origin after value has been set."
) # Defensive: not expected in normal usage
def _values(self) -> dict[tuple[str, int], bool | None]: """This function is for testing purposes only. Gets the current state of the probe cache""" with self._lock: return {k: v for k, v in self._cache_values.items()}
def _reset(self) -> None: """This function is for testing purposes only. Reset the cache values""" with self._lock:
self._cache_locks = {}
self._cache_values = {}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.