Skip to content

Commit

Permalink
feat: bring lock (#32)
Browse files Browse the repository at this point in the history
* feat(lock): introduce lock

* Update README.md

* Update README.md

* Update package.json

* Update package.json

* Update lock/lib.w

Co-authored-by: Chris Rybicki <[email protected]>

* feat: added expiry for lock

* fixed stress test

* updating README

---------

Co-authored-by: Chris Rybicki <[email protected]>
  • Loading branch information
ekeren and Chriscbr authored Mar 6, 2024
1 parent ff60e4d commit 0dc9f3b
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/lock-pull.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: lock-pull
on:
pull_request:
paths:
- lock/**
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: lock
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: lock
- name: Test
run: wing test
working-directory: lock
- name: Pack
run: wing pack
working-directory: lock
37 changes: 37 additions & 0 deletions .github/workflows/lock-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: lock-release
on:
push:
branches:
- main
paths:
- lock/**
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: lock
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: lock
- name: Test
run: wing test
working-directory: lock
- name: Pack
run: wing pack
working-directory: lock
- name: Publish
run: npm publish --access=public --registry https://registry.npmjs.org --tag
latest *.tgz
working-directory: lock
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 2 additions & 0 deletions lock/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions lock/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Wing

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 77 additions & 0 deletions lock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# lock

## Prerequisites

* [winglang](https://winglang.io).

## Installation

`sh
npm i @winglibs/lock
`

## Usage

```js
bring lock;
bring cloud;
bring util;

let l = new lock.Lock();
let withoutExpiry = new cloud.Queue() as "without expiry";
let withExpiry = new cloud.Queue() as "with expiry";
withExpiry.setConsumer(inflight (m: str) => {
let id = util.uuidv4();
l.acquire("my-lock", timeout: 20s, expiry: 10ms);
log("{id}:start");
util.sleep(1s);
log("{id}:end");
l.release("my-lock");
});

withoutExpiry.setConsumer(inflight (m: str) => {
let id = util.uuidv4();
l.acquire("my-lock", timeout: 20s);
log("{id}:start");
util.sleep(1s);
log("{id}:end");
l.release("my-lock");
});

/**
Output without the lock
│ 158e4f79-56e7-48ea-9469-4838741a255f:start
│ e6a01372-4761-4365-b909-e32fa7dad605:start
│ 158e4f79-56e7-48ea-9469-4838741a255f:end
│ e6a01372-4761-4365-b909-e32fa7dad605:end
Output with the lock:
│ 01da31df-4130-47f0-86f7-7173421a2676:start
│ 01da31df-4130-47f0-86f7-7173421a2676:end
│ a24e59d3-d088-4e4e-b134-d1c3a6dfe278:start
│ a24e59d3-d088-4e4e-b134-d1c3a6dfe278:end
Output with an expiry:
| dfac7a32-65f2-4267-bb91-e4041a22cc5f:start
| 0ccee5ac-feb3-444e-9470-c660a06c7383:start
| dfac7a32-65f2-4267-bb91-e4041a22cc5f:end
| 0ccee5ac-feb3-444e-9470-c660a06c7383:end
*/
test "count without expiry" {
withoutExpiry.push("1", "2");
util.sleep(5s);
}

test "count with expiry" {
withExpiry.push("1", "2");
util.sleep(5s);
}

```

## Maintainers

[@ekeren](https://github.com/ekeren)


## License

This library is licensed under the [MIT License](./LICENSE).
94 changes: 94 additions & 0 deletions lock/lib.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
bring expect;
bring cloud;
bring util;
bring "./lib.w" as lock;

let l = new lock.Lock();
let q = new cloud.Queue();
let counter = new cloud.Counter();

q.setConsumer(inflight (m: str) => {
l.release("consumer");
let sleepTime = duration.fromSeconds(num.fromStr(m));
l.acquire("l1", timeout: 1s);
util.sleep(sleepTime);
l.release("l1");
});

test "acquire within the time frame" {
l.acquire("consumer", timeout: 1s);
q.push("1"); // acquire lock for 1 second
l.acquire("consumer", timeout: 20s);
l.acquire("l1", timeout: 2s);
}

test "acquire outside the time frame should trow" {
l.acquire("consumer", timeout: 1s);
q.push("10"); // acquire lock for 10 seconds
l.acquire("consumer", timeout: 20s);
try {
l.acquire("l1", timeout: 2s);
assert(false);
} catch {

}
}

test "lock" {
l.acquire("l1", timeout: 1ms);
try {
l.acquire("l1", timeout: 1ms);
assert(false);
} catch {
assert(true);
}
}

test "tryLock" {
assert(l.tryAcquire("l1", timeout: 1ms));
assert(!l.tryAcquire("l1", timeout: 1ms));
}

test "release should throw if there is nothing to release" {
l.acquire("l1",timeout: 1ms);
l.release("l1");
try {
l.release("l1");
assert(false);
} catch { }
}

test "tryRelease" {
l.acquire("l1",timeout: 1ms);
expect.equal(true, l.tryRelease("l1"));
expect.equal(false, l.tryRelease("l1"));
}

test "lock Expires" {
l.acquire("l1", timeout: 1ms, expiry: 200ms);
expect.equal(false, l.tryAcquire("l1", timeout: 10ms)); // should fail to acquire
log("test: sleep 1s");
util.sleep(1s);
log("test: done sleeping");
expect.equal(true, l.tryAcquire("l1", timeout: 1s)); // should acquire
}

test "lock doesn't Expires" {
l.acquire("l1", timeout: 1ms, expiry: 3s);
expect.equal(false, l.tryAcquire("l1", timeout: 10ms)); // should fail to acquire
util.sleep(1s);
expect.equal(false, l.tryAcquire("l1", timeout: 10ms)); // should acquire
}


let q2 = new cloud.Queue() as "q2";
q2.setConsumer(inflight () => {
util.sleep(1ms);
try {
l.acquire("l1", timeout: 1s);
} catch {
assert(false);
}
});


119 changes: 119 additions & 0 deletions lock/lib.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
bring expect;
bring util;
bring cloud;
bring math;

pub struct LockAcquireOptions {
/**
Keeps trying to acquire lock until timeout has reached
*/
timeout: duration;
/**
Expire lock after expiry
*/
expiry: duration?;
}

pub class Lock {
counter: cloud.Counter;
new() {
this.counter = new cloud.Counter();
}

/**
try to acquire lock with id
@id the name of the lock
@timeout the time to try acquiring the lock
@returns true if success, false if timeout is reached
*/
pub inflight tryAcquire(id: str, options: LockAcquireOptions): bool {
try {
this.acquire(id, options);
return true;
} catch {
return false;
}
}

inflight releaseIfExpired(id: str) {
let now = datetime.systemNow().timestampMs;
let expiry = this.counter.peek("{id}-expiry");
if expiry == 0 {
return;
}
if now < expiry {
return;
}
if this.tryAcquire("{id}-expiry-lock", timeout:1ms) {
let expiry = this.counter.peek("{id}-expiry");
let expired = expiry != 0 && now > expiry;
if expired {
this.counter.set(0, "{id}-expiry");
}
this.release("{id}-expiry-lock");
this.release(id);
return;
}
log("releaseIfExpired({id}): failed to acquire {id}-expiry-lock");
return;
}
/**
try to acquire lock with id
@id the name of the lock
@timeout the time to try acquiring the lock
@throws if failed to acquire a lock within the timeout
*/
pub inflight acquire(id: str, options: LockAcquireOptions){
let acquired = util.waitUntil(inflight () => {
this.releaseIfExpired(id);
// // performance improvement to not acquire the lock and cause live locks
// let peeked = this.counter.peek(id);
// if (peeked != 0) {
// return false;
// }
let value = this.counter.inc(1, id);

if value == 0 {
if let expiry = options.expiry {
this.acquire("{id}-expiry-lock", timeout:options.timeout);
this.counter.set(datetime.systemNow().timestampMs + expiry.milliseconds, "{id}-expiry");
this.release("{id}-expiry-lock");
}
return true;
}
let preDec = this.counter.dec(1, id);
// randomize sleep time to prevent livelock
// util.sleep(duration.fromMilliseconds(100 * math.random()));
return false;
}, timeout: options.timeout);
if !acquired {
throw "Failed to acquire lock, timeout {options.timeout.seconds} seconds reached";
}
}

/**
Release the lock
@id the name of the lock
@returns false if lock is not locked
*/
pub inflight tryRelease(id: str): bool {
try {
this.release(id);
return true;
} catch {
return false;
}
}
/**
Release the lock
@id the name of the lock
@throws an exception if lock is not locked
*/
pub inflight release(id: str) {
if this.counter.peek(id) <= 0 {
throw "Lock is not being held";
}
this.counter.set(0, "{id}-expiry");
this.counter.dec(1, id);
}
}
Loading

0 comments on commit 0dc9f3b

Please sign in to comment.