Class TcpChaosRegistry

java.lang.Object
org.mockserver.mock.action.http.TcpChaosRegistry

public class TcpChaosRegistry extends Object
Process-wide registry of TCP-layer chaos profiles, keyed by host. These profiles are applied by the TcpChaosHandler Netty handler at the raw byte level before HTTP decoding, injecting transport-layer faults such as latency, connection drops, bandwidth throttling, TCP RST, data slicing, and data limits.

The design mirrors ServiceChaosRegistry — a singleton backed by a ConcurrentHashMap, with optional TTL-based auto-expiry, host normalisation (case-insensitive, port-stripped), and lazy eviction on lookup.

State is cleared on server reset (see HttpState.reset()).

Fleet-awareness (G11): when a clustered StateBackend is wired via setStateBackend(StateBackend), mutations are replicated via the backend's crudEntities("chaos-tcp") store, and an InvalidationListener rebuilds the node-local map on remote writes. The get(String) path remains purely node-local. When no backend is set or the backend is not clustered, behaviour is identical to the pre-G11 node-local-only registry.

  • Field Details

  • Constructor Details

    • TcpChaosRegistry

      public TcpChaosRegistry(LongSupplier clock)
  • Method Details

    • getInstance

      public static TcpChaosRegistry getInstance()
    • setStateBackend

      public void setStateBackend(StateBackend backend)
      Wires the clustered state backend for fleet-wide chaos replication. When the backend isClustered(), mutations are replicated via the backend's CRUD entity store, and an InvalidationListener is registered to rebuild the node-local map on remote writes. When the backend is not clustered, this method is a no-op — the registry stays purely node-local.
    • put

      public void put(String host, TcpChaosProfile profile)
      Register (or replace) the TCP chaos profile for the given host with no expiry.
    • put

      public void put(String host, TcpChaosProfile profile, long ttlMillis)
      Register (or replace) the TCP chaos profile for the given host, optionally with a time-to-live after which it auto-expires.
      Parameters:
      ttlMillis - milliseconds until the profile auto-expires; <= 0 means no expiry
    • get

      public TcpChaosProfile get(String host)
      Returns the TCP chaos profile registered for the given host, or null if none (or it has expired — an expired entry is removed lazily here).
    • patch

      public TcpChaosProfile patch(String host, TcpChaosProfile partial)
      Applies JSON Merge Patch semantics to the TCP chaos profile for the given host. Only non-null fields from partial are applied to the existing profile; unset fields in the partial are left unchanged.
    • remove

      public void remove(String host)
      Removes the TCP chaos profile for the given host (no-op if absent).
    • entries

      public Map<String,TcpChaosProfile> entries()
      Returns a snapshot copy of the current, non-expired host to profile mappings.
    • ttlRemainingMillis

      public Map<String,Long> ttlRemainingMillis()
      Returns, for each currently-active registration that carries a TTL, the remaining milliseconds until it auto-reverts.
    • activeCountByFaultType

      public Map<String,Integer> activeCountByFaultType()
      For each fault type, the number of currently-active (non-expired) registrations whose profile includes that fault.
    • activeCount

      public int activeCount()
      Returns the number of non-expired entries.
    • reset

      public void reset()
      Clear all TCP-layer chaos. Called on server reset and for test isolation.
    • reconcileFromBackend

      public void reconcileFromBackend()
      Rebuilds the node-local map from the backend store. Called by the InvalidationListener when a remote write is detected.