NodeJs - 实现当前线程唯一的单例对象

NodeJs - 实现当前线程唯一的单例对象

一. 实现当前线程唯一的单例对象

Java 里面,一般都把这种和当前线程绑定的单例对象存储到ThreadLocal里面,但是Node里面没有这种存储,那咋办呢?直接上代码:

const cluster = require('cluster');

class Context {
    constructor() {
        this.id = Math.random().toString(36).substring(2);
    }

    static create() {
        if (cluster.isWorker) {
            if (!cluster.worker.hasOwnProperty('_context')) {
                cluster.worker._context = new Context();
            }
            return cluster.worker._context;
        } else {
            if (!global.hasOwnProperty('_context')) {
                global._context = new Context();
            }
            return global._context;
        }
    }
    
    static release(){
		if (cluster.isWorker) {
            if (cluster.worker.hasOwnProperty('_context')) {
                delete cluster.worker._context;
            }
        } else {
            if (global.hasOwnProperty('_context')) {
                delete global._context;
            }
        }
	}
}

module.exports = Context;

create方法中:

  1. 我们首先检查当前进程/线程是否是一个工作进程。
  2. 如果是工作进程,我们使用cluster.worker对象来存储和获取Context对象。
  3. 每个工作进程都有自己的上下文,因此在每个工作进程中创建的Context对象都是唯一的。

如果当前进程/线程不是一个工作进程,我们使用global对象来存储和获取Context对象。在这种情况下,Context对象在整个进程/线程中都是唯一的。

测试代码:

const Context = require('./context');

(async () => {
    // 循环10次,每次创建一个新的Context实例,异步创建
    const promiseList = []
    for (let i = 0; i < 5; i++) {
        const context = Context.create();
        console.log('sync', context.id);
        promiseList.push(test());
    }
    await Promise.all(promiseList);
    // 清除这个对象,让下一次请求能够重新再赋值一个新的Context
    Context.release();
})();

async function test() {
    setTimeout(() => {
        const context = Context.create();
        console.log('async', context.id);
    }, 2000);
}

结果如下:
在这里插入图片描述

但是这种方式并不准确,因为对于Node来说,这里的上下文的释放和创建都是我们手动控制的,而从Node的实现角度,这种方式可能会导致上下文窜了。

可以看下NodeJs - 单线程模型和高并发处理原理,而我们这里就是把全局对象绑定在工作进程上的。(后来我生产上实际验证发现,上下文都窜了)

方式一仅供参考了解,后面再来看下更加合理的一种实现方案,AsyncLocalStorage 的使用,大部分开源框架实际上使用的都是它来实现上下文的传递,包括Egg框架也是如此。

二. AsyncLocalStorage 的运用

官方文档

1.我们安装依赖:

npm i async_hooks

2.我们创建一个全局的storage,并通过模块化导出,创建一个storage.ts文件。

const { AsyncLocalStorage } = require('async_hooks');
const storage = new AsyncLocalStorage();
export default storage;

3.其他地方,引入storage.ts中的对象:

const http = require('http');
const Context = require('./context');
const storage = require('./storage');

http.createServer((req, res) => {
    const context = new Context()
    console.log(`入口创建Context并塞入上下文`, context)
    storage.run(context, () => {
        const ctx = storage.getStore();
        console.log(`上下文一`, ctx)
        // Imagine any chain of async operations here
        setImmediate(() => {
            const ctx = storage.getStore();
            console.log(`上下文二`, ctx)
            res.end();
        });
    });
}).listen(8080);

for (let i = 0; i < 2; i++) {
    http.get('http://localhost:8080');
}

结果如下:一共两次请求,每次请求都是共享一个上下文对象。
在这里插入图片描述
基本上使用方式就是这样:

  • storage.run(obj, fn)obj就是代表你要存储的上下文对象,fn就是你后续的整个业务处理函数。就是把obj塞到你的这整个fn执行的生命周期里面。
  • storage.getStore() :那么你的业务逻辑代码,就可以通过getStore来获取之前塞好的上下文对象了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zong_0915

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值