Class: Nonnative::FaultInjectionProxy
- Inherits:
-
Proxy
- Object
- Proxy
- Nonnative::FaultInjectionProxy
- 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
- #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
- #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:
Defined Under Namespace
Classes: Connection
Instance Method Summary collapse
-
#close_all ⇒ void
Forces new connections to be closed immediately.
-
#delay ⇒ void
Delays reads before forwarding.
-
#host ⇒ String
Returns the upstream host behind this proxy.
-
#initialize(service) ⇒ FaultInjectionProxy
constructor
A new instance of FaultInjectionProxy.
-
#invalid_data ⇒ void
Mutates upstream responses while forwarding client requests unchanged.
-
#port ⇒ Integer
Returns the upstream port behind this proxy.
-
#reset ⇒ void
Resets the proxy back to healthy pass-through behavior.
-
#start ⇒ void
Starts the proxy accept loop in a background thread.
-
#stop ⇒ void
Stops the proxy, closes active connections, and closes its listening socket.
-
#timeout ⇒ void
Accepts connections and stalls without forwarding bytes.
Constructor Details
#initialize(service) ⇒ FaultInjectionProxy
Returns a new instance of FaultInjectionProxy.
55 56 57 58 59 60 61 62 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 55 def initialize(service) @connections = Concurrent::Hash.new @logger = Logger.new(service.proxy.log) @mutex = Mutex.new @state = :none super end |
Instance Method Details
#close_all ⇒ void
This method returns an undefined value.
Forces new connections to be closed immediately.
98 99 100 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 98 def close_all apply_state :close_all end |
#delay ⇒ void
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.
107 108 109 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 107 def delay apply_state :delay end |
#host ⇒ String
Returns the upstream host behind this proxy.
139 140 141 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 139 def host service.proxy.host end |
#invalid_data ⇒ void
This method returns an undefined value.
Mutates upstream responses while forwarding client requests unchanged.
125 126 127 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 125 def invalid_data apply_state :invalid_data end |
#port ⇒ Integer
Returns the upstream port behind this proxy.
146 147 148 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 146 def port service.proxy.port end |
#reset ⇒ void
This method returns an undefined value.
Resets the proxy back to healthy pass-through behavior.
132 133 134 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 132 def reset apply_state :none end |
#start ⇒ void
70 71 72 73 74 75 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 70 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 |
#stop ⇒ void
This method returns an undefined value.
Stops the proxy, closes active connections, and closes its listening socket.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 80 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 |
#timeout ⇒ void
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.
118 119 120 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 118 def timeout apply_state :timeout end |