Giter Site home page Giter Site logo

Comments (6)

mikberg avatar mikberg commented on June 14, 2024 1

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 GrpcTransports 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.

timostamm avatar timostamm commented on June 14, 2024

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:

  1. "request" - this is the input type of your RPC. The message to send
    to the server.
  2. "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.

awltr avatar awltr commented on June 14, 2024

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.

timostamm avatar timostamm commented on June 14, 2024

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.

awltr avatar awltr commented on June 14, 2024

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.

awltr avatar awltr commented on June 14, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.