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

Add distributed locking capability #5

Merged
merged 3 commits into from
Apr 15, 2017
Merged

Add distributed locking capability #5

merged 3 commits into from
Apr 15, 2017

Conversation

connor4312
Copy link
Member

@connor4312 connor4312 commented Apr 14, 2017

Example:

const { Etcd3 } = require('etcd3');
const client = new Etcd3();

client.lock('my_resource').do(() => {
  // The lock will automatically be released when this promise returns
  return doMyAtomicAction();
});

See the code for some more details. I branch off a bit from what, say python-etcd3 does and opted to use a lease for lock management. This prevents the resources from being locked indefinitely if an event occurs that prevents the consumer from unlocking the resource when they're finished with it. The logic is that the set of events which would allow a lease to be lost mid-lock is a subset of the events which would prevent the lease from ever being unlocked.

src/errors.ts Outdated
export class EtcdLockFailedError extends Error {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, EtcdLockFailedError.prototype);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yo, if you target es6, you don't need this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I was still have some issues with the ts-node-based unit tests. I'll see if there's an option to get it to target ES6.

Copy link

@SimonSchick SimonSchick Apr 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually it uses es6 by default (because it's your tsconfig)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't for some reason, but setting an env var worked

@kragniz
Copy link

kragniz commented Apr 15, 2017

If you can wait until etcd-io/etcd#7444 is released, I'd recommend using that instead.

@connor4312
Copy link
Member Author

Ah, neat, I wasn't aware that was a thing. I'll probably get this in and switch over to that at some point in the future -- should be possible to do without any breaking changes.

@connor4312 connor4312 merged commit 8615089 into txn Apr 15, 2017
@connor4312 connor4312 deleted the locks branch April 15, 2017 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants