Add your description here
Project description
gcs-atomic-lock
Google Cloud Storage (GCS) object generations and metagenerations are used to implement a lightweight distributed lock with strong, atomic semantics. This library wraps the key conditional operations and provides a simple Python API (context manager) to acquire and release a lock stored as a GCS object.
- Language: Python 3.10
- Package manager: uv
- Status: Library + unit tests
- License: See LICENSE
Why use this?
- Atomic lock acquisition via GCS conditional writes (
ifGenerationMatch=0) - Lock extension/update using metageneration preconditions (
ifMetagenerationMatch) - Simple Pythonic API (with-context)
- Pluggable access layer for HTTP/credential handling
- Helpful exceptions and logging
- Optional in-memory TTL map to minimize redundant work
How it works (high level)
-
Check lock object
- GET the lock object in the bucket.
- If not found → lock appears free (or expired; see metadata).
- If found → read
generation,metageneration,metadata.
-
Try to acquire (create) the lock
- PUT (or upload) with
ifGenerationMatch=0. - If the object already exists, GCS returns 412 Precondition Failed → lock acquisition fails.
- If created (200 OK), you hold the lock.
- PUT (or upload) with
-
Update metadata (optional/renew)
- PATCH the object with
ifMetagenerationMatch=<current>. - If someone else updated it in between, 412 is returned → treat as contention.
- On success (200 OK) → your lock metadata (e.g., owner, expiry) is updated.
- PATCH the object with
-
Release the lock
- DELETE the object to unlock (204 No Content on success).
- Optionally validate ownership based on stored metadata before deleting.
Flow
flowchart
A[Start] --> B{GET Bucket}
B -- Not Found --> Z[Bucket Not Found Error]
B -- Found --> C{GET Object}
C -- Not Found --> D[PUT with ifGenerationMatch=0]
D -- 412 --> E[LOCK FAILED]
D -- 200 --> F[LOCK SUCCESS]
C -- Found --> G[PATCH with ifMetagenerationMatch=CurrentValue]
G -- 412 --> E
G -- 200 --> F
F --> H[DELETE Object to Unlock]
H -- 204 --> I[LOCK RELEASED]
Installation
pip install gcslock
Example
from gcslock import GcsLock, LockState
from gcslock.exception import GcsLockError
BUCKET = "your-bucket-name"
OBJECT = "locks/my-resource.lock"
OWNER = "your-owner-id"
LOCK_EXPIRES_SEC = 60
lock = GcsLock(BUCKET, OBJECT, lock_owner=OWNER)
try:
with lock.acquire(lock_id=OBJECT, expires_seconds=LOCK_EXPIRES_SEC) as lock:
# Critical section
...
except GcsLockError as e:
print(f"Lock error: {e}")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gcslock-0.0.2.tar.gz.
File metadata
- Download URL: gcslock-0.0.2.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4abbe57cfb10f40ca4ae7cffe4345f3fe3417be19ec0aa0c32d2fba73dbe02f9
|
|
| MD5 |
ef849edb8362990d02c255a9e9d7b928
|
|
| BLAKE2b-256 |
81ac8a943e5b6d824303795856f1ce91440f2ea22e3bb1fa6e3ee596ff3963e8
|
Provenance
The following attestation bundles were made for gcslock-0.0.2.tar.gz:
Publisher:
publish-pypi.yml on minthem/gcs-lock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gcslock-0.0.2.tar.gz -
Subject digest:
4abbe57cfb10f40ca4ae7cffe4345f3fe3417be19ec0aa0c32d2fba73dbe02f9 - Sigstore transparency entry: 404395470
- Sigstore integration time:
-
Permalink:
minthem/gcs-lock@0325856c460c16e62ced38375c909cd4786375ed -
Branch / Tag:
refs/tags/0.0.2 - Owner: https://github.com/minthem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@0325856c460c16e62ced38375c909cd4786375ed -
Trigger Event:
push
-
Statement type:
File details
Details for the file gcslock-0.0.2-py3-none-any.whl.
File metadata
- Download URL: gcslock-0.0.2-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
314cafb6f96de2e8d050df87a3cf6e23e2dc0557aed16ee87b98e6c44e9cc001
|
|
| MD5 |
cb7d7d665e768bfb83f7ff3cce8b9959
|
|
| BLAKE2b-256 |
a94978295f8f85be6b1ae2847d4548a274649fbf129a0953e89c01ad7dd8006c
|
Provenance
The following attestation bundles were made for gcslock-0.0.2-py3-none-any.whl:
Publisher:
publish-pypi.yml on minthem/gcs-lock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gcslock-0.0.2-py3-none-any.whl -
Subject digest:
314cafb6f96de2e8d050df87a3cf6e23e2dc0557aed16ee87b98e6c44e9cc001 - Sigstore transparency entry: 404395474
- Sigstore integration time:
-
Permalink:
minthem/gcs-lock@0325856c460c16e62ced38375c909cd4786375ed -
Branch / Tag:
refs/tags/0.0.2 - Owner: https://github.com/minthem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@0325856c460c16e62ced38375c909cd4786375ed -
Trigger Event:
push
-
Statement type: