Class: Nonnative::FaultInjectionProxy

Inherits:
Proxy
  • Object
show all
Defined in:
lib/nonnative/fault_injection_proxy.rb

Overview

Fault-injection proxy for TCP services.

This proxy accepts incoming TCP connections and forwards traffic to the configured upstream (service.proxy.host / service.proxy.port) via a socket-pair implementation. It can also inject failures to help validate client resilience.

This class exposes a small public control surface for tests:

  • #close_all: close connections immediately on accept
  • #reset_peer: reset connections immediately on accept (TCP RST rather than a graceful close)
  • #delay: delay reads by a configured duration (default: 2 seconds)
  • #timeout: accept connections and keep them silent until clients time out
  • #invalid_data: forward requests unchanged and mutate upstream responses before they reach clients
  • #bandwidth: throttle forwarded throughput to a configured rate (KB/s)
  • #limit_data: forward a configured number of response bytes, then gracefully close
  • #reset: return to healthy pass-through behavior

State changes terminate any active connections so new connections observe the new behavior.

Wiring

When enabled, your test/client should connect to the service host and port (the proxy endpoint), and the proxy will forward traffic to the upstream target exposed by #host:#port.

Configuration

The proxy is configured via the service's proxy hash:

  • kind: "fault_injection"
  • host / port: upstream target behind the proxy (exposed via #host/#port)
  • log: file path used by this proxy’s internal logger
  • wait: sleep interval (seconds) applied after state changes
  • options:
    • delay: delay duration in seconds used by #delay
    • jitter: optional random offset (seconds) added in -jitter..jitter to each delay (a negative value uses its magnitude), so clients see variable latency instead of a flat value
    • bytes: positive response byte limit used by #limit_data; absent or non-positive values use pass-through behavior

Defined Under Namespace

Classes: Connection

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ FaultInjectionProxy

Returns a new instance of FaultInjectionProxy.

Parameters:



62
63
64
65
66
67
68
69
# File 'lib/nonnative/fault_injection_proxy.rb', line 62

def initialize(service)
  @connections = Concurrent::Hash.new
  @logger = Logger.new(service.proxy.log)
  @mutex = Mutex.new
  @state = :none

  super
end

Instance Method Details

#bandwidthvoid

This method returns an undefined value.

Throttles forwarded throughput to a configured rate.

The rate is controlled by service.proxy.options[:rate] (kilobytes per second); when it is absent or not positive the connection forwards at full speed.



152
153
154
# File 'lib/nonnative/fault_injection_proxy.rb', line 152

def bandwidth
  apply_state :bandwidth
end

#close_allvoid

This method returns an undefined value.

Forces new connections to be closed immediately.



105
106
107
# File 'lib/nonnative/fault_injection_proxy.rb', line 105

def close_all
  apply_state :close_all
end

#delayvoid

This method returns an undefined value.

Delays reads before forwarding.

The delay duration is controlled by service.proxy.options[:delay] and defaults to 2 seconds.



124
125
126
# File 'lib/nonnative/fault_injection_proxy.rb', line 124

def delay
  apply_state :delay
end

#hostString

Returns the upstream host behind this proxy.

Returns:

  • (String)


177
178
179
# File 'lib/nonnative/fault_injection_proxy.rb', line 177

def host
  service.proxy.host
end

#invalid_datavoid

This method returns an undefined value.

Mutates upstream responses while forwarding client requests unchanged.



142
143
144
# File 'lib/nonnative/fault_injection_proxy.rb', line 142

def invalid_data
  apply_state :invalid_data
end

#limit_datavoid

This method returns an undefined value.

Truncates the upstream byte stream after a configured number of bytes.

Client requests are forwarded unchanged. The response byte limit is read from service.proxy.options[:bytes]; when it is absent or not positive, the connection forwards at full speed without truncation.



163
164
165
# File 'lib/nonnative/fault_injection_proxy.rb', line 163

def limit_data
  apply_state :limit_data
end

#portInteger

Returns the upstream port behind this proxy.

Returns:

  • (Integer)


184
185
186
# File 'lib/nonnative/fault_injection_proxy.rb', line 184

def port
  service.proxy.port
end

#resetvoid

This method returns an undefined value.

Resets the proxy back to healthy pass-through behavior.



170
171
172
# File 'lib/nonnative/fault_injection_proxy.rb', line 170

def reset
  apply_state :none
end

#reset_peervoid

This method returns an undefined value.

Forces new connections to be reset immediately.

Unlike #close_all, which closes the socket gracefully (FIN), this closes the accepted socket with a zero linger timeout so clients observe a TCP reset (Errno::ECONNRESET).



115
116
117
# File 'lib/nonnative/fault_injection_proxy.rb', line 115

def reset_peer
  apply_state :reset_peer
end

#startvoid

This method returns an undefined value.

Starts the proxy accept loop in a background thread.

This binds a TCP server on the service host and port. Clients connect to that service endpoint, while upstream traffic is forwarded to #host:#port.



77
78
79
80
81
82
# File 'lib/nonnative/fault_injection_proxy.rb', line 77

def start
  @tcp_server = ::TCPServer.new(service.host, service.port)
  @thread = Thread.new { perform_start }

  Nonnative.logger.info "started with host '#{service.host}' and port '#{service.port}' for proxy 'fault_injection'"
end

#stopvoid

This method returns an undefined value.

Stops the proxy, closes active connections, and closes its listening socket.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nonnative/fault_injection_proxy.rb', line 87

def stop
  server = tcp_server
  server&.close

  listener_thread = thread
  listener_thread&.join

  @tcp_server = nil
  @thread = nil

  close_connections

  Nonnative.logger.info "stopped with host '#{service.host}' and port '#{service.port}' for proxy 'fault_injection'"
end

#timeoutvoid

This method returns an undefined value.

Accepts connections and stalls without forwarding bytes.

This simulates a dependency that accepts a TCP connection but leaves clients waiting until their own read timeout fires. The proxy keeps the connection silent until reset or stop closes active connections.



135
136
137
# File 'lib/nonnative/fault_injection_proxy.rb', line 135

def timeout
  apply_state :timeout
end