npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-lock

v0.2.2

Published

Node.js lock library for multi-process read/write

Downloads

43

Readme

node-lock

Node.js进程互斥锁:

Linux下默认读写/dev/shm.

Notice: 这一分支下开发互斥锁版本. 如无需锁支持, 可以使用master分支HEAD的高性能版本.

TODO

  • Parser性能测试
  • 压力测试

原理: 由于Node的单进程单线程模型, 同一时间只允许一个阻塞任务执行, 利用这一机制在Linux内存映射文件系统/dev/shm上使用Node原生的阻塞读写函数, 可以实现多进程对同一块内存区域访问的读写互斥作用.

如果需要进一步控制资源访问, 需要用.lock/.unlock. 它会根据LockFile判断是否允许申请资源锁, 用lock(key)对一个资源的事务过程加锁, 锁在超时(默认5s)后会自动释放掉, 申请失败时会在next Tick里重试.

Notice: 由于资源访问的互斥性, 带来不可避免的同步原语, 因此任何异步执行是无意义的, 最终仍需要等待锁的释放, 所以服务端采用同步模型. 但C/S的结构允许客户端的异步请求, 这样就做到了异步互斥.

Usage

# master process
# lock server
LockProxy = require 'node-lock/proxy'

# LockServer [options]
# @options:
# + namespace: 命名空间(默认为"default")
# + dir: 共享内存位置, linux下默认为/dev/shm, osx需要手动指定一块已经创建区域
# m = LockProxy namespace: 'ddd', dir: '/Volume/shm'
# m = LockProxy namespace: 'ddd', dir: '/Volume/shm'
# 启动服务端
LockProxy().startStandAlone()
# other process
LockClient = require 'node-lock'
client = new LockClient '/tmp/-tmp-default'
# 连接服务端socket
client.connect()
.then (resource) ->
  resource
  .lock 'key::1'
  .once 'lock', ->
    resource.create 'key::1', 'value'
    .once 'create', ->
      resource.retrieve 'key::1'
    .once 'retrieve', (ret) ->
      console.log ret
      resource.unlock 'key::1'
.catch (err) ->
  console.error err