-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grpc): initialize default metadata, passing "rpc.method.type"
key: rpc.method.type value: unary|server|client
- Loading branch information
1 parent
437e92d
commit 63e7dc1
Showing
12 changed files
with
313 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/grpc/test/fixtures/base-app-multiple-package/src/config/config.default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/grpc/test/fixtures/base-app-stream-meta/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "ali-demo" | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/grpc/test/fixtures/base-app-stream-meta/src/config/config.default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { join } from 'path'; | ||
|
||
export const grpcServer = { | ||
url: 'localhost:6571', | ||
services: [ | ||
{ | ||
protoPath: join(__dirname, '../../../proto/math.proto'), | ||
package: 'math', | ||
} | ||
], | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/grpc/test/fixtures/base-app-stream-meta/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Configuration } from '@midwayjs/core'; | ||
import * as grpc from '../../../../src'; | ||
import { join } from 'path'; | ||
|
||
@Configuration({ | ||
imports: [ | ||
grpc | ||
], | ||
importConfigs: [ | ||
join(__dirname, './config'), | ||
] | ||
}) | ||
export class AutoConfiguration { | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/grpc/test/fixtures/base-app-stream-meta/src/interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
IClientDuplexStreamService, | ||
IClientReadableStreamService, | ||
IClientUnaryService, | ||
IClientWritableStreamService | ||
} from '../../../../src'; | ||
|
||
export namespace math { | ||
export interface AddArgs { | ||
id?: number; | ||
num?: number; | ||
} | ||
export interface Num { | ||
id?: number; | ||
num?: number; | ||
} | ||
|
||
/** | ||
* server interface | ||
*/ | ||
export interface Math { | ||
add(data: AddArgs): Promise<Num>; | ||
addMore(data: AddArgs): Promise<void>; | ||
// 服务端推,客户端读 | ||
sumMany(fibArgs: AddArgs): Promise<void> | ||
// 客户端端推,服务端读 | ||
addMany(num: AddArgs): Promise<void>; | ||
} | ||
|
||
/** | ||
* client interface | ||
*/ | ||
export interface MathClient { | ||
add(): IClientUnaryService<AddArgs, Num>; | ||
addMore(): IClientDuplexStreamService<AddArgs, Num>; | ||
// 服务端推,客户端读 | ||
sumMany(): IClientReadableStreamService<AddArgs, Num>; | ||
// 客户端端推,服务端读 | ||
addMany(): IClientWritableStreamService<AddArgs, Num>; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
packages/grpc/test/fixtures/base-app-stream-meta/src/provider/math.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as assert from 'assert'; | ||
import { GrpcMethod, GrpcStreamTypeEnum, Inject, MSProviderType, Provide, Provider } from '@midwayjs/core'; | ||
import { Context } from '../../../../../src'; | ||
import { math } from '../interface'; | ||
import { Metadata } from '@grpc/grpc-js'; | ||
|
||
/** | ||
*/ | ||
@Provide() | ||
@Provider(MSProviderType.GRPC, { package: 'math' }) | ||
export class Math implements math.Math { | ||
|
||
@Inject() | ||
ctx: Context; | ||
|
||
sumDataList = []; | ||
|
||
@GrpcMethod() | ||
async add(data: math.AddArgs): Promise<math.Num> { | ||
assert(this.ctx, 'should get context'); | ||
const { metadata } = this.ctx; | ||
assert(metadata, 'should get metadata'); | ||
|
||
const rpcMethodType = metadata.get('rpc.method.type'); | ||
assert(rpcMethodType[0] === 'unary', `should get rpc.method.type, but got "${rpcMethodType[0]}"`); | ||
|
||
return { | ||
num: data.num + 2, | ||
} | ||
} | ||
|
||
@GrpcMethod({type: GrpcStreamTypeEnum.DUPLEX, onEnd: 'duplexEnd' }) | ||
async addMore(message: math.AddArgs) { | ||
const { metadata } = this.ctx; | ||
assert(metadata, 'should get metadata'); | ||
|
||
const rpcMethodType = metadata.get('rpc.method.type'); | ||
assert(rpcMethodType[0] === 'bidi', `should get rpc.method.type, but got "${rpcMethodType[0]}"`); | ||
|
||
this.ctx.write({ | ||
id: message.id, | ||
num: message.num + 10, | ||
}); | ||
} | ||
|
||
async duplexEnd() { | ||
console.log('got client end message'); | ||
} | ||
|
||
@GrpcMethod({type: GrpcStreamTypeEnum.WRITEABLE }) | ||
async sumMany(args: math.AddArgs) { | ||
const { metadata } = this.ctx; | ||
assert(metadata, 'should get metadata'); | ||
|
||
const rpcMethodType = metadata.get('rpc.method.type'); | ||
assert(rpcMethodType[0] === 'server', `should get rpc.method.type, but got "${rpcMethodType[0]}"`); | ||
|
||
this.ctx.write({ | ||
num: 1 + args.num | ||
}); | ||
this.ctx.write({ | ||
num: 2 + args.num | ||
}); | ||
this.ctx.write({ | ||
num: 3 + args.num | ||
}); | ||
|
||
const meta = new Metadata(); | ||
meta.add('xxx', 'bbb'); | ||
|
||
this.ctx.sendMetadata(meta); | ||
this.ctx.end(); | ||
} | ||
|
||
@GrpcMethod({type: GrpcStreamTypeEnum.READABLE, onEnd: 'sumEnd' }) | ||
async addMany(data: math.Num) { | ||
const { metadata } = this.ctx; | ||
assert(metadata, 'should get metadata'); | ||
|
||
const rpcMethodType = metadata.get('rpc.method.type'); | ||
assert(rpcMethodType[0] === 'client', `should get rpc.method.type, but got "${rpcMethodType[0]}"`); | ||
|
||
this.sumDataList.push(data); | ||
} | ||
|
||
async sumEnd(): Promise<math.Num> { | ||
const total = this.sumDataList.reduce((pre, cur) => { | ||
return { | ||
num: pre.num + cur.num, | ||
} | ||
}); | ||
return total; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
packages/grpc/test/fixtures/base-app-stream/src/config/config.default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.