1.0.7 • Published 1 year ago

@chandre/egg-call-context v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

egg-call-context 跨进程上下文调用

为解决多进程模式下,无法在其他进程调用长连接上下文(限单服务应用)

  1. 基于 messenger 进程间通信
  2. 消息由 agent 进程中转到对应的 woker 进程执行调用,响应结果直接返回到请求进程
  3. 执行进程意外退出自动销毁
  4. 内置基于文件系统全局状态缓存

场景

向客户端长连接请求发送消息,并获取消息处理结果

安装

npm install @chandre/egg-call-context
// config/plugin.js
exports.callContext = {
    enable: true,
    package: '@chandre/egg-call-context'
}
// config/config.default.js
// 全局状态缓存文件配置
config.store = {
    // 是否开启
    enable: true,
    client: {
        // 文件名
        name: 'store'
        // 保存路径
        path: './',
    },
}

使用

上下文调

// app/controller/home.js
class HomeController extends Controller {
    
    async connect() {
        const { ctx } = this;

        // 创建
        ctx.callContext.create('serviceName', {
            echo(a, b) {
                // 销毁
                ctx.callContext.destroy('serviceName');
                return a+b;
            }
        })
    }


    async echo() {
        const { ctx } = this;
        // 调用serviceName的echo方法,超时(15000ms)
        ctx.callContext.requrest('serviceName', 15000).echo(1,2).then(res => {
            console.log(res) // 3
        }).catch(err => {
            consoel.log(err.message);
        })
    }

}

全局状态

const store = this.app.store;

/**
 * 设置
 * @param {string} key 主键
 * @param {any} value 值
 * @param {number} [ttl=Infinity] 过期时间(秒), 默认不过期
 */
await store.set('key', 123456, 50);

/**
 * 读取
 * @param {string} key 主键
 */
await store.get(key)

/**
 * 是否存在
 * @param {string} key 主键
 * @returns {boolean}
 */
await store.has(key)

/**
 * 删除
 * @param {string} key 主键
 * @returns {void}
 */
await store.delete(key)

/**
 * 清空
 * @returns {void}
 */
await store.clear()
1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago