GrpcClient

Abstract client that calls remote endpoints using the gRPC protocol.

Implements: IOpenable, IReferenceable, IConfigurable

Description

The GrpcClient class allows you to create clients that call remote endpoints using the gRPC protocol.

Configuration parameters

  • connection(s):
    • discovery_key: (optional) key to retrieve the connection from IDiscovery
    • protocol: connection protocol: http or https
    • host: host name or IP address
    • port: port number
    • uri: resource URI or connection string with all parameters in it
  • options:
    • retries: number of retries (default: 3)
    • connect_timeout: connection timeout in milliseconds (default: 10 sec)
    • timeout: invocation timeout in milliseconds (default: 10 sec)

Constructors

Creates a new instance of the gRPC client.

GrpcClient(String clientName)

  • clientName: String - client’s name.

Fields

_channel

gRPC client.

_channel: grpc.ClientChannel?

_connectionResolver

Connection resolver.

_connectionResolver: HttpConnectionResolver

_logger

Logger.

_logger: CompositeLogger

_counters

Performance counters.

_counters: CompositeCounters

_options

Configuration options.

_options: ConfigParams

_connectTimeout

Connection timeout in milliseconds.

_connectTimeout: int = 100000

_timeout

Invocation timeout in milliseconds.

_timeout: int = 100000

_tracer

The tracer.

_tracer: CompositeTracer

_uri

Remote service URI which is calculated on openning.

_uri: String?

Instance methods

addFilterParams

AddFilterParams method are adds filter parameters (with the same name as they defined) to invocation parameter map.

StringValueMap addFilterParams(StringValueMap? params, FilterParams? filter)

addPagingParams

AddPagingParams method are adds paging parameters (skip, take, total) to invocation parameter map.

StringValueMap addPagingParams(StringValueMap? params, PagingParams? paging)

call

Calls a remote method via gRPC protocol.

grpc.ResponseFuture<R>call<Q extends GeneratedMessage, R extends GeneratedMessage>(String method, IContext? context, Q request, {grpc.CallOptions? options})

  • method: String - name of the calling method
  • context: IContext? - current client
  • request: Q - (optional) request object.
  • options: grpc.CallOptions? - (optional) call options
  • returns: grpc.ResponseFuture<R> - (optional) Future that receives result object or error.

close

Closes the component and frees used resources.

@override

Future close(IContext? context)

  • context: IContext - (optional) a context to trace execution through a call chain.

configure

Configures the component by passing its configuration parameters.

@override

void configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

instrument

Adds instrumentation to log calls and measure call time. It returns a CounterTiming object that is used to end the time measurement.

InstrumentTiming instrument(IContext? context, String name)

  • context: IContext - (optional) a context to trace execution through a call chain.
  • name: String - method name.
  • returns: InstrumentTiming - CounterTiming object used to end the time measurement.

isOpen

Checks if the component is open.

@override

bool isOpen()

  • returns: bool - true if the component is open and false otherwise.

open

Opens the component.

@override

Future open(IContext? context)

  • context: IContext - (optional) a context to trace execution through a call chain.

setReferences

Sets references to dependent components.

@override

void setReferences(IReferences references)

  • references: IReferences - references to locate the component dependencies.

Examples

class MyGrpcClient extends GrpcClient implements IMyClient {
   ...
   Future<MyData> getData(IContext? context, string id) async {
       var counter_timing = this.instrument(context, 'myclient.get_data');
       var request = MyDataRequest();
       request.id = id;
       var response = await call<MydataRequest,MyDataResponse>('get_data', context, request)
       counter_timing.endTiming();
       MyData item;
       ///... convert MyDataResponse to MyData
       return item;
   }
   ...
}
var client = MyGrpcClient();
client.configure(ConfigParams.fromTuples([
    'connection.protocol', 'http',
    'connection.host', 'localhost',
    'connection.port', 8080
]));

var item = await client.getData('123', '1')
  ...