Skip to content

Commit

Permalink
docs: clarify about atomic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 30, 2024
1 parent 3792580 commit 9df9778
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ Originally this library was implemented using [hashes](https://redis.io/docs/dat

Since we need to do that all the time, we prefer to use key/value. Also this approach allow to customize serializer/deserializer, which is JSON by default.

## Are writting operations atomic?

No, writes operatoins are not atomic because there are very few use cases where that matters. **openkey** is designed to process a constant stream of requests, where the only thing important to control reaching the limit of each plan.

In case you need it, you can combine **openkey** with [superlock](https://github.com/Kikobeats/superlock), check this example.

# License

**openkey** © [microlink.io](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/openkey/blob/master/LICENSE.md) License.<br>
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
},
"keywords": [],
"dependencies": {
"http-body": "~1.0.11",
"mri": "~1.2.0",
"ms": "~2.1.3",
"send-http": "~1.0.6"
"superlock": "~1.1.3"
},
"devDependencies": {
"@commitlint/cli": "latest",
Expand All @@ -55,11 +54,13 @@
"gulp-concat": "latest",
"gulp-postcss": "latest",
"gulp-uglify": "latest",
"http-body": "latest",
"ioredis": "latest",
"nano-staged": "latest",
"npm-check-updates": "latest",
"postcss": "latest",
"postcss-focus": "latest",
"send-http": "latest",
"simple-git-hooks": "latest",
"standard": "latest",
"standard-version": "latest"
Expand Down
26 changes: 26 additions & 0 deletions test/usage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { setTimeout } = require('timers/promises')
const { withLock } = require('superlock')
const { randomUUID } = require('crypto')
const Redis = require('ioredis')
const test = require('ava')
Expand Down Expand Up @@ -110,3 +111,28 @@ test(".increment # don't increment more than the limit", async t => {
t.is(usage.remaining, 0)
}
})

test('.increment # handle race conditions (using superlock)', async t => {
const lock = withLock()

const plan = await openkey.plans.create({
id: randomUUID(),
limit: 1000,
period: '100ms'
})
const key = await openkey.keys.create({ plan: plan.id })

await Promise.all(
[...Array(100).keys()].map(() =>
lock(async () => {
const { pending, ...usage } = await openkey.usage.increment(key.value)
await pending
return usage
})
)
)

const usage = await openkey.usage(key.value)
t.is(usage.limit, 1000)
t.is(usage.remaining, 900)
})

0 comments on commit 9df9778

Please sign in to comment.