Skip to content

Refactor out string -> lock_id method from django-pglocks code #679

@AlanCoding

Description

@AlanCoding

Followup from #661

The proposal here is to fully adopt the code held in django_pglocks_advisory_lock. That means that we would extract several parts of the logic into the new method:

    elif isinstance(lock_id, str):
        # Generates an id within postgres integer range (-2^31 to 2^31 - 1).
        # crc32 generates an unsigned integer in Py3, we convert it into
        # a signed integer using 2's complement (this is a noop in Py2)
        pos = crc32(lock_id.encode("utf-8"))
        lock_id = (2**31 - 1) & pos
        if pos & 2**31:
            lock_id -= 2**31

We would put this in a new method, takes a string, returns an integer. Also, very interesting for debugging, we could make a reverse method for debugging, so it takes an integer and returns the string that produced it? We could even have a method that returned a list of all the locks being held, maybe?

Next, we should change the import of from django.db connections to the more direct from django.db.transaction import get_connection

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions