#mysql #orm #lock #locking

logic-lock

MySQL logic locks implemented over sea-orm

6 releases (1 stable)

1.0.0 Aug 8, 2024
0.12.0 Aug 18, 2023
0.11.0 Feb 7, 2023
0.10.5 Dec 2, 2022
0.9.0 Nov 7, 2022

#403 in Database interfaces

Download history 129/week @ 2024-08-06 5/week @ 2024-08-13 7/week @ 2024-09-10 6/week @ 2024-09-17 24/week @ 2024-09-24 6/week @ 2024-10-01 1/week @ 2024-10-08

290 downloads per month

MIT license

17KB
353 lines

logic-lock

MySQL logic locks implemented over sea-orm

Locking

Lock::build takes a key, any owned connection, so it can be a sea-orm::DatabaseConnection, a sea-orm::DatabaseTransaction or another Lock himself, and an optional timeout in seconds, defaulting to 1 second.
In case of error, the owned connection is returned alongside the database error, if any, this way the connection isn't automatically dropped.
Lock himself acts as a connection, so you can use it everywhere you would have used the original connection.

let lock = logic_lock::Lock::build("my_lock_key", conn, None).await.unwrap();

Unlocking

Since MySQL logic locks lives in the session, not in a transaction like table locks, a not-dropped lock will live as long as the connection, and if a connection is part of a connection pool, this can be a really long time.
To release a Lock simply use the Lock::release method.
On success it will return the original connection, on error it will return the Lock himself alongside with the database error, if any.

let conn = lock.release().await.unwrap();

Drop

Dropping a locked Lock will log an error.
It has been chosen to not panic to avoid cases of panic-while-panicking.

Dependencies

~8.5MB
~130K SLoC