# Path: aredis/connection.py
# class ClusterConnection(Connection):
#     "Manages TCP communication to and from a Redis server"
#     description = "ClusterConnection<host={host},port={port}>"
# 
#     def __init__(self, *args, **kwargs):
#         self.readonly = kwargs.pop('readonly', False)
#         super(ClusterConnection, self).__init__(*args, **kwargs)
# 
#     async def on_connect(self):
#         """
#         Initialize the connection, authenticate and select a database and send READONLY if it is
#         set during object initialization.
#         """
#         if self.db:
#             warnings.warn('SELECT DB is not allowed in cluster mode')
#             self.db = ''
#         await super(ClusterConnection, self).on_connect()
#         if self.readonly:
#             await self.send_command('READONLY')
#             if nativestr(await self.read_response()) != 'OK':
#                 raise ConnectionError('READONLY command failed')
# 
# Path: aredis/exceptions.py
# class LockError(RedisError, ValueError):
#     """Errors acquiring or releasing a lock"""
#     # NOTE: For backwards compatability, this class derives from ValueError.
#     # This was originally chosen to behave like threading.Lock.
#     pass
# 
# class WatchError(RedisError):
#     pass
# 
# Path: aredis/utils.py
# def b(x):
#     return x.encode('latin-1') if not isinstance(x, bytes) else x
# 
# class dummy:
#     """
#     Instances of this class can be used as an attribute container.
#     """
# 
#     def __init__(self):
#         self.token = None
# 
#     def set(self, value):
#         self.token = value
# 
#     def get(self):
#         return self.token


import asyncio
import time as mod_time
import uuid
import warnings
import contextvars
from aredis.connection import ClusterConnection
from aredis.exceptions import LockError, WatchError
from aredis.utils import b, dummy
            time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
                     thread-1 sets the token to "abc"
            time: 1, thread-2 blocks trying to acquire `my-lock` using the
                     Lock instance.
            time: 5, thread-1 has not yet completed. redis expires the lock
                     key.
            time: 5, thread-2 acquired `my-lock` now that it's available.
                     thread-2 sets the token to "xyz"
            time: 6, thread-1 finishes its work and calls release(). if the
                     token is *not* stored in thread local storage, then
                     thread-1 would see the token value as "xyz" and would be
                     able to successfully release the thread-2's lock.

        In some use cases it's necessary to disable thread local storage. For
        example, if you have code where one thread acquires a lock and passes
        that lock instance to a worker thread to release later. If thread
        local storage isn't disabled in this case, the worker thread won't see
        the token set by the thread that acquired the lock. Our assumption
        is that these cases aren't common and as such default to using
        thread local storage.
        """
        self.redis = redis
        self.name = name
        self.timeout = timeout
        self.sleep = sleep
        self.blocking = blocking
        self.blocking_timeout = blocking_timeout
        self.thread_local = bool(thread_local)
        self.local = contextvars.ContextVar('token', default=None) if self.thread_local else dummy()
        if self.timeout and self.sleep > self.timeout:

# Path: aredis/connection.py
# class ClusterConnection(Connection):
#     "Manages TCP communication to and from a Redis server"
#     description = "ClusterConnection<host={host},port={port}>"
# 
#     def __init__(self, *args, **kwargs):
#         self.readonly = kwargs.pop('readonly', False)
#         super(ClusterConnection, self).__init__(*args, **kwargs)
# 
#     async def on_connect(self):
#         """
#         Initialize the connection, authenticate and select a database and send READONLY if it is
#         set during object initialization.
#         """
#         if self.db:
#             warnings.warn('SELECT DB is not allowed in cluster mode')
#             self.db = ''
#         await super(ClusterConnection, self).on_connect()
#         if self.readonly:
#             await self.send_command('READONLY')
#             if nativestr(await self.read_response()) != 'OK':
#                 raise ConnectionError('READONLY command failed')
# 
# Path: aredis/exceptions.py
# class LockError(RedisError, ValueError):
#     """Errors acquiring or releasing a lock"""
#     # NOTE: For backwards compatability, this class derives from ValueError.
#     # This was originally chosen to behave like threading.Lock.
#     pass
# 
# class WatchError(RedisError):
#     pass
# 
# Path: aredis/utils.py
# def b(x):
#     return x.encode('latin-1') if not isinstance(x, bytes) else x
# 
# class dummy:
#     """
#     Instances of this class can be used as an attribute container.
#     """
# 
#     def __init__(self):
#         self.token = None
# 
#     def set(self, value):
#         self.token = value
# 
#     def get(self):
#         return self.token


# Path: aredis/lock.py
import asyncio
import time as mod_time
import uuid
import warnings
import contextvars
from aredis.connection import ClusterConnection
from aredis.exceptions import LockError, WatchError
from aredis.utils import b, dummy

            time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
                     thread-1 sets the token to "abc"
            time: 1, thread-2 blocks trying to acquire `my-lock` using the
                     Lock instance.
            time: 5, thread-1 has not yet completed. redis expires the lock
                     key.
            time: 5, thread-2 acquired `my-lock` now that it's available.
                     thread-2 sets the token to "xyz"
            time: 6, thread-1 finishes its work and calls release(). if the
                     token is *not* stored in thread local storage, then
                     thread-1 would see the token value as "xyz" and would be
                     able to successfully release the thread-2's lock.

        In some use cases it's necessary to disable thread local storage. For
        example, if you have code where one thread acquires a lock and passes
        that lock instance to a worker thread to release later. If thread
        local storage isn't disabled in this case, the worker thread won't see
        the token set by the thread that acquired the lock. Our assumption
        is that these cases aren't common and as such default to using
        thread local storage.
        """
        self.redis = redis
        self.name = name
        self.timeout = timeout
        self.sleep = sleep
        self.blocking = blocking
        self.blocking_timeout = blocking_timeout
        self.thread_local = bool(thread_local)
        self.local = contextvars.ContextVar('token', default=None) if self.thread_local else dummy()
        if self.timeout and self.sleep > self.timeout:

            raise LockError("'sleep' must be less than 'timeout'")