Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added "readLock" option to aquireLock #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ Creates a "fail closed" client that acquires "fail closed" locks. If process cra

Creates a "fail open" client that acquires "fail open" locks. If process crashes and lock is not released, lock will eventually expire after `leaseDurationMs` from last heartbeat sent (if any). This means that if process acquires a lock, goes to sleep for more than `leaseDurationMs`, and then wakes up assuming it still has a lock, then it can perform an operation ignoring other processes that may assume they have a lock on the operation.

### client.acquireLock(id, callback)
### client.acquireLock(id, [opt], callback)

* `id`: _String\|Buffer\|Number_ Unique identifier for the lock. The type must correspond to lock table's partition key type.
* `opt`: _Object_ Options, optionally containing
* readLock: _Boolean_ Indicate if this is a read lock.
* `callback`: _Function_ `(error, lock) => {}`
* `error`: _Error_ Error, if any.
* `lock`: _DynamoDBLockClient.Lock_ Successfully acquired lock object. Lock object is an instance of `EventEmitter`. If the `lock` is acquired via a fail open `client` configured to heartbeat, then the returned `lock` may emit an `error` event if a `heartbeat` operation fails.
Expand Down
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ FailClosed.schema =
config: require("./schema/failClosedConfig.js")
};

FailClosed.prototype.acquireLock = function(id, callback)
FailClosed.prototype.acquireLock = function(id, opts, callback)
{
if (callback === undefined)
{
callback = opts;
opts = {};
}
const self = this;
const workflow = new events.EventEmitter();
setImmediate(() => workflow.emit("start",
Expand Down Expand Up @@ -69,6 +74,11 @@ FailClosed.prototype.acquireLock = function(id, callback)
"#partitionKey": self._partitionKey
}
};
if (opts.readLock === true)
{
params.Item.readLock = 'readLock';
params.ConditionExpression += ' OR attribute_exists(readLock)';
}
params.Item[self._partitionKey] = dataBag.id;
self._dynamodb.put(params, (error, data) =>
{
Expand Down Expand Up @@ -147,8 +157,13 @@ FailOpen.schema =
config: require("./schema/failOpenConfig.js")
};

FailOpen.prototype.acquireLock = function(id, callback)
FailOpen.prototype.acquireLock = function(id, opts, callback)
{
if (callback === undefined)
{
callback = opts;
opts = {};
}
const self = this;
const workflow = new events.EventEmitter();
setImmediate(() => workflow.emit("start",
Expand Down Expand Up @@ -220,6 +235,11 @@ FailOpen.prototype.acquireLock = function(id, callback)
"#partitionKey": self._partitionKey
}
};
if (opts.readLock === true)
{
params.Item.readLock = 'readLock';
params.ConditionExpression += ' OR attribute_exists(readLock)';
}
if (self._trustLocalTime)
{
params.Item.lockAcquiredTimeUnixMs = (new Date()).getTime();
Expand Down