Class NetworkLatencyStrategyFactory

All Implemented Interfaces:
MoleculerLifecycle

public class NetworkLatencyStrategyFactory extends ArrayBasedStrategyFactory
Factory of lowest network latency strategy. This strategy comes from a random strategy, but preferably communicates with the "closest" nodes (nodes with the lowest response/ping time).
See Also:
  • Field Details

    • sampleCount

      protected int sampleCount
      This strategy compares number of 'sampleCount' random node.
    • pingInterval

      protected int pingInterval
      Ping period time, in SECONDS.
    • pingTimeout

      protected long pingTimeout
      Ping timeout time, in MILLISECONDS.
    • collectCount

      protected int collectCount
      Number of samples used for average calculation.
    • scheduler

      protected ScheduledExecutorService scheduler
    • transporter

      protected Transporter transporter
    • nodeID

      protected String nodeID
    • responseTimes

    • pingTimer

      protected volatile ScheduledFuture<?> pingTimer
      Cancelable timer of ping loop
    • previousNodeID

      protected volatile String previousNodeID
  • Constructor Details

    • NetworkLatencyStrategyFactory

      public NetworkLatencyStrategyFactory()
      Constructor that calls for service based primarily on its own (latency-based) logic. Does not matter if the service is available locally.
    • NetworkLatencyStrategyFactory

      public NetworkLatencyStrategyFactory(boolean preferLocal)
      Constructor that can be configured to use local services if possible.
      Parameters:
      preferLocal - invoke local actions if possible
  • Method Details

    • started

      public void started(ServiceBroker broker) throws Exception
      Initializes strategy instance.
      Specified by:
      started in interface MoleculerLifecycle
      Overrides:
      started in class MoleculerComponent
      Parameters:
      broker - parent ServiceBroker
      Throws:
      Exception
    • stopped

      public void stopped()
      Specified by:
      stopped in interface MoleculerLifecycle
      Overrides:
      stopped in class MoleculerComponent
    • sendNextPing

      protected void sendNextPing()
    • sendPing

      protected void sendPing(String nextNodeID)
    • getAverageResponseTime

      protected long getAverageResponseTime(String nextNodeID)
    • create

      public <T extends Endpoint> Strategy<T> create()
      Specified by:
      create in class StrategyFactory
    • getSampleCount

      public int getSampleCount()
    • setSampleCount

      public void setSampleCount(int sampleCount)
    • getPingInterval

      public int getPingInterval()
    • setPingInterval

      public void setPingInterval(int pingInterval)
    • getPingTimeout

      public long getPingTimeout()
    • setPingTimeout

      public void setPingTimeout(long pingTimeout)
    • getCollectCount

      public int getCollectCount()
    • setCollectCount

      public void setCollectCount(int collectCount)