# PessimisticLockMongo
Pessimistic lock implementation for mongoengine, using a very simple logic
`Inspired by mongolock and pessimistic locks concept.`
Read more about the implementation [here](https://medium.com/@abhishek.tamrakar/implementing-pessimistic-locks-in-mongodb-8f3fbe2ddfa9)
## Setting up the context
The real intention behind writing this utility isn't just Database locking. Locking is already handled in mongoDB, sufficient or not, but it locks the Database with exclusive write locks, thats when the problem begins.
With multiple collections and frequent requests coming in, it is on DB performance when a transaction is waitingon the lock on Database, when there is only one collection involved in the operation at the moment.
Hence, this utility, targets collection based locking mechanism, fully controoled in application for the DB.
## Usage
```
from mongoEngineLock import mongoEngineLock, MongoLockTimeout
lock = mongoEngineLock('<db..name>', poll=1, timeout=40, retries=15)
with lock(entity):
try:
<..do something here..>
except Exception as e:
raise e
# or use with conditional statements
if lock(entity):
try:
<..do something here..>
except Exception as e:
raise e
```
##### mongoEngineLock Parameters:
**client**: connection information for MongoEngine.
**poll**: interval to introduce a delay while retrying.
**timeout**: interval (seconds) to introduce timeout.
**retries**: number of retries before timeout.
##### Instance of the class takes exactly 1 parameter:
**entity**: owner of the lock or collection name(in case there are more than one collections to be managed in a database).
## Misc
* Try to use a smaller polling interval or interval based on time taken to execute the query, in case of bigger DB size.
* Worth to mention, timeout or retries should not supersede each other.
* Use proper NTP servers to keep your machines in time sync.
Pessimistic lock implementation for mongoengine, using a very simple logic
`Inspired by mongolock and pessimistic locks concept.`
Read more about the implementation [here](https://medium.com/@abhishek.tamrakar/implementing-pessimistic-locks-in-mongodb-8f3fbe2ddfa9)
## Setting up the context
The real intention behind writing this utility isn't just Database locking. Locking is already handled in mongoDB, sufficient or not, but it locks the Database with exclusive write locks, thats when the problem begins.
With multiple collections and frequent requests coming in, it is on DB performance when a transaction is waitingon the lock on Database, when there is only one collection involved in the operation at the moment.
Hence, this utility, targets collection based locking mechanism, fully controoled in application for the DB.
## Usage
```
from mongoEngineLock import mongoEngineLock, MongoLockTimeout
lock = mongoEngineLock('<db..name>', poll=1, timeout=40, retries=15)
with lock(entity):
try:
<..do something here..>
except Exception as e:
raise e
# or use with conditional statements
if lock(entity):
try:
<..do something here..>
except Exception as e:
raise e
```
##### mongoEngineLock Parameters:
**client**: connection information for MongoEngine.
**poll**: interval to introduce a delay while retrying.
**timeout**: interval (seconds) to introduce timeout.
**retries**: number of retries before timeout.
##### Instance of the class takes exactly 1 parameter:
**entity**: owner of the lock or collection name(in case there are more than one collections to be managed in a database).
## Misc
* Try to use a smaller polling interval or interval based on time taken to execute the query, in case of bigger DB size.
* Worth to mention, timeout or retries should not supersede each other.
* Use proper NTP servers to keep your machines in time sync.
Metadata
Release files for mongoEngineLock 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mongoEngineLock-0.1.0.tar.gz | 3.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mongoEngineLock-0.1.0-py2-none-any.whl | Python 2 | none | any | Details |
Total release size: 9.1 kB
Release files / mongoEngineLock-0.1.0.tar.gz
| Download URL | mongoEngineLock-0.1.0.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7be6c0792516115c8378ba93778d821cbd54a1468ad035448fff8f679a1cedb8
|
|
BLAKE2b-256 checksum How to use checksums |
b9f6336a2232e4ce947400556e7a6e3b6ce2b0c3496dde7216c694c13d0d0c78
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/2.7.12
|
Release files / mongoEngineLock-0.1.0-py2-none-any.whl
| Download URL | mongoEngineLock-0.1.0-py2-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
2dbfd690188b62b4a476bb95aa90a6154e9be6a6750343f505701b32343424c2
|
|
BLAKE2b-256 checksum How to use checksums |
6f6663d9c9b2032375568098598e96d5b9960e927f6190051aca9ae1c08b6631
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/2.7.12
|