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.2.tar.gz (22.8 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.2-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

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

Hashes for gcslock-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4abbe57cfb10f40ca4ae7cffe4345f3fe3417be19ec0aa0c32d2fba73dbe02f9
MD5 ef849edb8362990d02c255a9e9d7b928
BLAKE2b-256 81ac8a943e5b6d824303795856f1ce91440f2ea22e3bb1fa6e3ee596ff3963e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcslock-0.0.2.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.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

Hashes for gcslock-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 314cafb6f96de2e8d050df87a3cf6e23e2dc0557aed16ee87b98e6c44e9cc001
MD5 cb7d7d665e768bfb83f7ff3cce8b9959
BLAKE2b-256 a94978295f8f85be6b1ae2847d4548a274649fbf129a0953e89c01ad7dd8006c

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcslock-0.0.2-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