SnmpKit.SnmpMgr.Router (snmpkit v1.4.0)

Intelligent request routing and load balancing for SNMP requests.

This module provides sophisticated routing strategies to optimize request distribution across multiple engines and target devices, with support for load balancing, affinity routing, and performance-based routing decisions.

Summary

Functions

Adds an engine to the routing pool.

Attempts to recover a failed engine.

Returns a specification to start this module under a supervisor.

Configures batch processing strategy.

Configures engine settings.

Configures health check settings.

Gets engine health information.

Gets routing statistics and engine health.

Marks an engine as healthy.

Removes an engine from the routing pool.

Routes multiple requests as a batch.

Routes a request to the best available engine.

Sets engine weights for weighted routing.

Updates routing strategy.

Starts the request router.

Functions

add_engine(router, engine_spec)

@spec add_engine(GenServer.server(), map() | atom()) :: :ok

Adds an engine to the routing pool.

attempt_engine_recovery(router, engine_name)

@spec attempt_engine_recovery(GenServer.server(), atom()) :: :ok | {:error, term()}

Attempts to recover a failed engine.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

configure_batch_strategy(router, strategy_config)

@spec configure_batch_strategy(
  GenServer.server(),
  keyword()
) :: :ok

Configures batch processing strategy.

configure_engines(router, config)

@spec configure_engines(
  GenServer.server(),
  keyword()
) :: :ok | {:error, term()}

Configures engine settings.

configure_health_check(router, config)

@spec configure_health_check(
  GenServer.server(),
  keyword()
) :: :ok

Configures health check settings.

get_engine_health(router)

@spec get_engine_health(GenServer.server()) :: map()

Gets engine health information.

get_stats(router)

@spec get_stats(GenServer.server()) :: map()

Gets routing statistics and engine health.

mark_engine_healthy(router, engine_name)

@spec mark_engine_healthy(GenServer.server(), atom()) :: :ok | {:error, term()}

Marks an engine as healthy.

mark_engine_unhealthy(router, engine_name, reason)

@spec mark_engine_unhealthy(GenServer.server(), atom(), term()) ::
  :ok | {:error, term()}

Marks an engine as unhealthy.

remove_engine(router, engine_name)

@spec remove_engine(GenServer.server(), atom()) :: :ok

Removes an engine from the routing pool.

route_batch(router, requests, opts \\ [])

@spec route_batch(GenServer.server(), [map()], keyword()) ::
  {:ok, list()} | {:error, term()}

Routes multiple requests as a batch.

Parameters

  • router - Router PID or name
  • requests - List of request specifications
  • opts - Routing options

Examples

requests = [
  %{type: :get, target: "device1", oid: "sysDescr.0"},
  %{type: :get, target: "device2", oid: "sysUpTime.0"}
]

{:ok, results} = SnmpKit.SnmpMgr.Router.route_batch(router, requests)

route_request(router, request, opts \\ [])

@spec route_request(GenServer.server(), map(), keyword()) ::
  {:ok, term()} | {:error, term()}

Routes a request to the best available engine.

Parameters

  • router - Router PID or name
  • request - Request specification
  • opts - Routing options

Examples

request = %{
  type: :get,
  target: "192.168.1.1",
  oid: "sysDescr.0"
}

{:ok, result} = SnmpKit.SnmpMgr.Router.route_request(router, request)

set_engine_weights(router, weights)

@spec set_engine_weights(GenServer.server(), map() | keyword()) ::
  :ok | {:error, term()}

Sets engine weights for weighted routing.

set_strategy(router, strategy)

@spec set_strategy(GenServer.server(), atom()) :: :ok

Updates routing strategy.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the request router.

Options

  • :strategy - Routing strategy (:round_robin, :least_connections, :weighted, :affinity)
  • :engines - List of engine specifications
  • :health_check_interval - Health check interval in ms (default: 30000)
  • :max_retries - Maximum retry attempts (default: 3)

Examples

{:ok, router} = SnmpKit.SnmpMgr.Router.start_link(
  strategy: :least_connections,
  engines: [
    %{name: :engine1, weight: 2, max_load: 100},
    %{name: :engine2, weight: 1, max_load: 50}
  ]
)