RpcLoadBalancer.LoadBalancer.NodeCpuCache (rpc_load_balancer v0.3.0)

Copy Markdown View Source

Shared ETS cache for CPU metrics, keyed by node.

CPU utilization is a property of the node itself, not of any particular load balancer — every load balancer that contains a given node sees the same value. Storing one entry per node (rather than per {load_balancer_name, target_node}) deduplicates writes, simplifies the cache shape, and lets every Poller across every load balancer cooperate on warming the same data.

Backed by Cache.ETS so the cache is one named table for the entire VM. Test sandboxing flows through Cache.SandboxRegistry.

Cache.PersistentTerm is unsuitable because the Poller writes every tick and :persistent_term.put/2 triggers a global GC sweep of every process referencing the term table.

Summary

Types

entry()

@type entry() :: %{cpu: float(), fetched_at: integer()}

Functions

adapter_options()

cache_adapter()

cache_name()

child_spec(_)

delete(key)

delete_cpu(target_node)

@spec delete_cpu(node()) :: :ok | {:error, ErrorMessage.t()}

get(key)

get_cpu(target_node)

@spec get_cpu(node()) :: {:ok, entry() | nil} | {:error, ErrorMessage.t()}

get_local_cpu()

@spec get_local_cpu() :: {:ok, entry() | nil} | {:error, ErrorMessage.t()}

Reads the local node's cached CPU entry.

Exposed as a zero-arity function so :erpc.multicall/5 can target it with an empty argument list — the remote node resolves its own identity rather than the caller embedding it in the key.

get_or_create(key, fnc)

put(key, ttl \\ nil, value)

put_cpu(target_node, entry)

@spec put_cpu(node(), entry()) :: :ok | {:error, ErrorMessage.t()}