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-mesh

v2.0.19

Published

Master - Branch 形式 Nodejs 微服务框架

Downloads

9

Readme

Master - Branch 形式 Nodejs 微服务框架

一、用法(Usage)

Master 和 Branch 可以是不同文件的不同进程,为了方便示例,都放在同一个文件

1、 Request - Response


import { Master, Branch } from 'node-mesh';

const actions = {
    add: (a: number, b: number): number => {
        return a + b;
    }
};

const actions2 = {
    add2: (a: number, b: number): number => {
        return a + b + 1;
    }
};

const master1 = new Master<typeof actions & typeof actions2>('master1', { port: 3010 });

const branch1 = new Branch<typeof actions & typeof actions2>('branch1', { port: 3010 });

// 批量注册方法
master1.createResponder(actions2);

// 创建请求者
const masterReq = master1.createRequester();

// 批量注册方法
branch1.createResponder(actions);

// 创建请求者
const branchReq = branch1.createRequester();

// 请求
branchReq.add(11, 11).then((result) => {
    console.log('result1', result); // 22
});

// 请求
branchReq.add2(22, 22).then((result) => {
    console.log('result2', result); // 45
});

// 请求
masterReq.add(33, 33).then((result) => {
    console.log('result3', result); // 66
});

// 请求
masterReq.add2(44, 44).then((result) => {
    console.log('result4', result); // 89
});

2、Subscribe - Publish


import { Master, Branch } from 'node-mesh';

const master1 = new Master('master1', { port: 3010 });

const branch1 = new Branch('branch1', { port: 3010, master: 'master1' });

// Subscribe
master1.subscribe('sub/test', (error, content) => {
    console.log(555, content); // content === sub
});

// Publish
branch1.publish('sub/test', 'sub');

二、原理(Design)

1、客户端与服务端绑定(bind)


客户端与服务端建立 connect,客户端发出绑定通知,服务端接收绑定通知,校验返回结果,完成绑定

2、单主分支(branch)


master 启动 -》 branch 建立链接 -》 注册 action 到 master 缓存 -》 通知其他分支上线

分支请求动作 -》 检查自己有没有 action -》 检查有没有缓存 action -》 请求 action

分支下线 -》 清除 master 上的分支 action 缓存 -》 通知其他分支下线

三、错误列表(Error)

export enum CustomErrorCode {
    success = "OK", // 成功
    none = "NONE", // 未知
    requestTimeout = "REQUEST_TIMEOUT", // 请求超时
    bindError = "BIND_ERROR", // 绑定失败
    requestParamsError = "REQUEST_PARAM_ERROR", // 请求参数问题
    bindTimeout = "BIND_TIMEOUT", // 绑定超时
    actionNotExist = "ACTION_NOT_EXIST", // 动作不存在
    actionSocketNotActive = "ACTION_SOCKET_NOT_ACTIVE" // socket不在线或者不存在
}