Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

locko

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

locko

A simple in-process locking mechanism for critical sections of code.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
increased by34.55%
Maintainers
1
Weekly downloads
 
Created
Source

Locko

Locko is a small package for implementing exclusive locking on critical sections of code.

When to use

Use locko when you need to ensure that only one "thread" can be inside of a section of code at once. Consider the following code.

const util = require('util');
const sleep = util.promisify(setTimeout);

async function print() {
  console.log('First');
  await sleep(100);
  console.log('Second');
  await sleep(100);
  console.log('Third');
}

print();
print();
print();

Without locko, this will print:

First
First
First
Second
Second
Second
Third
Third
Third

Now add locko:

const util = require('util');
const sleep = util.promisify(setTimeout);

async function print() {
  await locko.lock('print');

  console.log('First');
  await sleep(100);
  console.log('Second');
  await sleep(100);
  console.log('Third');

  locko.unlock('print');
}

print();
print();
print();

This will now print:

First
Second
Third
First
Second
Third
First
Second
Third

A real world example

It's often the case that you need to both read and write to a file, but without any kind of synchronization strategy, it's possible to read from a file while it's being written, or write to a file while it's being read, or have more than one writer writing at once. This is very likely to cause failures in an application.

Using locko you can ensure that no more than one operation is being executed on a file at any given time.

async function readFile(filePath) {
  await locko.lock(filePath);

  return new Promise((fulfill, reject) => {
    fs.readFile(filePath, (err, content) => {
      locko.unlock(filePath);

      if (err) {
        reject(err);
      } else {
        fulfill(content);
      }
    }); 
  }); 
}

async function writeFile(filePath, content) {
  await locko.lock(filePath);

  return new Promise((fulfill, reject) => {
    fs.writeFile(filePath, content, (err) => {
      locko.unlock(filePath);

      if (err) {
        reject(err);
      } else {
        fulfill();
      }
    });
  });
}

Best practices

If you never unlock the lock, it will remain locked forever. Therefore you should usually use a try-finally to ensure that the lock gets released. For example:

async function print() {
  try {
    await locko.lock('print');

    console.log('First');
    await wait(100);
    console.log('Second');
    await wait(100);
    console.log('Third');
  } finally {
    locko.unlock('print');
  }
}

Alternatively, you can use the doWithLock() wrapper function that handles unlocking safely for you:

async function print() {
  await locko.doWithLock('print', async () => {
    console.log('First');
    await wait(100);
    console.log('Second');
    await wait(100);
    console.log('Third');
  });
}

Keywords

FAQs

Package last updated on 23 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc