deeplay-io / nice-grpc

A TypeScript gRPC library that is nice to you

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to set default "deadline" option when using deadlineMiddleware

pygaissert opened this issue · comments

We would like to augment our current usage of nice-grpc by adopting the deadlineMiddleware.

Here is an abridged version of a class we would use for creating and managing our various Service implementations, in which any call by any service client returned by the createClient method would have a default deadline of 30 seconds after invocation.

import { createChannel, createClientFactory, Metadata, RawClient } from 'nice-grpc';
import { deadlineMiddleware, DeadlineOptions } from 'nice-grpc-client-middleware-deadline';
import { NormalizedServiceDefinition } from 'nice-grpc/lib/service-definitions';

// nice-grpc client with support for setting deadlines for calls
export type WrappedClient<Service extends ServiceDefinition> = RawClient<NormalizedServiceDefinition<Service>, DeadlineOptions>;

class WrappedClientManager {

   private metaData: Metadata;
   private address: string;
   // private wrappedClientFactory = createClientFactory();
   private wrappedClientFactory = createClientFactory().use(deadlineMiddleware);
   
   // ...
   
   public createClient<Service extends ServiceDefinition>(definition: Service, metadata?: Metadata | null): WrappedClient<Service> {
      const channel = createChannel(this.address);
      const result = this.wrappedClientFactory.create(definition, channel, {
         ...(metadata === null
            ? {}
            : {
                 '*': {
                    metadata: metadata ?? this.metaData,
                    onTrailer(trailer) {
                       // ...
                    },
                    deadline: 30000 // 30 seconds
                 }
              })
         });
         return result;
   }

However, this gives us the error below, which is particularly confusing because there seems to be a two-way unassignable relation going on between DeadlineOptions ( { deadline?: number | Date;}) and {deadline: number | Date;}

error TS2322: Type 'RawClient<NormalizedServiceDefinition<Service>, { deadline: number | Date; }>' is not assignable to type 'RawClient<NormalizedServiceDefinition<Service>, DeadlineOptions>'.
  Type 'ClientMethod<NormalizedServiceDefinition<Service>[Method], { deadline: number | Date; }>' is not assignable to type 'ClientMethod<NormalizedServiceDefinition<Service>[Method], DeadlineOptions>'.
    Type 'DeadlineOptions' is not assignable to type '{ deadline: number | Date; }'.
   
         return result;

Is it possible to set a default call option for deadline when deadlineMiddleware is in use? I have not found any examples in the documentation nor in other projects.

Appears to work with the following change, but I'm not sure why. This seems redundant.

private wrappedClientFactory = createClientFactory().use<DeadlineOptions>(deadlineMiddleware);

Hmm, I don't get any error with the code you provided. Which version of TypeScript are you using?

TS 4.5.5

And according to yarn.lock:

  • nice-grpc 1.0.6
  • nice-grpc-common 1.1.0
  • nice-grpc-client-middleware-deadline 1.1.2

(each was set to ^1.0.6 in package.json)

I'm going to chalk this up to a mismatch in the versions of nice-grpc-common pulled in by nice-grpc@1.0.6 and nice-grpc-client-middleware-deadline@1.1.2 -- 1.0.4 and 1.1.0 respectively.

Things appear to work as expected when the two resolve their dependency on nice-grpc-common to the same version.