Go to file
程广 14d81e8f5c ```
feat(lock): 实现基于TCP和命名管道的读写锁服务端与客户端

新增支持通过TCP协议和命名管道进行通信的读写锁机制,包括服务端和客户端实现。
主要变更包括:

- 添加 LockServer 基类及 NamedPipeLockServer、TCPLockServer 实现
- 新增 LockClient 基类以及 NamedPipeRWLock 和 TcpRwLock 客户端实现
- 更新测试用例以适配新的连接配置方式,并增加对 TCP 锁的支持
- 调整 jest 超时设置以便更好地支持异步锁操作测试
- 导出新模块至 index.js 便于外部使用
```
2025-11-17 15:32:19 +08:00
__tests__ ``` 2025-11-17 15:32:19 +08:00
.gitignore feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
LICENSE feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
README.md feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
async-lock.js feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
file-lock.js feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
index.js ``` 2025-11-17 15:32:19 +08:00
jest.config.js feat(init): 初始化项目结构和基础代码 2025-11-13 22:32:05 +08:00
lock-client.js ``` 2025-11-17 15:32:19 +08:00
lock-client.namedpipe.js ``` 2025-11-17 15:32:19 +08:00
lock-client.tcp.js ``` 2025-11-17 15:32:19 +08:00
lock.js ``` 2025-11-17 15:32:19 +08:00
lock.namedpipe.js ``` 2025-11-17 15:32:19 +08:00
lock.tcp.js ``` 2025-11-17 15:32:19 +08:00
package.json feat(namedpipe): 实现基于命名管道的读写锁功能 2025-11-16 21:00:55 +08:00

README.md

nlocks

A Node.js library providing asynchronous locking mechanisms including in-memory locks and file-based locks for coordinating access to resources across concurrent processes.

Features

  • AsyncLock: In-memory asynchronous lock for coordinating access to resources within a single process
  • FileLock: File-based lock for coordinating access to resources across multiple processes
  • Lightweight and easy to use
  • Promise-based API
  • Comprehensive test coverage

Installation

npm install nlocks

Usage

const { AsyncLock, FileLock } = require('nlocks');

const asyncLock = new AsyncLock();
const fileLock = new FileLock();

AsyncLock

An in-memory lock for synchronizing async operations within a single Node.js process:

const AsyncLock = require('nlocks/async-lock');

const lock = new AsyncLock();

async function protectedOperation() {
  await lock.acquire();
  try {
    // Your critical section code here
    console.log('Performing protected operation');
    await someAsyncWork();
  } finally {
    lock.release();
  }
}

FileLock

A file-based lock for synchronizing operations across multiple Node.js processes:

const FileLock = require('nlocks/file-lock');

const lock = new FileLock();

async function fileProtectedOperation() {
  await lock.acquire('/path/to/your/file.txt');
  try {
    // Your file operation code here
    console.log('Performing file operation');
    await someFileAsyncWork();
  } finally {
    lock.release('/path/to/your/file.txt');
  }
}

API

AsyncLock

  • new AsyncLock() - Creates a new AsyncLock instance
  • acquire(): Promise<void> - Acquires the lock
  • release(): void - Releases the lock

FileLock

  • new FileLock() - Creates a new FileLock instance
  • acquire(filePath): Promise<void> - Acquires a lock for the specified file path
  • release(filePath): void - Releases the lock for the specified file path

Testing

Run the test suite with:

npm test

License

MIT