b .|ddlZddlZddlZddlZddlmZddlmZmZm Z m Z ddl m Z m Z erddlmZGddZy)N)SimpleNamespace) TYPE_CHECKING AwaitableOptionalUnion) LockErrorLockNotOwnedError)RediscVeZdZdZdZdZdZdZdZdZ d!ddde e e e fd eed ed ed eed efdZdZdZdZ d"d eed eedee e e ffdZde e e fdefdZdefdZdefdZdedfdZde ddfdZ d#dededeefdZdefdZdeefdZdefd Zy)$Lockz A shared, distributed Lock. Using Redis for locking allows the Lock to be shared across processes and/or machines. It's left to the user to resolve deadlock issues and make sure multiple clients play nicely together. Nz local token = redis.call('get', KEYS[1]) if not token or token ~= ARGV[1] then return 0 end redis.call('del', KEYS[1]) return 1 a local token = redis.call('get', KEYS[1]) if not token or token ~= ARGV[1] then return 0 end local expiration = redis.call('pttl', KEYS[1]) if not expiration then expiration = 0 end if expiration < 0 then return 0 end local newttl = ARGV[2] if ARGV[3] == "0" then newttl = ARGV[2] + expiration end redis.call('pexpire', KEYS[1], newttl) return 1 z local token = redis.call('get', KEYS[1]) if not token or token ~= ARGV[1] then return 0 end redis.call('pexpire', KEYS[1], ARGV[2]) return 1 redisr nametimeoutsleepblockingblocking_timeout thread_localc||_||_||_||_||_||_t ||_|jrtjn t|_ d|j_ |jy)a Create a new Lock instance named ``name`` using the Redis client supplied by ``redis``. ``timeout`` indicates a maximum life for the lock in seconds. By default, it will remain locked until release() is called. ``timeout`` can be specified as a float or integer, both representing the number of seconds to wait. ``sleep`` indicates the amount of time to sleep in seconds per loop iteration when the lock is in blocking mode and another client is currently holding the lock. ``blocking`` indicates whether calling ``acquire`` should block until the lock has been acquired or to fail immediately, causing ``acquire`` to return False and the lock not being acquired. Defaults to True. Note this value can be overridden by passing a ``blocking`` argument to ``acquire``. ``blocking_timeout`` indicates the maximum amount of time in seconds to spend trying to acquire the lock. A value of ``None`` indicates continue trying forever. ``blocking_timeout`` can be specified as a float or integer, both representing the number of seconds to wait. ``thread_local`` indicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline: 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. N) r rrrrrboolr threadinglocalrtokenregister_scripts)selfr rrrrrrs 4/usr/lib/python3/dist-packages/redis/asyncio/lock.py__init__z Lock.__init__Ospx      0 .*.*;*;Y__&AR   c>|j}|j}|j |j|j|_|j |j|j |_|j!|j|j|_yyN) __class__r lua_releaseregister_scriptLUA_RELEASE_SCRIPT lua_extendLUA_EXTEND_SCRIPT lua_reacquireLUA_REACQUIRE_SCRIPT)rclsclients rrzLock.register_scriptssnn ?? "$44S5K5KLCO >> !#33C4I4IJCN    $ & 6 6s7O7O PC  %rcXK|jd{r|Std7w)Nz0Unable to acquire lock within the time specified)acquirerrs r __aenter__zLock.__aenter__s+  KJKK s *(*c@K|jd{y7wr)release)rexc_type exc_value tracebacks r __aexit__zLock.__aexit__sllns rcKtjdddk7rtj}ntj}|j }|-t jjj}n5|jjj}|j|}| |j}| |j}d}||j|z} |j!|d{r||j"_y|sy|j|z}|||kDrytj |d{g7R7w)as Use Redis to hold a shared, distributed lock named ``name``. Returns True once the lock is acquired. If ``blocking`` is False, always return immediately. If the lock was acquired, return True, otherwise return False. ``blocking_timeout`` specifies the maximum number of seconds to wait trying to acquire the lock. ``token`` specifies the token value to be used. If provided, token must be a bytes object or a string that can be encoded to a bytes object with the default encoding. If a token isn't specified, a UUID will be generated. r)NTF)sys version_infoasyncioget_running_loopget_event_loopruuiduuid1hexencoder connection_pool get_encoderrrtime do_acquirerr) rrrrlooprencoderstop_trying_at next_try_ats rr+z Lock.acquires.*   Aa F *++-D))+D  =JJL$$++-Ejj00<<>GNN5)E  }}H  ##44   '!YY[+;;N__U+++#(  ))+-K)kN.J--& & &+ 's%C8E:E ;A EEEEreturncK|jrt|jdz}nd}|jj|j|d|d{ryy7w)NT)nxpxF)rintr setr)rrrs rrDzLock.do_acquiresS <<$,,-.GG 5TgF F F GsAAAAclK|jj|jd{duS7w)zU Returns True if this key is locked by any process, otherwise False. N)r getrr,s rlockedz Lock.lockeds*ZZ^^DII..d::.s )424c\K|jj|jd{}|rEt|ts5|jj j }|j|}|jjduxr||jjk(S7~w)zS Returns True if this key is locked by this lock, otherwise False. N) r rQr isinstancebytesrArBr@rr)r stored_tokenrFs rownedz Lock.owneds"ZZ^^DII66   < ?jj00<<>G">>,7Lzzt+P  @P@P0PP 7s)B,B*A?B,c|jj}| tdd|j_|j|S)z"Releases the already acquired lockNzCannot release an unlocked lock)rrr do_releaserexpected_tokens rr/z Lock.releases>))  !=> > ~..rr[cKt|j|jg|g|jd{s t dy7w)Nkeysargsr)z,Cannot release a lock that's no longer owned)rr!rr r rZs rrYzLock.do_releasesR""ii['7 #  $$UV V   s3A A A additional_time replace_ttlc|jj td|j td|j ||S)ah Adds more time to an already acquired lock. ``additional_time`` can be specified as an integer or a float, both representing the number of seconds to add. ``replace_ttl`` if False (the default), add `additional_time` to the lock's existing ttl. If True, replace the lock's ttl with `additional_time`. zCannot extend an unlocked lockz$Cannot extend a lock with no timeout)rrrr do_extendrr`ras rextendz Lock.extendsG ::   #<= = << BC C~~o{;;rc Kt|dz}t|j|jg|jj ||xrdxsdg|j d{s tdy7w)NrK10r]z+Cannot extend a lock that's no longer ownedT)rNrr$rrrr r rds rrczLock.do_extendsxo45//ii[jj&&9L9SPSTzz"  $$TU U sAA8!A6"A8c|jj td|j td|j S)zS Resets a TTL of an already acquired lock back to a timeout value. z!Cannot reacquire an unlocked lockz'Cannot reacquire a lock with no timeout)rrrr do_reacquirer,s r reacquirezLock.reacquire%sE ::   #?@ @ << EF F  ""rcKt|jdz}t|j|jg|j j |g|jd{s tdy7w)NrKr]z.Cannot reacquire a lock that's no longer ownedT) rNrrr&rrrr r )rrs rrjzLock.do_reacquire/sodllT)*$$ii[ (8(8''B4::%  $$WX X  sA A9"A7#A9)Ng?TNT)NNN)F) __name__ __module__ __qualname____doc__r!r$r&r#r%r'rstrrU memoryviewrfloatrrrr-r3r+rDrRrWrr/rYrercrkrjrrr r sKJM 2$(,0!E E C *+E % E  E  E #5/E E NQL  $(,0-1 0'4.0'#5/0'c5j)* 0'deCJ&7D;d; QT Q/4/WuWW;@<$<37< 4<& t #9T?#Drr )r:r8rr=typesrtypingrrrrredis.exceptionsrr redis.asyncior r rtrrrys/  !<<9#iir