DirectClient<T>

Abstract client that calls a controller directly in the same memory space.

Implements: IConfigurable, IReferenceable, IOpenable

Description

The DirectClientclass allows you to create clients that call a controller directly in the same memory space.

Important points

  • It is used when multiple microservices are deployed in a single container (monolyth) and communication between them can be done by direct calls rather than through the network.

Configuration parameters

  • dependencies:
    • controller: override controller descriptor

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements
  • *:traces:*:*:1.0 - (optional) ITracer components to record traces
  • *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve a connection

Fields

_controller

The controller reference.

protected _controller: T

_opened

The open flag.

protected _opened: boolean = True

_logger

The logger.

protected _logger: CompositeLogger = CompositeLogger()

_counters

The performance counters

protected _counters: CompositeCounters = CompositeCounters()

_dependencyResolver

The dependency resolver used to get the controller’s reference.

protected _dependencyResolver: DependencyResolver = DependencyResolver()

Instance methods

close

Closes a component and frees used resources.

public close(context: IContext): Promise<void>

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

configure

Configures component by passing configuration parameters.

public configure(config: ConfigParams): void

  • config: ConfigParams - configuration parameters to be set.

instrument

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

protected instrument(context: IContext, name: string): InstrumentTiming

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

isOpen

Checks if the component is open.

public isOpen(): boolean

  • returns: boolean - True if the component has been opened and False otherwise.

open

Opens the component.

public open(context: IContext): Promise<void>

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

setReferences

Sets references to dependent components.

public setReferences(references: IReferences): void

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

Examples

class MyDirectClient extends DirectClient<IMyService> implements IMyClient {
    public constructor() {
      super();
      this._dependencyResolver.put('service', new Descriptor(
          "mygroup", "service", "*", "*", "*"));
    }
    ...
    public async getData(context: IContext, id: string): Promise<MyData> {
      let timing = this.instrument(context, 'myclient.get_data');
      try {
        let res = await this._service.getData(context, id);
        timing.endTiming();
        return res;
      } catch (ex) {
        timing.endFailure(ex);
      }
    }
    ...
}

let client = new MyDirectClient();
client.setReferences(References.fromTuples(
    new Descriptor("mygroup","service","default","default","1.0"), service
));

let result = await client.getData(new Context(), "1");