Skip to main content

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)

  1. 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.
  2. 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.
  3. 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.
  4. 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

gcslock-0.0.1.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gcslock-0.0.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file gcslock-0.0.1.tar.gz.

File metadata

  • Download URL: gcslock-0.0.1.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for gcslock-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c0afd3ca4c8640afa7304cdab9d84f6816fdf0ad7683f26a803f097d49e0d0f8
MD5 a2fbed6c39464268fbe5fcdcc8bb569c
BLAKE2b-256 ece9e8d8ba4c49ed0be872a5e057441cd0ebcb3c072ec8190302a01b9df36aa7

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcslock-0.0.1.tar.gz:

Publisher: publish-pypi.yml on minthem/gcs-lock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gcslock-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: gcslock-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for gcslock-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bf6442deb0ec6d614528830a1583006b60e26d18ed03cc18aacd8819d38ca392
MD5 6c86d9bddded2f416aef02e0e11977c6
BLAKE2b-256 52c6094d21a989f7b70a4cda505f63cd6739a88cfe45fcfa4e365652ed7b6c87

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcslock-0.0.1-py3-none-any.whl:

Publisher: publish-pypi.yml on minthem/gcs-lock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page