Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AlanCoding opened this issue Dec 18, 2024 · 0 comments
Open

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

AlanCoding opened this issue Dec 18, 2024 · 0 comments

Comments

@AlanCoding
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant