# @boxed/warlock

> Battle-hardened distributed locking using redis

Latest version **1.1.2** (published 2022-01-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @boxed/warlock
pnpm add @boxed/warlock
yarn add @boxed/warlock
bun add @boxed/warlock
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2022-01-26 |
| First published | 2020-01-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 25.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 145 |
| Author | Geoff Wagstaff |
| Maintainers | adamg1, dfu-boxed, rsanchezbx, sgchung, derence, josephlum, blugavere, boxed-deploy, pheroth, johnkimz, chrissung, georgeyang316, mrestine, acolicc, andytian, greenieweinee, wlee22, paulhan221, leejustin, natalianyc, brentboxed, holdenmc, don-boxed, jfethers, gdominguez210, emacsnw, wjzhou, broveloper, ma6e1, suhanw, minalbee, vchari, hellageek, chenboxed, elinboxed, gio-boxed, andre_11, bertoboxed, sadiqboxed, james1x0, snigdhasaikam |
| Keywords | node.js, redis, lock |

## Links

- npm: https://www.npmjs.com/package/@boxed/warlock
- Repository: https://github.com/thedeveloper/warlock
- Issues: https://github.com/thedeveloper/warlock/issues
- npm.io page: https://npm.io/package/@boxed/warlock

## Dependencies (1)

- [uuid](https://npm.io/package/uuid.md) 8.3.2

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2022-01-26
- 1.1.1 — 2022-01-26
- 1.1.0 — 2022-01-26
- 1.0.1 — 2020-01-07
- 1.0.0 — 2020-01-07

## README

warlock
=======

Battle-hardened distributed locking using redis.

## Requirements

* [node-redis](https://github.com/mranney/node_redis) compatible with `v0.10`
* Redis `v2.6.12` or above. If you're running a Redis version from `v2.6.0` to `v2.6.11` inclusive use `v0.0.7` of this module.

## Install

    npm install node-redis-warlock

## Usage

```javascript

var Warlock = require('node-redis-warlock');
var redis = require('redis');

// Establish a redis client and pass it to warlock
var redis = redis.createClient();
var warlock = Warlock(redis);

// Set a lock
var key = 'test-lock';
var ttl = 10000; // Lifetime of the lock

warlock.lock(key, ttl, function(err, unlock){
  if (err) {
    // Something went wrong and we weren't able to set a lock
    return;
  }

  if (typeof unlock === 'function') {
    // If the lock is set successfully by this process, an unlock function is passed to our callback.
    // Do the work that required lock protection, and then unlock() when finished...
    //
    // do stuff...
    //
    unlock();
  } else {
    // Otherwise, the lock was not established by us so we must decide what to do
    // Perhaps wait a bit & retry...
  }
});

// set a lock optimistically
var key = 'opt-lock';
var ttl = 10000;
var maxAttempts = 4; // Max number of times to try setting the lock before erroring
var wait = 1000; // Time to wait before another attempt if lock already in place
warlock.optimistic(key, ttl, maxAttempts, wait, function(err, unlock) {});

```

## ProTips

* Read my [Distributed locks using Redis](https://engineering.gosquared.com/distributed-locks-using-redis) article and Redis' author's [A proposal for more reliable locks using Redis](http://antirez.com/news/77) to learn more about the theory of distributed locks using Redis.

---
_Source: https://npm.io/package/@boxed/warlock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
