PALs 0.4.0
Easy distributed locking using PostgreSQL Advisory Locks.
License: BSD-3-Clause

.. default-role:: code

PostgreSQL Advisory Locks (PALs)
################################

.. image:: https://circleci.com/gh/level12/pals.svg?style=shield
    :target: https://circleci.com/gh/level12/pals
.. image:: https://codecov.io/gh/level12/pals/branch/master/graph/badge.svg
    :target: https://codecov.io/gh/level12/pals


Introduction
============

PALs makes it easy to use `PostgreSQL Advisory Locks`_ to do distributed application level
locking.

Do not confuse this type of locking with table or row locking in PostgreSQL.  It's not the same
thing.

Distributed application level locking can be implemented by using Redis, Memcache, ZeroMQ and
others.  But for those who are already using PostgreSQL, setup & management of another service is
unnecessary.

.. _PostgreSQL Advisory Locks: https://www.postgresql.org/docs/current/static/explicit-locking.html#ADVISORY-LOCKS


Usage
========

Install with::

    pip install PALs

Then usage is as follows:

.. code:: python

    import datetime as dt
    import pals

    # Think of the Locker instance as a Lock factory.
    locker = pals.Locker('my-app-name', 'postgresql://user:pass@server/dbname')

    lock1 = locker.lock('my-lock')
    lock2 = locker.lock('my-lock')

    # The first acquire works
    assert lock1.acquire() is True

    # Non blocking version should fail immediately
    assert lock2.acquire(blocking=False) is False

    # Blocking version should fail after a short time
    start = dt.datetime.now()
    acquired = lock2.acquire(acquire_timeout=300)
    waited_ms = duration(start)

    assert acquired is False
    assert waited_ms >= 300 and waited_ms < 350

    # Release the lock
    lock1.release()

    # Non-blocking usage pattern
    if not lock1.acquire(blocking=False):
        # Aquire returned False, indicating we did not get the lock.
        return
    try:
        # do your work here
    finally:
        lock1.release()

    # If you want to block, you can use a context manager:
    try:
        with lock1:
            # Do your work here
            pass
    except pals.AcquireFailure:
        # This indicates the aquire_timeout was reached before the lock could be aquired.
        pass

Docs
========

Just this readme, the code, and tests.  It a small project, should be easy to understand.

Feel free to open an issue with questions.

Running Tests Locally
=====================

Setup Database Connection
-------------------------

We have provided a docker-compose file to ease running the tests::

    $ docker-compose up -d
    $ export PALS_DB_URL=postgresql://postgres:password@localhost:54321/postgres


Run the Tests
-------------

With tox::

    $ tox

Or, manually (assuming an activated virtualenv)::

    $ pip install -e .[tests]
    $ pytest pals/tests/


Lock Releasing & Expiration
---------------------------

Unlike locking systems built on cache services like Memcache and Redis, whose keys can be expired
by the service, there is no faculty for expiring an advisory lock in PostgreSQL.  If a client
holds a lock and then sleeps/hangs for mins/hours/days, no other client will be able to get that
lock until the client releases it.  This actually seems like a good thing to us, if a lock is
acquired, it should be kept until released.

But what about accidental failures to release the lock?

1. If a developer uses `lock.acquire()` but doesn't later call `lock.release()`?
2. If code inside a lock accidentally throws an exception (and .release() is not called)?
3. If the process running the application crashes or the process' server dies?

PALs helps #1 and #2 above in a few different ways:

* Locks work as context managers.  Use them as much as possible to guarantee a lock is released.
* Locks release their lock when garbage collected.
* PALs uses a dedicated SQLAlchemy connection pool.  When a connection is returned to the pool,
  either because a connection `.close()` is called or due to garbage collection of the connection,
  PALs issues a `pg_advisory_unlock_all()`.  It should therefore be impossible for an idle
  connection in the pool to ever still be holding a lock.

Regarding #3 above, `pg_advisory_unlock_all()` is implicitly invoked by PostgreSQL whenever a
connection (a.k.a session) ends, even if the client disconnects ungracefully.  So if a process
crashes or otherwise disappears, PostgreSQL should notice and remove all locks held by that
connection/session.

The possibility could exist that PostgreSQL does not detect a connection has closed and keeps
a lock open indefinitely.  However, in manual testing using `scripts/hang.py` no way was found
to end the Python process without PostgreSQL detecting it.


See Also
==========

* https://vladmihalcea.com/how-do-postgresql-advisory-locks-work/
* https://github.com/binded/advisory-lock
* https://github.com/vaidik/sherlock
* https://github.com/Xof/django-pglocks



Changelog
=========

0.4.0 released 2024-04-16
-------------------------

- support psycopg3, backwards compatible with psycopg2 (thanks to @petr.prikryl) (0139bea_)
- remove python 3.7 and 3.8 from CI, resolve flake8 issue (24bff1f_)

.. _0139bea: https://github.com/level12/pals/commit/0139bea
.. _24bff1f: https://github.com/level12/pals/commit/24bff1f


0.3.5 released 2023-06-29
-------------------------

- fix cursor usage after connection close (thanks to @moser) (ded88e7_)

.. _ded88e7: https://github.com/level12/pals/commit/ded88e7


0.3.4 released 2023-03-06
-------------------------

- support SQLAlchemy 2.0 (6879081_)

.. _6879081: https://github.com/level12/pals/commit/6879081


0.3.3 released 2023-01-06
-------------------------

- add additional info to AcquireFailure exception (6d81db9_)

.. _6d81db9: https://github.com/level12/pals/commit/6d81db9


0.3.2 released 2021-02-01
-------------------------

- Support shared advisory locks (thanks to @absalon-james) (ba2fe21_)

.. _ba2fe21: https://github.com/level12/pals/commit/ba2fe21


0.3.1 released 2020-09-03
-------------------------

- readme: update postgresql link (260bf75_)
- Handle case where a DB connection is returned to the pool which is already closed (5d730c9_)
- Fix a couple of typos in comments (da2b8af_)
- readme improvements (4efba90_)
- CI: fix coverage upload (52daa27_)
- Fix CI: bump CI python to v3.7 and postgres to v11 (23b3028_)

.. _260bf75: https://github.com/level12/pals/commit/260bf75
.. _5d730c9: https://github.com/level12/pals/commit/5d730c9
.. _da2b8af: https://github.com/level12/pals/commit/da2b8af
.. _4efba90: https://github.com/level12/pals/commit/4efba90
.. _52daa27: https://github.com/level12/pals/commit/52daa27
.. _23b3028: https://github.com/level12/pals/commit/23b3028


0.3.0 released 2019-11-13
-------------------------

Enhancements
~~~~~~~~~~~~

- Add acquire timeout and blocking defaults at Locker level (681c3ba_)
- Adjust default lock timeout from 1s to 30s (5a0963b_)

Project Cleanup
~~~~~~~~~~~~~~~

- adjust flake8 ignore and other tox project warning (ee123fc_)
- fix comment in test (0d8eb98_)
- Additional readme updates (0786766_)
- update locked dependencies (f5743a6_)
- Remove Python 3.5 from CI (b63c71a_)
- Cleaned up the readme code example a bit and added more references (dabb497_)
- Update setup.py to use SPDX license identifier (b811a99_)
- remove Pipefiles (0637f39_)
- move to using piptools for dependency management (af2e91f_)

.. _ee123fc: https://github.com/level12/pals/commit/ee123fc
.. _681c3ba: https://github.com/level12/pals/commit/681c3ba
.. _5a0963b: https://github.com/level12/pals/commit/5a0963b
.. _0d8eb98: https://github.com/level12/pals/commit/0d8eb98
.. _0786766: https://github.com/level12/pals/commit/0786766
.. _f5743a6: https://github.com/level12/pals/commit/f5743a6
.. _b63c71a: https://github.com/level12/pals/commit/b63c71a
.. _dabb497: https://github.com/level12/pals/commit/dabb497
.. _b811a99: https://github.com/level12/pals/commit/b811a99
.. _0637f39: https://github.com/level12/pals/commit/0637f39
.. _af2e91f: https://github.com/level12/pals/commit/af2e91f


0.2.0 released 2019-03-07
-------------------------

- Fix misspelling of "acquire" (737763f_)

.. _737763f: https://github.com/level12/pals/commit/737763f


0.1.0 released 2019-02-22
-------------------------

- Use `lock_timeout` setting to expire blocking calls (d0216ce_)
- fix tox (1b0ffe2_)
- rename to PALs (95d5a3c_)
- improve readme (e8dd6f2_)
- move tests file to better location (a153af5_)
- add flake8 dep (3909c95_)
- fix tests so they work locally too (7102294_)
- get circleci working (28f16d2_)
- suppress exceptions in Lock __del__ (e29c1ce_)
- Add hang.py script (3372ef0_)
- fix packaging stuff, update readme (cebd976_)
- initial commit (871b877_)

.. _d0216ce: https://github.com/level12/pals/commit/d0216ce
.. _1b0ffe2: https://github.com/level12/pals/commit/1b0ffe2
.. _95d5a3c: https://github.com/level12/pals/commit/95d5a3c
.. _e8dd6f2: https://github.com/level12/pals/commit/e8dd6f2
.. _a153af5: https://github.com/level12/pals/commit/a153af5
.. _3909c95: https://github.com/level12/pals/commit/3909c95
.. _7102294: https://github.com/level12/pals/commit/7102294
.. _28f16d2: https://github.com/level12/pals/commit/28f16d2
.. _e29c1ce: https://github.com/level12/pals/commit/e29c1ce
.. _3372ef0: https://github.com/level12/pals/commit/3372ef0
.. _cebd976: https://github.com/level12/pals/commit/cebd976
.. _871b877: https://github.com/level12/pals/commit/871b877