Phoenix.SocketClient.Router (phoenix_socket_client v0.8.1)
View SourceHigh-performance message routing utilities.
This module provides optimized message routing functions that use the route cache for fast channel discovery while falling back to Registry lookups when necessary.
Features
- Cached route lookups for performance
- Automatic cache population on successful lookups
- Fallback to Registry for cache misses
- Route statistics and monitoring
- Configurable routing strategies
Usage
The router is used internally by message processing components to provide fast channel-to-process mapping.
Performance Benefits
- O(1) cached lookups vs O(n) Registry operations
- Automatic cache warming for frequently accessed channels
- Reduced process discovery overhead
- Scalable routing for high message volumes
Summary
Functions
Clears all cached routes.
Invalidates a cached route.
Routes multiple messages efficiently.
Checks if a route is cached.
Routes a message to the appropriate channel process.
Gets routing statistics for monitoring.
Pre-populates the route cache with known channels.
Types
Functions
@spec clear_cache(pid()) :: :ok
Clears all cached routes.
Useful for cache invalidation or testing.
Parameters
cache_pid- Route cache process PID
Returns
:okon success
Invalidates a cached route.
Removes a topic from the route cache, forcing the next lookup to use Registry and potentially update the cache with fresh data.
Parameters
topic- The channel topic to invalidatecache_pid- Route cache process PID
Returns
:okon success
Routes multiple messages efficiently.
Processes a batch of topics and returns their corresponding PIDs. Uses cached lookups where possible and Registry fallbacks.
Parameters
topics- List of channel topics to routeopts- Routing options
Returns
- List of
{:ok, pid}or:errorresults for each topic
Examples
results = Router.route_batch(["rooms:lobby", "users:123"], opts)
Checks if a route is cached.
Returns true if the topic exists in the route cache.
Parameters
topic- The channel topic to checkcache_pid- Route cache process PID
Returns
trueif cached,falseotherwise
Routes a message to the appropriate channel process.
This function attempts to find the channel PID using the cache first, then falls back to Registry lookups if the cache miss occurs.
Parameters
topic- The channel topic to route toopts- Routing options including cache PID and registry name
Returns
{:ok, pid}- Channel process found:error- Channel not found
Examples
{:ok, pid} = Router.route_message("rooms:lobby", [
cache_pid: cache_pid,
registry_name: MyRegistry
])
Gets routing statistics for monitoring.
Returns statistics about cache performance and routing operations.
Parameters
cache_pid- Route cache process PID
Returns
- Statistics map with hit rates, cache size, etc.
Pre-populates the route cache with known channels.
Useful for warming up the cache with expected channels.
Parameters
routes- List of {topic, pid} tuplescache_pid- Route cache process PID
Returns
:okon success,{:error, reason}on failure