Module: Nonnative

Defined in:
lib/nonnative.rb,
lib/nonnative/pool.rb,
lib/nonnative/port.rb,
lib/nonnative/error.rb,
lib/nonnative/proxy.rb,
lib/nonnative/header.rb,
lib/nonnative/runner.rb,
lib/nonnative/server.rb,
lib/nonnative/process.rb,
lib/nonnative/service.rb,
lib/nonnative/timeout.rb,
lib/nonnative/version.rb,
lib/nonnative/no_proxy.rb,
lib/nonnative/go_command.rb,
lib/nonnative/stop_error.rb,
lib/nonnative/grpc_server.rb,
lib/nonnative/http_client.rb,
lib/nonnative/http_server.rb,
lib/nonnative/socket_pair.rb,
lib/nonnative/start_error.rb,
lib/nonnative/configuration.rb,
lib/nonnative/observability.rb,
lib/nonnative/proxy_factory.rb,
lib/nonnative/not_found_error.rb,
lib/nonnative/delay_socket_pair.rb,
lib/nonnative/http_proxy_server.rb,
lib/nonnative/configuration_proxy.rb,
lib/nonnative/socket_pair_factory.rb,
lib/nonnative/configuration_runner.rb,
lib/nonnative/configuration_server.rb,
lib/nonnative/close_all_socket_pair.rb,
lib/nonnative/configuration_process.rb,
lib/nonnative/configuration_service.rb,
lib/nonnative/fault_injection_proxy.rb,
lib/nonnative/invalid_data_socket_pair.rb

Overview

Sinatra-based HTTP forward proxy server used as an in-process Nonnative server.

The proxy receives inbound HTTP requests and forwards them to an upstream host over HTTPS, returning the upstream response status and body.

This file defines two classes:

Notes:

See Also:

Defined Under Namespace

Classes: CloseAllSocketPair, Configuration, ConfigurationProcess, ConfigurationProxy, ConfigurationRunner, ConfigurationServer, ConfigurationService, DelaySocketPair, Error, FaultInjectionProxy, GRPCServer, GoCommand, HTTPClient, HTTPProxy, HTTPProxyServer, HTTPServer, Header, InvalidDataSocketPair, NoProxy, NotFoundError, Observability, Pool, Port, Process, Proxy, ProxyFactory, Runner, Server, Service, SocketPair, SocketPairFactory, StartError, StopError, Timeout

Constant Summary collapse

VERSION =

The current gem version.

Returns:

  • (String)
'1.108.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.poolNonnative::Pool? (readonly)

Returns the current runner pool (created on start).

Returns:



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

def pool
  @pool
end

Class Method Details

.clearvoid

This method returns an undefined value.

Clears memoized configuration and pool.



255
256
257
258
# File 'lib/nonnative.rb', line 255

def clear
  clear_configuration
  clear_pool
end

.clear_configurationvoid

This method returns an undefined value.

Clears the memoized configuration instance.



241
242
243
# File 'lib/nonnative.rb', line 241

def clear_configuration
  @configuration = nil
end

.clear_poolvoid

This method returns an undefined value.

Clears the memoized pool instance.



248
249
250
# File 'lib/nonnative.rb', line 248

def clear_pool
  @pool = nil
end

.configurationNonnative::Configuration

Returns the current configuration (memoized).



130
131
132
# File 'lib/nonnative.rb', line 130

def configuration
  @configuration ||= Nonnative::Configuration.new
end

.configurations(*files) ⇒ Config::Options

Loads one or more configuration files using the config gem.

This is primarily used by Nonnative::Configuration#load_file, but is public for advanced cases.

Parameters:

  • files (Array<String>)

    paths to configuration files

Returns:

  • (Config::Options)

    the loaded configuration object



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

def configurations(*files)
  Config.load_files(files)
end

.configure {|config| ... } ⇒ void

This method returns an undefined value.

Yields the configuration to a block for programmatic setup.

Examples:

Nonnative.configure do |config|
  config.name = 'my-service'
  # ...
end

Yield Parameters:



144
145
146
# File 'lib/nonnative.rb', line 144

def configure
  yield configuration
end

.go_executable(tools, output, exec, cmd, *params) ⇒ String

Builds a Go test executable command line with optional profiling/trace/coverage flags.

This is used when process configuration specifies a go section.

Parameters:

  • tools (Array<String>)

    enabled tool names (e.g. ‘[“prof”, “trace”, “cover”]`)

  • output (String)

    directory where outputs should be written

  • exec (String)

    the test binary (or wrapper) to execute

  • cmd (String)

    the command argument passed to the test binary

  • params (Array<String>)

    extra parameters for the command

Returns:

  • (String)

    executable command string



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

def go_executable(tools, output, exec, cmd, *params)
  Nonnative::GoCommand.new(tools, exec, output).executable(cmd, params)
end

.log_lines(path, predicate) ⇒ Array<String>

Reads a file and returns only lines matching the given predicate.

Parameters:

  • path (String)

    file path to read

  • predicate (#call)

    callable that receives a line and returns truthy/falsey

Returns:

  • (Array<String>)

    matching lines



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

def log_lines(path, predicate)
  File.readlines(path).select { |l| predicate.call(l) }
end

.loggerLogger

Returns the gem logger (memoized).

The logger writes to the path configured at Nonnative::Configuration#log.

Returns:

  • (Logger)


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

def logger
  @logger ||= Logger.new(configuration.log)
end

.observabilityNonnative::Observability

Returns an HTTP client for common health/readiness endpoints.



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

def observability
  @observability ||= Nonnative::Observability.new(configuration.url)
end

.proxiesHash{String=>Class}

Returns the configured proxy kinds mapped to proxy classes.

Consumers can extend this map to add custom proxy implementations.

Returns:

  • (Hash{String=>Class})


192
193
194
# File 'lib/nonnative.rb', line 192

def proxies
  @proxies ||= { 'fault_injection' => Nonnative::FaultInjectionProxy }.freeze
end

.proxy(kind) ⇒ Class

Resolves a proxy implementation for a configured kind.

Parameters:

  • kind (String)

    proxy kind name (for example ‘“fault_injection”`)

Returns:

  • (Class)

    a subclass of Proxy



200
201
202
# File 'lib/nonnative.rb', line 200

def proxy(kind)
  Nonnative.proxies[kind] || Nonnative::NoProxy
end

.resetvoid

This method returns an undefined value.

Resets proxies for all currently started runners.

Raises:

  • (NoMethodError)

    if called before start (because pool is nil)



264
265
266
# File 'lib/nonnative.rb', line 264

def reset
  Nonnative.pool.reset
end

.startvoid

This method returns an undefined value.

Starts all configured services, servers, and processes, and waits for readiness.

Readiness is determined by attempting to connect to each runner’s configured host/port.

Raises:



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/nonnative.rb', line 210

def start
  @pool ||= Nonnative::Pool.new(configuration)
  errors = []

  @pool.start do |name, values, result|
    id, started = values
    errors << "Started #{name} with id #{id}, though did respond in time" if !started || !result
  end

  raise Nonnative::StartError, errors.join("\n") unless errors.empty?
end

.stopvoid

This method returns an undefined value.

Stops all configured processes and servers, then services, and waits for shutdown.

Raises:



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/nonnative.rb', line 226

def stop
  return if @pool.nil?

  errors = []

  @pool.stop do |name, id, result|
    errors << "Stopped #{name} with id #{id}, though did respond in time" unless result
  end

  raise Nonnative::StopError, errors.join("\n") unless errors.empty?
end