Metadata-Version: 2.4
Name: mithril-locks
Version: 0.1.0
Summary: Distributed locks with fencing tokens, sync and async APIs, and multiple backends
Project-URL: Source, https://github.com/RohanDalton/mithril
Project-URL: Issues, https://github.com/RohanDalton/mithril/issues
Author-email: "Rohan B. Dalton" <rohan@rohandalton.io>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: asyncio,distributed-lock,fencing,locking,redis
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Requires-Dist: opentelemetry-api<2,>=1.20.0
Requires-Dist: redis<8,>=5.0
Requires-Dist: sqlalchemy[asyncio]<3,>=2.0
Requires-Dist: structlog<26,>=24.0.0
Provides-Extra: async-postgres
Requires-Dist: asyncpg<1,>=0.29; extra == 'async-postgres'
Provides-Extra: async-sqlite
Requires-Dist: aiosqlite<1,>=0.20; extra == 'async-sqlite'
Provides-Extra: postgres
Requires-Dist: psycopg2-binary<3,>=2.9; extra == 'postgres'
Description-Content-Type: text/markdown

# Mithril

Mithril provides distributed locks with fencing tokens, ownership checks, TTLs,
and opt-in automatic renewal. It includes synchronous and asynchronous APIs,
Redis, Redis Cluster, Redlock, PostgreSQL, SQLite, and local file backends.

## Installation

```sh
python -m pip install mithril-locks
```

The distribution is named `mithril-locks`; Python imports use `mithril`.
Python 3.9 or newer is required. Database drivers are available as extras:

```sh
python -m pip install 'mithril-locks[postgres]'
python -m pip install 'mithril-locks[async-postgres]'
python -m pip install 'mithril-locks[async-sqlite]'
```

## Usage

```python
from redis import Redis

from mithril import Lock
from mithril.backends import RedisBackend

client = Redis.from_url(
    "redis://localhost:6379/0", socket_timeout=5, socket_connect_timeout=5
)
backend = RedisBackend(client)

with Lock("payment", backend=backend, ttl=30, timeout=10) as token:
    print(token.fencing_token)
    # Pass the fencing token to your protected resource with each write.
```

Context managers release the lock on exit and return a `LockToken`. Lock objects
are single-use. For work that may exceed the TTL, pass `auto_renewal=True`.

The async API uses `mithril.aio.AsyncLock`,
`mithril.backends.aio.AsyncRedisBackend`, and a `redis.asyncio.Redis` client:

```python
import asyncio

from redis.asyncio import Redis

from mithril.aio import AsyncLock
from mithril.backends.aio import AsyncRedisBackend


async def main():
    async with Redis.from_url(
        "redis://localhost:6379/0", socket_timeout=5, socket_connect_timeout=5
    ) as client:
        backend = AsyncRedisBackend(client)
        async with AsyncLock("payment", backend=backend, ttl=30) as token:
            print(token.fencing_token)


asyncio.run(main())
```

## Fencing and durability

Your downstream resource must atomically reject writes carrying a fencing token
older than the highest token it has accepted. A TTL alone cannot stop a delayed
holder from writing after its lease expires. Fencing counters must be durable:
Redis failover, eviction, or restoration of an older snapshot can invalidate
ordering guarantees. Redlock requires an explicit shared fencing authority.
File locks are for processes on the same host.

## License

Apache License 2.0. See [LICENSE](LICENSE).
