Skip to main content

Mongoengine pessimistic lock utility

Project description

# 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.


Project details


Release history Release notifications

This version

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for mongoEngineLock, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size mongoEngineLock-0.1.0-py2-none-any.whl (5.5 kB) File type Wheel Python version py2 Upload date Hashes View hashes
Filename, size mongoEngineLock-0.1.0.tar.gz (3.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page