mithril-locks 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mithril_locks-0.1.0/.gitignore +16 -0
- mithril_locks-0.1.0/LICENSE +202 -0
- mithril_locks-0.1.0/PKG-INFO +104 -0
- mithril_locks-0.1.0/README.md +78 -0
- mithril_locks-0.1.0/mithril/UPGRADING.md +86 -0
- mithril_locks-0.1.0/mithril/__init__.py +67 -0
- mithril_locks-0.1.0/mithril/__version__.py +3 -0
- mithril_locks-0.1.0/mithril/_fencing.py +164 -0
- mithril_locks-0.1.0/mithril/_file_io.py +117 -0
- mithril_locks-0.1.0/mithril/_observability.py +168 -0
- mithril_locks-0.1.0/mithril/_renewal.py +115 -0
- mithril_locks-0.1.0/mithril/_retry.py +94 -0
- mithril_locks-0.1.0/mithril/_types.py +197 -0
- mithril_locks-0.1.0/mithril/aio/__init__.py +12 -0
- mithril_locks-0.1.0/mithril/aio/_renewal.py +126 -0
- mithril_locks-0.1.0/mithril/aio/_retry.py +83 -0
- mithril_locks-0.1.0/mithril/aio/lock.py +571 -0
- mithril_locks-0.1.0/mithril/backends/__init__.py +17 -0
- mithril_locks-0.1.0/mithril/backends/_database.py +192 -0
- mithril_locks-0.1.0/mithril/backends/_file.py +245 -0
- mithril_locks-0.1.0/mithril/backends/_postgres.py +332 -0
- mithril_locks-0.1.0/mithril/backends/_redis.py +162 -0
- mithril_locks-0.1.0/mithril/backends/_redis_cluster.py +167 -0
- mithril_locks-0.1.0/mithril/backends/_redis_common.py +53 -0
- mithril_locks-0.1.0/mithril/backends/_redis_scripts.py +141 -0
- mithril_locks-0.1.0/mithril/backends/_redlock.py +248 -0
- mithril_locks-0.1.0/mithril/backends/_sqlite.py +305 -0
- mithril_locks-0.1.0/mithril/backends/aio/__init__.py +8 -0
- mithril_locks-0.1.0/mithril/backends/aio/_database.py +166 -0
- mithril_locks-0.1.0/mithril/backends/aio/_postgres.py +144 -0
- mithril_locks-0.1.0/mithril/backends/aio/_redis.py +169 -0
- mithril_locks-0.1.0/mithril/backends/aio/_sqlite.py +156 -0
- mithril_locks-0.1.0/mithril/exceptions.py +126 -0
- mithril_locks-0.1.0/mithril/lock.py +548 -0
- mithril_locks-0.1.0/mithril/py.typed +0 -0
- mithril_locks-0.1.0/pyproject.toml +129 -0
- mithril_locks-0.1.0/tests/__init__.py +4 -0
- mithril_locks-0.1.0/tests/aio/__init__.py +4 -0
- mithril_locks-0.1.0/tests/aio/test_aio_lock.py +336 -0
- mithril_locks-0.1.0/tests/aio/test_aio_lock_safety.py +147 -0
- mithril_locks-0.1.0/tests/aio/test_aio_renewal.py +123 -0
- mithril_locks-0.1.0/tests/aio/test_aio_retry.py +72 -0
- mithril_locks-0.1.0/tests/backends/__init__.py +4 -0
- mithril_locks-0.1.0/tests/backends/aio/__init__.py +4 -0
- mithril_locks-0.1.0/tests/backends/aio/test_async_postgres_backend.py +325 -0
- mithril_locks-0.1.0/tests/backends/aio/test_async_redis_backend.py +47 -0
- mithril_locks-0.1.0/tests/backends/aio/test_async_sqlite_backend.py +366 -0
- mithril_locks-0.1.0/tests/backends/test_file_backend.py +416 -0
- mithril_locks-0.1.0/tests/backends/test_file_safety.py +171 -0
- mithril_locks-0.1.0/tests/backends/test_postgres_backend.py +408 -0
- mithril_locks-0.1.0/tests/backends/test_redis_backend.py +159 -0
- mithril_locks-0.1.0/tests/backends/test_redis_cluster_backend.py +179 -0
- mithril_locks-0.1.0/tests/backends/test_redis_safety.py +133 -0
- mithril_locks-0.1.0/tests/backends/test_redlock_backend.py +464 -0
- mithril_locks-0.1.0/tests/backends/test_sqlite_backend.py +323 -0
- mithril_locks-0.1.0/tests/test_exceptions.py +30 -0
- mithril_locks-0.1.0/tests/test_fencing.py +109 -0
- mithril_locks-0.1.0/tests/test_lock.py +389 -0
- mithril_locks-0.1.0/tests/test_lock_safety.py +260 -0
- mithril_locks-0.1.0/tests/test_observability.py +134 -0
- mithril_locks-0.1.0/tests/test_renewal.py +180 -0
- mithril_locks-0.1.0/tests/test_retry.py +125 -0
- mithril_locks-0.1.0/tests/test_types.py +72 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mithril-locks
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Distributed locks with fencing tokens, sync and async APIs, and multiple backends
|
|
5
|
+
Project-URL: Source, https://github.com/RohanDalton/mithril
|
|
6
|
+
Project-URL: Issues, https://github.com/RohanDalton/mithril/issues
|
|
7
|
+
Author-email: "Rohan B. Dalton" <rohan@rohandalton.io>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: asyncio,distributed-lock,fencing,locking,redis
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Requires-Dist: opentelemetry-api<2,>=1.20.0
|
|
16
|
+
Requires-Dist: redis<8,>=5.0
|
|
17
|
+
Requires-Dist: sqlalchemy[asyncio]<3,>=2.0
|
|
18
|
+
Requires-Dist: structlog<26,>=24.0.0
|
|
19
|
+
Provides-Extra: async-postgres
|
|
20
|
+
Requires-Dist: asyncpg<1,>=0.29; extra == 'async-postgres'
|
|
21
|
+
Provides-Extra: async-sqlite
|
|
22
|
+
Requires-Dist: aiosqlite<1,>=0.20; extra == 'async-sqlite'
|
|
23
|
+
Provides-Extra: postgres
|
|
24
|
+
Requires-Dist: psycopg2-binary<3,>=2.9; extra == 'postgres'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Mithril
|
|
28
|
+
|
|
29
|
+
Mithril provides distributed locks with fencing tokens, ownership checks, TTLs,
|
|
30
|
+
and opt-in automatic renewal. It includes synchronous and asynchronous APIs,
|
|
31
|
+
Redis, Redis Cluster, Redlock, PostgreSQL, SQLite, and local file backends.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
python -m pip install mithril-locks
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The distribution is named `mithril-locks`; Python imports use `mithril`.
|
|
40
|
+
Python 3.9 or newer is required. Database drivers are available as extras:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
python -m pip install 'mithril-locks[postgres]'
|
|
44
|
+
python -m pip install 'mithril-locks[async-postgres]'
|
|
45
|
+
python -m pip install 'mithril-locks[async-sqlite]'
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from redis import Redis
|
|
52
|
+
|
|
53
|
+
from mithril import Lock
|
|
54
|
+
from mithril.backends import RedisBackend
|
|
55
|
+
|
|
56
|
+
client = Redis.from_url(
|
|
57
|
+
"redis://localhost:6379/0", socket_timeout=5, socket_connect_timeout=5
|
|
58
|
+
)
|
|
59
|
+
backend = RedisBackend(client)
|
|
60
|
+
|
|
61
|
+
with Lock("payment", backend=backend, ttl=30, timeout=10) as token:
|
|
62
|
+
print(token.fencing_token)
|
|
63
|
+
# Pass the fencing token to your protected resource with each write.
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Context managers release the lock on exit and return a `LockToken`. Lock objects
|
|
67
|
+
are single-use. For work that may exceed the TTL, pass `auto_renewal=True`.
|
|
68
|
+
|
|
69
|
+
The async API uses `mithril.aio.AsyncLock`,
|
|
70
|
+
`mithril.backends.aio.AsyncRedisBackend`, and a `redis.asyncio.Redis` client:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import asyncio
|
|
74
|
+
|
|
75
|
+
from redis.asyncio import Redis
|
|
76
|
+
|
|
77
|
+
from mithril.aio import AsyncLock
|
|
78
|
+
from mithril.backends.aio import AsyncRedisBackend
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
async def main():
|
|
82
|
+
async with Redis.from_url(
|
|
83
|
+
"redis://localhost:6379/0", socket_timeout=5, socket_connect_timeout=5
|
|
84
|
+
) as client:
|
|
85
|
+
backend = AsyncRedisBackend(client)
|
|
86
|
+
async with AsyncLock("payment", backend=backend, ttl=30) as token:
|
|
87
|
+
print(token.fencing_token)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
asyncio.run(main())
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Fencing and durability
|
|
94
|
+
|
|
95
|
+
Your downstream resource must atomically reject writes carrying a fencing token
|
|
96
|
+
older than the highest token it has accepted. A TTL alone cannot stop a delayed
|
|
97
|
+
holder from writing after its lease expires. Fencing counters must be durable:
|
|
98
|
+
Redis failover, eviction, or restoration of an older snapshot can invalidate
|
|
99
|
+
ordering guarantees. Redlock requires an explicit shared fencing authority.
|
|
100
|
+
File locks are for processes on the same host.
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
Apache License 2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Mithril
|
|
2
|
+
|
|
3
|
+
Mithril provides distributed locks with fencing tokens, ownership checks, TTLs,
|
|
4
|
+
and opt-in automatic renewal. It includes synchronous and asynchronous APIs,
|
|
5
|
+
Redis, Redis Cluster, Redlock, PostgreSQL, SQLite, and local file backends.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
python -m pip install mithril-locks
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The distribution is named `mithril-locks`; Python imports use `mithril`.
|
|
14
|
+
Python 3.9 or newer is required. Database drivers are available as extras:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
python -m pip install 'mithril-locks[postgres]'
|
|
18
|
+
python -m pip install 'mithril-locks[async-postgres]'
|
|
19
|
+
python -m pip install 'mithril-locks[async-sqlite]'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from redis import Redis
|
|
26
|
+
|
|
27
|
+
from mithril import Lock
|
|
28
|
+
from mithril.backends import RedisBackend
|
|
29
|
+
|
|
30
|
+
client = Redis.from_url(
|
|
31
|
+
"redis://localhost:6379/0", socket_timeout=5, socket_connect_timeout=5
|
|
32
|
+
)
|
|
33
|
+
backend = RedisBackend(client)
|
|
34
|
+
|
|
35
|
+
with Lock("payment", backend=backend, ttl=30, timeout=10) as token:
|
|
36
|
+
print(token.fencing_token)
|
|
37
|
+
# Pass the fencing token to your protected resource with each write.
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Context managers release the lock on exit and return a `LockToken`. Lock objects
|
|
41
|
+
are single-use. For work that may exceed the TTL, pass `auto_renewal=True`.
|
|
42
|
+
|
|
43
|
+
The async API uses `mithril.aio.AsyncLock`,
|
|
44
|
+
`mithril.backends.aio.AsyncRedisBackend`, and a `redis.asyncio.Redis` client:
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import asyncio
|
|
48
|
+
|
|
49
|
+
from redis.asyncio import Redis
|
|
50
|
+
|
|
51
|
+
from mithril.aio import AsyncLock
|
|
52
|
+
from mithril.backends.aio import AsyncRedisBackend
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
async def main():
|
|
56
|
+
async with Redis.from_url(
|
|
57
|
+
"redis://localhost:6379/0", socket_timeout=5, socket_connect_timeout=5
|
|
58
|
+
) as client:
|
|
59
|
+
backend = AsyncRedisBackend(client)
|
|
60
|
+
async with AsyncLock("payment", backend=backend, ttl=30) as token:
|
|
61
|
+
print(token.fencing_token)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
asyncio.run(main())
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Fencing and durability
|
|
68
|
+
|
|
69
|
+
Your downstream resource must atomically reject writes carrying a fencing token
|
|
70
|
+
older than the highest token it has accepted. A TTL alone cannot stop a delayed
|
|
71
|
+
holder from writing after its lease expires. Fencing counters must be durable:
|
|
72
|
+
Redis failover, eviction, or restoration of an older snapshot can invalidate
|
|
73
|
+
ordering guarantees. Redlock requires an explicit shared fencing authority.
|
|
74
|
+
File locks are for processes on the same host.
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
Apache License 2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Reliability changes
|
|
2
|
+
|
|
3
|
+
## Before upgrading an existing deployment
|
|
4
|
+
|
|
5
|
+
Stop all old-version lock users and drain their leases before starting the new
|
|
6
|
+
version. Redis reader metadata and file lease metadata have changed; old and new
|
|
7
|
+
holders must not run concurrently against the same resources.
|
|
8
|
+
|
|
9
|
+
Preserve fencing history. Back up existing counters and remove their legacy Redis
|
|
10
|
+
expirations with `PERSIST` during the maintenance window. Names and namespaces
|
|
11
|
+
containing punctuation are now percent-encoded in Redis keys, so migrate those
|
|
12
|
+
counters to the encoded keys without lowering their values. Cluster hash tags use
|
|
13
|
+
the same encoding. Ordinary alphanumeric Redis fencing keys and file fencing
|
|
14
|
+
counter paths are unchanged. While all file-lock users are stopped, remove their
|
|
15
|
+
old `.lock` metadata files so the new format can be created. Keep the adjacent
|
|
16
|
+
`.fence` counters and do not delete live `.guard` files.
|
|
17
|
+
|
|
18
|
+
If an old counter has already expired, recover a floor from durable history or
|
|
19
|
+
the downstream system's highest accepted token before resuming. Draining holders
|
|
20
|
+
alone does not make resetting a fencing counter safe.
|
|
21
|
+
|
|
22
|
+
## Redlock requires a fencing authority
|
|
23
|
+
|
|
24
|
+
`RedlockConfig` now requires `fencing_provider`. Taking the maximum of independent
|
|
25
|
+
node counters cannot order acquisitions across changing quorums. All clients
|
|
26
|
+
locking the same resource must share the same persistent authority and namespace.
|
|
27
|
+
Seed that authority at least as high as every previously issued or downstream-
|
|
28
|
+
accepted token before migrating an existing Redlock deployment.
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from redis import Redis
|
|
32
|
+
|
|
33
|
+
from mithril import RedisFencingTokenProvider
|
|
34
|
+
from mithril.backends import RedlockBackend, RedlockConfig
|
|
35
|
+
|
|
36
|
+
authority = Redis(
|
|
37
|
+
host="fencing-service",
|
|
38
|
+
socket_timeout=0.1,
|
|
39
|
+
socket_connect_timeout=0.1,
|
|
40
|
+
)
|
|
41
|
+
backend = RedlockBackend(
|
|
42
|
+
RedlockConfig(
|
|
43
|
+
instances=[Redis(host=host) for host in ("redis-a", "redis-b", "redis-c")],
|
|
44
|
+
fencing_provider=RedisFencingTokenProvider(authority),
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The authority must never roll back, expire, or evict issued counters. A Redis
|
|
50
|
+
authority is only as durable as its deployment: asynchronous failover or restoring
|
|
51
|
+
an old snapshot can violate this requirement. Supply a custom `FencingTokenProvider`
|
|
52
|
+
backed by a suitable durable service when needed. The in-memory provider is for
|
|
53
|
+
isolated tests only. Call `backend.close()` when finished to close its private
|
|
54
|
+
bounded-timeout Redis pools; caller-owned clients are not closed.
|
|
55
|
+
|
|
56
|
+
## Database backends
|
|
57
|
+
|
|
58
|
+
An SQLAlchemy `Engine` / `AsyncEngine` is now accepted and recommended. Existing
|
|
59
|
+
`Connection` / `AsyncConnection` arguments still work: Mithril borrows independent
|
|
60
|
+
connections from their engine rather than sharing the caller's transaction.
|
|
61
|
+
Configure bounded pool and driver timeouts appropriate for the lease TTL. Retry
|
|
62
|
+
budgets cannot interrupt synchronous driver calls already in progress.
|
|
63
|
+
|
|
64
|
+
SQLite requires a file-backed database with an independent-connection pool;
|
|
65
|
+
`StaticPool` and `SingletonThreadPool` are rejected. In-memory databases cannot
|
|
66
|
+
provide persistent fencing or safely shared transactions. Async backends still
|
|
67
|
+
require `await backend.initialize()` before use.
|
|
68
|
+
|
|
69
|
+
## Lifecycle behavior
|
|
70
|
+
|
|
71
|
+
Nonblocking acquisition makes one attempt. Concurrent acquisition through the
|
|
72
|
+
same facade is rejected. Expired or released instances remain single-use, expose
|
|
73
|
+
no current token, and context exit raises on ownership loss unless preserving an
|
|
74
|
+
exception from the context body. Renewals refresh the facade's deadline; backend
|
|
75
|
+
latency and Redlock drift are subtracted from validity. A manually shortened TTL
|
|
76
|
+
must still exceed the configured background-renewal interval, including jitter.
|
|
77
|
+
|
|
78
|
+
File leases now allow takeover after TTL expiry even while the previous process
|
|
79
|
+
is alive, and detect process crashes using OS locks. As with every TTL-based
|
|
80
|
+
backend, downstream writes must enforce fencing tokens. A standalone guard check
|
|
81
|
+
followed by an unrelated write is not an atomic fenced write.
|
|
82
|
+
|
|
83
|
+
Logging and metrics failures no longer interrupt lease ownership or cleanup.
|
|
84
|
+
Epochs are backend-issued persistent sequence numbers, not caller-supplied counts.
|
|
85
|
+
The backend `epoch` argument remains accepted for compatibility but is ignored by
|
|
86
|
+
the built-in backends.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Public API for Mithril.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from .__version__ import __version__
|
|
8
|
+
from ._fencing import FencingTokenProvider, FileFencingTokenProvider, RedisFencingTokenProvider
|
|
9
|
+
from ._observability import LockHooks, configure_logging, get_logger, set_hooks
|
|
10
|
+
from ._types import LockConfig, LockMode, LockState, LockToken, Result
|
|
11
|
+
from .exceptions import (
|
|
12
|
+
AlreadyAcquiredError,
|
|
13
|
+
AutoRenewalStoppedError,
|
|
14
|
+
BackendError,
|
|
15
|
+
BackendTimeoutError,
|
|
16
|
+
BackendUnavailableError,
|
|
17
|
+
ConfigurationError,
|
|
18
|
+
FencingTokenError,
|
|
19
|
+
LockAcquisitionError,
|
|
20
|
+
LockExpiredError,
|
|
21
|
+
LockReleaseError,
|
|
22
|
+
LockRenewalError,
|
|
23
|
+
LockTimeoutError,
|
|
24
|
+
MithrilError,
|
|
25
|
+
NotAcquiredError,
|
|
26
|
+
RenewalFailedError,
|
|
27
|
+
StaleTokenError,
|
|
28
|
+
)
|
|
29
|
+
from .lock import Lock
|
|
30
|
+
|
|
31
|
+
__author__ = "Rohan B. Dalton"
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"__version__",
|
|
35
|
+
"FencingTokenProvider",
|
|
36
|
+
"FileFencingTokenProvider",
|
|
37
|
+
"RedisFencingTokenProvider",
|
|
38
|
+
"LockConfig",
|
|
39
|
+
"LockMode",
|
|
40
|
+
"LockState",
|
|
41
|
+
"LockToken",
|
|
42
|
+
"Result",
|
|
43
|
+
"Lock",
|
|
44
|
+
"LockHooks",
|
|
45
|
+
"configure_logging",
|
|
46
|
+
"get_logger",
|
|
47
|
+
"set_hooks",
|
|
48
|
+
"MithrilError",
|
|
49
|
+
"LockAcquisitionError",
|
|
50
|
+
"LockTimeoutError",
|
|
51
|
+
"AlreadyAcquiredError",
|
|
52
|
+
"LockReleaseError",
|
|
53
|
+
"NotAcquiredError",
|
|
54
|
+
"LockExpiredError",
|
|
55
|
+
"LockRenewalError",
|
|
56
|
+
"RenewalFailedError",
|
|
57
|
+
"AutoRenewalStoppedError",
|
|
58
|
+
"FencingTokenError",
|
|
59
|
+
"StaleTokenError",
|
|
60
|
+
"BackendError",
|
|
61
|
+
"BackendUnavailableError",
|
|
62
|
+
"BackendTimeoutError",
|
|
63
|
+
"ConfigurationError",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
if __name__ == "__main__":
|
|
67
|
+
pass
|