Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.0 #2

Merged
merged 31 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
216039b
feat: refactor init
chakhsu Dec 18, 2023
e68eb50
feat: refactor core
chakhsu Jan 4, 2024
e579e28
fix: export types
chakhsu Jan 4, 2024
6edfeaf
feat: make it run
chakhsu Jan 4, 2024
abaf142
feat: add example
chakhsu Jan 4, 2024
8ad3593
fix: allow no args
chakhsu Jan 4, 2024
0e6f287
feat: set deadline
chakhsu Jan 4, 2024
fd80765
chore: rename x-service
chakhsu Jan 4, 2024
eaa25f8
chore: adjust make credentials type
chakhsu Jan 4, 2024
30fa1f7
fix: adjust remove
chakhsu Jan 4, 2024
484af65
chore: adjust helloword console
chakhsu Jan 4, 2024
abd5b61
feat: support client middleware
chakhsu Jan 5, 2024
4d664cf
feat: stream client support middleware
chakhsu Jan 5, 2024
4992c56
feat: new context for client and server
chakhsu Jan 6, 2024
babb013
feat: remove lodash and loader.message
chakhsu Jan 6, 2024
ea7f9c5
feat: add loader test
chakhsu Jan 6, 2024
70ea0b3
feat: change to commonjs
chakhsu Jan 6, 2024
8b2014b
feat: status support
chakhsu Jan 6, 2024
8e12f45
feat: allow to add new record to context
chakhsu Jan 6, 2024
6f3113b
feat: add peer
chakhsu Jan 6, 2024
417854c
feat: add more proxy type
chakhsu Jan 6, 2024
102e6b9
feat: improve middleware context
chakhsu Jan 7, 2024
74245aa
feat: add protoloader & unarycall test
chakhsu Jan 7, 2024
7d4f3d1
chore: update version to beta
chakhsu Jan 7, 2024
8951ff6
chore: adjust
chakhsu Jan 7, 2024
abfd886
feat: add async stream test
chakhsu Jan 7, 2024
8040418
chore: adjust readme to v2
chakhsu Jan 7, 2024
868c5a7
feat: add benchmark
chakhsu Jan 8, 2024
8fceab9
feat: release v2.0.0
chakhsu Jan 8, 2024
7f74ec3
chore: adjust deps
chakhsu Jan 8, 2024
b72f8f9
test: remove code expect
chakhsu Jan 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: npm
directory: /
schedule:
interval: monthly
open-pull-requests-limit: 15
versioning-strategy: widen
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fail-fast: false
matrix:
node-version: [16.x, 18.x, 20.x]
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, macos-latest]

steps:
- name: Checkout master
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: publish

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout master
uses: actions/checkout@v4

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
cache: 'pnpm'

- name: Install Dependencies
run: pnpm i

- name: Build
run: pnpm build

- name: Publish
run: pnpm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxSingleQuote": true,
"printWidth": 200,
"printWidth": 150,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
Expand Down
40 changes: 15 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# gRPCity ![build-status](https://github.com/chakhsu/grpcity/actions/workflows/tests.yml/badge.svg) ![npm](https://img.shields.io/npm/v/grpcity) ![license](https://img.shields.io/npm/l/grpcity) ![code-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg)
# gRPCity ![build-status](https://github.com/chakhsu/grpcity/actions/workflows/build.yml/badge.svg) ![npm](https://img.shields.io/npm/v/grpcity) ![license](https://img.shields.io/npm/l/grpcity)

[English](./README.md) | [简体中文](./README_CN.md)

Expand All @@ -17,33 +17,26 @@ numerous advanced features to meet the needs of most development scenarios.

Here is the feature:

- **API**: The communication protocol is based on gRPC and defined using
Protobuf.
- **Protobuf**: Supports only dynamic loading of pb, simplifying the loading
process of pb files.
- **Client**: Configured once and can be called anytime, anywhere, supporting
multi-server invocation.
- **Server**: Simplifies the initialization process, starting the server in
three steps, supporting multi-server startup.
- **API**: Communication protocol is based on gRPC and defined through Protobuf.
- **Protobuf**: Supports only dynamic load, simplifying the loading process of protobuf files.
- **Client**: Configured once, callable anytime, anywhere, and supports multi-server invocation.
- **Server**: Simplifies the initialization process with a three-step start, supporting multi-server deployment.
- **Credentials**: Complete support for certificate loading on both the client and server, providing communication encryption capabilities.
- **No-Route**: No routing, RPC is inherently bound to methods.
- **Middleware**: Integrates middleware mechanism similar to Koa, providing pre
and post-processing capabilities for RPC.
- **Middleware**: Both client and server support middleware.
- **Metadata**: Standardizes the transmission and retrieval of metadata.
- **Error**: Provides dedicated Error objects to ensure targeted handling of
exceptions after catching.
- **Promise**: Supports promisify internally in RPC methods while also
preserving callbackify.
- **Config**: Aligned with official configurations, supports pb load
configuration and gRPC channel configuration.
- **Pattern**: Singleton pattern ensures the uniqueness of instance objects.
- **Typescript**: Supported, ensuring compatibility between TS and JS.
- **Config**: Aligned with official configurations, supports protobuf load configurations and gRPC channel configurations.
- **Typescript**: Implemented purely in TypeScript with comprehensive types.

...and a lot more.

---

View full documentation and examples on
[grpcity.js.org](https://grpcity.js.org).
View full documentation and examples on [grpcity.js.org](https://grpcity.js.org).

## Quick Start

Expand Down Expand Up @@ -78,8 +71,6 @@ Next, create `loader.js` and write the following code in it:
```js
import GrpcLoader from 'grpcity'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))

export default new GrpcLoader({
location: path.join(__dirname, './'),
Expand All @@ -106,8 +97,8 @@ class Greeter {
const start = async (addr) => {
await loader.init()

const server = loader.initServer()
server.addService('helloworld.Greeter', new Greeter())
const server = await loader.initServer()
server.add('helloworld.Greeter', new Greeter())

await server.listen(addr)
console.log('gRPC Server is started: ', addr)
Expand All @@ -126,13 +117,13 @@ import loader from './loader.js'
const start = async (addr) => {
await loader.init()

await loader.initClients({
const clients = await loader.initClients({
services: {
'helloworld.Greeter': addr
}
})

const client = loader.client('helloworld.Greeter')
const client = clients.get('helloworld.Greeter')
const result = await client.sayGreet({ message: 'greeter' })
console.log('sayGreet', result.response)
}
Expand All @@ -149,8 +140,7 @@ node ./client.js

---

View full documentation and examples on
[grpcity.js.org](https://grpcity.js.org).
View full documentation and examples on [grpcity.js.org](https://grpcity.js.org).

## License

Expand Down
20 changes: 9 additions & 11 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# gRPCity ![build-status](https://github.com/chakhsu/grpcity/actions/workflows/tests.yml/badge.svg) ![npm](https://img.shields.io/npm/v/grpcity) ![license](https://img.shields.io/npm/l/grpcity) ![code-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg)
# gRPCity ![build-status](https://github.com/chakhsu/grpcity/actions/workflows/build.yml/badge.svg) ![npm](https://img.shields.io/npm/v/grpcity) ![license](https://img.shields.io/npm/l/grpcity)

[English](./README.md) | [简体中文](./README_CN.md)

Expand All @@ -17,15 +17,15 @@
- **API**: 通信协议以 gRPC 为基础,通过 Protobuf 进行定义;
- **Protobuf**: 只支持动态 pb 加载,简化了 pb 文件的加载流程;
- **Client**: 一次配置,随时随处调用,支持 multi-server 的调用;
- **Server**: 简化了初始化流程,三步完成服务端的启动,支持 multi-server 的启动;
- **Server**: 简化了初始化流程,三步完成启动,支持 multi-server 的启动;
- **Credentials**: 客户端和服务端完整地支持了证书加载,提供了通信加密的能力;
- **No-Route**: 无路由,rpc 与 method 天生绑定;
- **Middleware**: 集成了跟 Koa 一样中间件机制,得到了 rpc 前后处理的能力
- **Middleware**: 客户端和服务端都支持中间件机制
- **Metadata**: 规范化了元信息的传递和获取;
- **Error**: 提供了专有 Error 对象,保证异常捕捉后可以针对性处理;
- **Promise**: rpc 方法内部支持了 promisify,同时也保留了 callbackify ;
- **Config**: 与官方配置对齐,支持 pb load 配置和 gRPC channel 配置;
- **Pattern**: 单例模式,保证了实例对象的唯一性;
- **Typescript**: 支持,保证了 ts 和 js 的兼容性;
- **Typescript**: 纯 TS 实现,类型齐全。

...还有更多等你发现。

Expand Down Expand Up @@ -66,8 +66,6 @@ message Message {
```js
import GrpcLoader from 'grpcity'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))

export default new GrpcLoader({
location: path.join(__dirname, './'),
Expand All @@ -94,8 +92,8 @@ class Greeter {
const start = async (addr) => {
await loader.init()

const server = loader.initServer()
server.addService('helloworld.Greeter', new Greeter())
const server = await loader.initServer()
server.add('helloworld.Greeter', new Greeter())

await server.listen(addr)
console.log('gRPC Server is started: ', addr)
Expand All @@ -114,13 +112,13 @@ import loader from './loader.js'
const start = async (addr) => {
await loader.init()

await loader.initClients({
const clients = await loader.initClients({
services: {
'helloworld.Greeter': addr
}
})

const client = loader.client('helloworld.Greeter')
const client = clients.get('helloworld.Greeter')
const result = await client.sayGreet({ message: 'greeter' })
console.log('sayGreet', result.response)
}
Expand Down
109 changes: 109 additions & 0 deletions benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Benchmark Test

Tool: https://ghz.sh

### gRPCity

```
node benchmark/server-grpcity.js

ghz --insecure \
--proto ./benchmark/helloworld.proto \
--call helloworld.Greeter/SayHello \
-d '{"name": "grpcity"}' \
-n 10000 \
-c 100 \
0.0.0.0:9099
```

result:

```
Summary:
Count: 10000
Total: 2.02 s
Slowest: 64.17 ms
Fastest: 1.36 ms
Average: 17.22 ms
Requests/sec: 4942.17

Response time histogram:
1.355 [1] |
7.636 [339] |∎∎∎
13.918 [3251] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
20.199 [3887] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
26.480 [1502] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
32.761 [622] |∎∎∎∎∎∎
39.042 [175] |∎∎
45.324 [130] |∎
51.605 [31] |
57.886 [30] |
64.167 [32] |

Latency distribution:
10 % in 9.75 ms
25 % in 12.52 ms
50 % in 15.38 ms
75 % in 20.27 ms
90 % in 26.62 ms
95 % in 31.47 ms
99 % in 44.53 ms

Status code distribution:
[OK] 10000 responses
```

### grpc-js

```
node benchmark/server-grpcjs.js

ghz --insecure \
--proto ./benchmark/helloworld.proto \
--call helloworld.Greeter/SayHello \
-d '{"name": "grpcity"}' \
-n 10000 \
-c 100 \
0.0.0.0:9098
```

result:

```
Summary:
Count: 10000
Total: 2.54 s
Slowest: 68.57 ms
Fastest: 1.46 ms
Average: 21.11 ms
Requests/sec: 3936.49

Response time histogram:
1.458 [1] |
8.169 [297] |∎∎∎
14.881 [1556] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
21.592 [3900] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
28.303 [2702] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
35.014 [1088] |∎∎∎∎∎∎∎∎∎∎∎
41.725 [296] |∎∎∎
48.437 [97] |∎
55.148 [56] |∎
61.859 [2] |
68.570 [5] |

Latency distribution:
10 % in 12.27 ms
25 % in 16.06 ms
50 % in 20.32 ms
75 % in 25.07 ms
90 % in 30.95 ms
95 % in 34.49 ms
99 % in 44.33 ms

Status code distribution:
[OK] 10000 responses
```

### Conclusion

Compared with grpc-js, gRPCity has no loss and almost the same performance.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const GrpcLoader = require('../../lib')
const { ProtoLoader } = require('../lib')
const path = require('path')

const implementation = {
Expand All @@ -8,14 +8,14 @@ const implementation = {
}

const start = async (addr) => {
const loader = new GrpcLoader({
const loader = new ProtoLoader({
location: path.resolve(__dirname, './'),
files: ['helloworld.proto']
})
await loader.init()

const server = loader.initServer()
server.addService('helloworld.Greeter', implementation)
const server = await loader.initServer()
server.add('helloworld.Greeter', implementation)
await server.listen(addr)
console.log('start:', addr)
}
Expand Down
File renamed without changes.
Loading