pglock
v1.0.3
Published
基於 PostgreSQL 互斥鎖 依賴於 [advisory-lock](https://github.com/binded/advisory-lock/blob/master/README.md)
Downloads
7
Readme
pglock
基於 PostgreSQL 互斥鎖 依賴於 advisory-lock
Usage
import PGlock from 'pglock'
const pglock = new PGlock('postgres://user:pass@localhost:3475/dbname')
const mutex = pglock.mutex('some-lock-name')
async function runSerialTaskAsync() {
await mutex.lock()
try {
// Do something...
} finally {
mutex.unlock()
}
}
使用 trylock
import PGlock from 'pglock'
const pglock = new PGlock('postgres://user:pass@localhost:3475/dbname')
const mutex = pglock.mutex('some-lock-name')
async function runSerialTaskAsync() {
if (!await mutex.trylock()) {
return
}
try {
// Do something...
} finally {
mutex.unlock()
}
}
});