Comments (6)
I ran into this recently when tracing down memory leaks in our GraphQL server, where memory use appeared to be linearly increasing with the number of requests. We were creating new GrpcTransport
s for every request in order to attach authorization information to them.
I ended up writing a function to wrap the transport for each incoming request context:
// Wrap some existing RpcTransport to provide default call options.
const transportWithDefaults = (
t: RpcTransport,
callOpts: Partial<RpcOptions>
): RpcTransport => {
return {
mergeOptions: options => t.mergeOptions(options),
unary: (method, input, options) =>
t.unary(method, input, mergeRpcOptions(callOpts, options)),
serverStreaming: (method, input, options) =>
t.serverStreaming(method, input, mergeRpcOptions(callOpts, options)),
clientStreaming: (method, options) =>
t.clientStreaming(method, mergeRpcOptions(callOpts, options)),
duplex: (method, options) =>
t.duplex(method, mergeRpcOptions(callOpts, options)),
};
};
Usage:
const transport = new GrpcTransport({ ... });
// for each incoming request – client creation is cheap
const myClient = new MyClient(transportWithDefaults(transport, {
meta: {
authorization: 'Bearer somevalue',
}
}));
from protobuf-ts.
From the manual:
First,
protobuf-ts
generates the following interface:export interface IHaberdasherClient { makeHat(request: Size, options?: RpcOptions): UnaryCall<Size, Hat>; }As you can see, a method with a "lowerCamelCase" name is generated. It takes
two arguments:
- "request" - this is the input type of your RPC. The message to send
to the server.- "options" - RpcOptions for this call. Options can include
authentication information, for example.
Have you tried this? Are you looking for something else?
from protobuf-ts.
RpcOptions can be specified either when creating the RpcTransport or directly with the call
This would be the second option via call, right? I'm looking for an option to add default metadata to every call of a client. This can be achieved with RpcTransport but as said, I want to reuse the RpcTransport-Instance. So it would be great, to pass options to the Client-Stub as second parameter, e.g.
const client = new HaberdasherClient(transport, {
meta: {
// add general meta data, e.g. bearer token of request scope
}
})
from protobuf-ts.
I see. That's not possible. Would certainly be nice for some use cases, but it would also complicate code gen for clients.
Interceptors might be an option for your use case:
https://github.com/timostamm/protobuf-ts/blob/main/MANUAL.md#rpc-options
https://github.com/timostamm/protobuf-ts/blob/main/packages/runtime-rpc/src/rpc-interceptor.ts
from protobuf-ts.
Then I would add this as a feature request. Maybe it will get some other people's support.
In the architecture of micro services, you have actually very often an incoming request on one side and an outgoing request on the other. The incoming request has a context such as the token of the client that originally made the request - this is what I mean with request scope. Creating one RcpTransport per incoming request just doesn't seem right to me, as already said there is still a lot of initialization going on (see here and here). Channel overwrite may be an option - that's also a bit cumbersome but I'll probably do this for now.
from protobuf-ts.
@mikberg I already thought about something similiar, but wanted to keep the complexity low so I decided to just pass the options to every rpc for now. But that actually looks simpler than I thought, maybe I'll take it over. Thanks.
@timostamm It looks like you're currently working on other projects addressing the same purpose - protobuf-es and connect-es. Maybe you can put this requirement into your considerations there. Since I don't think it will be accepted in this project, I will close this issue. Thanks.
from protobuf-ts.
Related Issues (20)
- unused parameters in the empty.ts are causing TypeScript check errors HOT 6
- Global `Object` is not referenced when there is a message named Object. HOT 2
- Proto2 required fields are not serialized when they are the default value HOT 3
- Is this compiling error could be turned off by Typescript or Option in protobuf-ts HOT 2
- 2.9.2 not working with 'exactOptionalPropertyTypes: true' HOT 1
- Release 2.9.3 breaks serialization for me HOT 6
- Generated message classes cannot be subclassed because of missing 'export' HOT 1
- Error decoding from binary: "cant skip wire type 4" HOT 1
- How to debug INVALID_ARGUMENT? HOT 1
- Missing headers on RPCError HOT 8
- rpcError cannot support Chinese
- [Request] Flag to handle google wrapper types HOT 1
- Vite cannot found module HOT 2
- Invalid typescript in descriptor.ts HOT 2
- Can't retry request after awaiting response HOT 2
- Typesafe API to retrieve enums from strings HOT 4
- `output_javascript` no longer emits `.d.ts` files HOT 6
- Request: Support for moduleResolution "nodenext" and "node16" HOT 2
- Fails to download v27.0-rc1 (which was just released) HOT 7
- invalid url for latest v27.0-rc1 release HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf-ts.