
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
mongodb-lock
Advanced tools
A really light-weight way to get distributed locks with a nice API if you're already using MongoDB.
Please note that the API has changed for v1.0 (compared to v0.4 and previously). This fits in with MongoDB Driver v3 and above.
Create a connection to your MongoDB database, and use it to create a lock object:
const mongodb = require('mongodb')
const mongoDbLock = require('.')
const url = 'mongodb://localhost:27017/'
const dbName = 'test'
const colName = 'locks'
const lockName = 'database-backup'
mongodb.MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
const db = client.db(dbName)
const col = db.collection(colName)
// supply the collection to use and the lock name
const lock = mongoDbLock(col, lockName)
lock.ensureIndexes(function(err, result) {
console.log('Ensured Index:', err, result)
})
// Your Program Here!
})
Now, acquire the lock:
lock.acquire((err, code) => {
if (err) {
return console.error(err)
}
if ( !code ) {
// lock was not acquired
console.log('code=' + code)
return
}
// lock was acquired
})
Once you have a lock, you have a 30 second timeout until the lock is released. You can release it earlier by supplying the code:
lock.release(code, (err, ok) => {
if (err) {
return console.error(err)
}
if (!ok) {
console.log("Lock was not released, perhaps it's already been released or timed out")
return
}
console.log('Lock released ok')
})
You should make sure any indexes have been added to the collection to make the queries faster:
lock.ensureIndexes(err => {
if (err) {
return console.error(err)
}
// all ok
})
Multiple locks can use the same collection and operate quite independently:
const dbBackupLock = mongoDbLock(db, 'locks', 'database-backup')
const hourlyStats = mongoDbLock(db, 'locks', 'hourly-stats')
const sendInvoices = mongoDbLock(db, 'locks', 'send-invoices')
Currently there are two options: timeout
and removeExpired
Currently the default is 30 seconds, but you can change it (in milliseconds):
// lock for 60 seconds
const opts = { timeout : 60 * 1000 }
// create the lock object
const uploadFiles = mongoDbLock(db, 'locks', 'upload-files', opts)
// acquire the lock
uploadFiles.lock((err, code) => {
// locked for 60s
})
Currently the default value is false
.
When set to true
this will remove expired lock records from MongoDB instead of modifying them.
// remove old locks from MongoDB
const opts = { removeExpired : true }
// create the lock object
const cleanUpCacheDirs = mongoDbLock(db, 'locks', 'clean-up-cache-dirs', opts)
// all old locks are now deleted, rather than just hanging around
$ npx chilts
╒════════════════════════════════════════════════════╕
│ │
│ Andrew Chilton (Personal) │
│ ------------------------- │
│ │
│ Email : andychilton@gmail.com │
│ Web : https://chilts.org │
│ Twitter : https://twitter.com/andychilton │
│ GitHub : https://github.com/chilts │
│ GitLab : https://gitlab.org/chilts │
│ │
│ Apps Attic Ltd (My Company) │
│ --------------------------- │
│ │
│ Email : chilts@appsattic.com │
│ Web : https://appsattic.com │
│ Twitter : https://twitter.com/AppsAttic │
│ GitLab : https://gitlab.com/appsattic │
│ │
│ Node.js / npm │
│ ------------- │
│ │
│ Profile : https://www.npmjs.com/~chilts │
│ Card : $ npx chilts │
│ │
╘════════════════════════════════════════════════════╛
MIT - http://chilts.mit-license.org/2014/
(Ends)
FAQs
Locks which uses MongoDB's atomic operations.
We found that mongodb-lock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.