Giter Site home page Giter Site logo

ehsan2003 / mutex-guard Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rouzbehsbz/mutex-guard

0.0 1.0 0.0 37 KB

Mutex implementation for synchronizing access to shared resources in Node.js applications.

License: MIT License

TypeScript 100.00%

mutex-guard's Introduction

Mutex Guard

Mutex Guard is a simple yet robust mutex implementation in TypeScript for managing critical sections and synchronizing access to shared resources in Node.js applications.

Features

  • Lightweight Implementation: Mutex Guard doesn't rely on external packages like Redis for atomic locking, keeping it lightweight and dependency-free.
  • Native Event Emitter: Implemented using the native Node.js event emitter module, providing an efficient event-based mechanism for queuing and processing lock requests, thus preventing resource contention.
  • Efficient Waiting Mechanism: Ensures that every request waiting for lock access to a resource is handled efficiently, ensuring optimal resource utilization.

Installation

You can install MutexGuard via npm:

npm install mutex-guard

Usage

In this example, we spawn three asynchronous login jobs, but we aim to call the login function only once. By utilizing the locking mechanism, we guarantee that the login function is invoked only once.

import MutexGuard from "mutex-guard";
import { setTimeout } from "timers/promises";

let isLoggedIn = false;

async function main() {
    let mutexGuard = new MutexGuard();
    let pendingPromises = [];

    for (let i = 0; i < 3; i++) {
        pendingPromises.push(login(mutexGuard));
    }

    await Promise.all(pendingPromises);
}

async function login(mutexGuard: MutexGuard) {
    await mutexGuard.lock();

    if (isLoggedIn) {
        console.log("Already logged in.");
    } else {
        console.log("Logging ...");
        await setTimeout(3000);
        isLoggedIn = true;
        console.log("Logged in.");
    }

    mutexGuard.unlock();
}

main();

Output:

Logging ...
Logged in.
Already logged in.
Already logged in.

mutex-guard's People

Contributors

ehsan2003 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.