DirectClient

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.

_controller: Any

_opened

The open flag.

_opened: bool = True

_logger

The logger.

_logger: CompositeLogger = CompositeLogger()

_counters

The performance counters

_counters: CompositeCounters = CompositeCounters()

_dependency_resolver

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

_dependency_resolver: DependencyResolver = DependencyResolver()

Instance methods

close

Closes a component and frees used resources.

close(correlation_id: Optional[str])

  • correlation_id: Optional[str] - (optional) transaction id used to trace execution through a call chain.

configure

Configures component by passing configuration parameters.

configure(config: ConfigParams)

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

_instrument(correlation_id: Optional[str], name: str): InstrumentTiming

  • correlation_id: Optional[str] - (optional) transaction id used to trace execution through a call chain.
  • name: str - method name.
  • returns: InstrumentTiming - InstrumentTiming object used to end the time measurement.

is_open

Checks if the component is open.

is_open(): bool

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

open

Opens the component.

open(correlation_id: Optional[str])

  • correlation_id: Optional[str] - (optional) transaction id used to trace execution through a call chain.

set_references

Sets references to dependent components.

set_references(references: IReferences)

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

Examples

class MyDirectClient(DirectClient, IMyClient):
    def __init__(self):
        super(MyDirectClient, self).__init__()
        self._dependencyResolver.put('controller', Descriptor("mygroup", "controller", "*", "*", "*"))

    # ...

    def get_data(self, correlation_id, id):
        timing = self.instrument(correlationId, 'myclient.get_data')
        result = self._controller.get_data(correlationId, id)
        timing.end_timing()
        return result

    client = MyDirectClient()
    client.set_references(References.from_tuples(Descriptor("mygroup","controller","default","default","1.0"), controller))
    data = client.get_data("123", "1")
    # ...