Git Product home page Git Product logo

redis-mutex's Introduction

redis-mutex

mutex lock implemented using redis

Install

npm install @unbugcolen/redis-mutex

Use

// js
const redisMutex = require('@unbugcolen/redis-mutex').default;
// ts
import redisMutex from '@unbugcolen/redis-mutex';

const mutex = new Lock('redis-master', 6379);
mutex.lock('mutex_key', async () => {
    // business code...
});

Test

npm run test
import * as assert from 'assert';
import Lock from '..';
import { sleep } from '../unit';
const mutex1 = new Lock('redis-master', 6379);
const mutex2 = new Lock('redis-master', 6379);
function getRandomArbitrary(min: number, max: number) {
    return Math.random() * (max - min) + min;
}

describe('base', function () {
    it('promise all', async () => {
        // this.timeout(10000);
        let number = 0;
        async function unsafeAdd() {
            let temp = number;
            await sleep(getRandomArbitrary(1, 100));
            number = temp + 1;
        }
        async function unsafeSubtract() {
            let temp = number;
            await sleep(getRandomArbitrary(1, 100));
            number = temp - 1;
        }
        const key = 'key';
        await Promise.all(
            new Array(10)
                .fill(0)
                .map(async () => mutex1.lock(key, unsafeAdd))
                .concat(new Array(10).fill(0).map(async () => mutex2.lock(key, unsafeSubtract)))
        );

        assert(number === 0, 'promise failed');
    });

    it('promise && setTimeout', async () => {
        // this.timeout(10000);
        let number = 0;
        async function unsafeAdd() {
            let temp = number;
            await sleep(getRandomArbitrary(1, 100));
            number = temp + 1;
        }
        async function unsafeSubtract() {
            let temp = number;
            await sleep(getRandomArbitrary(1, 100));
            number = temp - 1;
        }
        const key = 'key';
        async function func1(func: () => Promise<void>) {
            return await mutex1.lock(key, func, true, 10);
        }
        async function func2(func: () => Promise<void>) {
            return await mutex2.lock(key, func, true, 100);
        }
        let result = await new Promise(async (resolve) => {
            let result: any = [];
            setTimeout(async () => {
                await func1(unsafeAdd).then(() => result.push(3));
                await func2(unsafeSubtract).then(() => result.push(4));
            }, 1000);
            await func1(unsafeAdd).then(() => result.push(1));
            await func2(unsafeSubtract).then(() => result.push(2));
            setInterval(() => {
                if (result.length === 4) {
                    resolve(result);
                }
            }, 1000);
        });
        // console.log(number, result);
        assert(number === 0, `promise && setTimeout failed`);
    });

    it('watchdog', async function () {
        this.timeout(100000);
        async function businessWaitingFun(fun: () => void, time: number = 1000): Promise<boolean> {
            await fun();
            await sleep(time);
            return true;
        }

        let number = 0;
        async function unsafeAdd() {
            let temp = number;
            await sleep(getRandomArbitrary(1, 100));
            number = temp + 1;
        }
        async function unsafeSubtract() {
            let temp = number;
            await sleep(getRandomArbitrary(1, 100));
            number = temp - 1;
        }
        const key = 'key';
        async function func1(func: () => void, time: number = 1000) {
            return await mutex1.lock(key, async () => await businessWaitingFun(func, time), true, 10);
        }
        async function func2(func: () => void, time: number = 1000) {
            return await mutex2.lock(key, async () => await businessWaitingFun(func, time), true, 10);
        }

        let result: any = [];
        await Promise.all([
            func1(unsafeAdd).then(() => result.push(1)),
            func1(unsafeSubtract).then(() => result.push(2)),
            func2(unsafeAdd).then(() => result.push(3)),
            func2(unsafeSubtract).then(() => result.push(4)),
        ]);
        // console.log(result);
        assert(number === 0, 'watchdog failed');
    });
});

Dependence

redis

Thanks

process-key-mutex

redis-mutex's People

Contributors

unbugcolen avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.