pub struct K2KBroker { /* private fields */ }Expand description
K2K message broker with per-tenant isolation.
Internally holds a HashMap<TenantId, K2KSubBroker>. Single-tenant
deployments see exactly one entry (for UNSPECIFIED_TENANT = 0); the
only overhead over the legacy broker is one additional HashMap lookup
per send (~20 ns).
Implementations§
Source§impl K2KBroker
impl K2KBroker
Sourcepub fn new(config: K2KConfig) -> Arc<Self>
pub fn new(config: K2KConfig) -> Arc<Self>
Create a new K2K broker with an empty TenantRegistry.
Sourcepub fn with_registry(
config: K2KConfig,
registry: Arc<TenantRegistry>,
) -> Arc<Self>
pub fn with_registry( config: K2KConfig, registry: Arc<TenantRegistry>, ) -> Arc<Self>
Create a new K2K broker sharing the given TenantRegistry.
This is the multi-tenant constructor: wire up the registry (with its
audit sink) once, then pass the Arc to every broker that should
enforce against it.
Sourcepub fn registry(&self) -> &Arc<TenantRegistry>
pub fn registry(&self) -> &Arc<TenantRegistry>
Shared reference to the tenant registry.
Sourcepub fn tenant_count(&self) -> usize
pub fn tenant_count(&self) -> usize
Total number of active tenant sub-brokers.
Sourcepub fn sub_broker(&self, tenant_id: TenantId) -> Option<Arc<K2KSubBroker>>
pub fn sub_broker(&self, tenant_id: TenantId) -> Option<Arc<K2KSubBroker>>
Get the sub-broker for a tenant, if it exists.
Sourcepub fn register(self: &Arc<Self>, kernel_id: KernelId) -> K2KEndpoint
pub fn register(self: &Arc<Self>, kernel_id: KernelId) -> K2KEndpoint
Register a kernel without a tenant / audit tag (legacy API).
The kernel is placed in the unspecified-tenant sub-broker
(UNSPECIFIED_TENANT = 0). This is the backward-compatible entry
point for single-tenant deployments — existing callers don’t need to
change anything.
Sourcepub fn register_tenant(
self: &Arc<Self>,
tenant_id: TenantId,
audit_tag: AuditTag,
kernel_id: KernelId,
) -> K2KEndpoint
pub fn register_tenant( self: &Arc<Self>, tenant_id: TenantId, audit_tag: AuditTag, kernel_id: KernelId, ) -> K2KEndpoint
Register a kernel into the given tenant’s sub-broker, stamping the
AuditTag that will be applied to outgoing messages from this
kernel.
If the tenant doesn’t yet have a sub-broker, one is created lazily. If the kernel was previously registered under a different tenant, the old registration is replaced (the old mpsc sender is dropped — in-flight messages to the old registration are lost). This preserves the invariant that a kernel belongs to exactly one tenant.
Sourcepub fn unregister(&self, kernel_id: &KernelId)
pub fn unregister(&self, kernel_id: &KernelId)
Unregister a kernel from whichever tenant it belongs to.
Sourcepub fn is_registered(&self, kernel_id: &KernelId) -> bool
pub fn is_registered(&self, kernel_id: &KernelId) -> bool
Check if a kernel is registered (under any tenant).
Sourcepub fn tenant_of(&self, kernel_id: &KernelId) -> Option<TenantId>
pub fn tenant_of(&self, kernel_id: &KernelId) -> Option<TenantId>
Get the tenant this kernel belongs to, if registered.
Sourcepub fn registered_kernels(&self) -> Vec<KernelId>
pub fn registered_kernels(&self) -> Vec<KernelId>
All registered kernel IDs (across every tenant).
Sourcepub fn registered_kernels_for(&self, tenant_id: TenantId) -> Vec<KernelId>
pub fn registered_kernels_for(&self, tenant_id: TenantId) -> Vec<KernelId>
Kernel IDs registered under a specific tenant.
Sourcepub async fn send(
&self,
source: KernelId,
destination: KernelId,
envelope: MessageEnvelope,
) -> Result<DeliveryReceipt>
pub async fn send( &self, source: KernelId, destination: KernelId, envelope: MessageEnvelope, ) -> Result<DeliveryReceipt>
Send a message from one kernel to another (normal priority).
Enforces tenant isolation: the sender’s and destination’s tenants
must match, or the send is rejected with
crate::error::RingKernelError::TenantMismatch and an audit event
is emitted.
Sourcepub async fn send_priority(
&self,
source: KernelId,
destination: KernelId,
envelope: MessageEnvelope,
priority: u8,
) -> Result<DeliveryReceipt>
pub async fn send_priority( &self, source: KernelId, destination: KernelId, envelope: MessageEnvelope, priority: u8, ) -> Result<DeliveryReceipt>
Send a priority message.
Sourcepub async fn send_with_audit(
&self,
source: KernelId,
destination: KernelId,
envelope: MessageEnvelope,
audit_tag: AuditTag,
) -> Result<DeliveryReceipt>
pub async fn send_with_audit( &self, source: KernelId, destination: KernelId, envelope: MessageEnvelope, audit_tag: AuditTag, ) -> Result<DeliveryReceipt>
Send a message stamped with an explicit audit tag (overriding the registration-time tag).
Useful for the same kernel participating in multiple engagements — e.g. a shared report-generation kernel that should bill different engagements depending on which request it’s serving.
Sourcepub fn add_route(&self, destination: KernelId, next_hop: KernelId)
pub fn add_route(&self, destination: KernelId, next_hop: KernelId)
Add an indirect route inside the unspecified-tenant sub-broker (legacy API — single-tenant callers should use this).
Sourcepub fn add_route_in(
&self,
tenant_id: TenantId,
destination: KernelId,
next_hop: KernelId,
)
pub fn add_route_in( &self, tenant_id: TenantId, destination: KernelId, next_hop: KernelId, )
Add an indirect route inside a specific tenant’s sub-broker.
Sourcepub fn remove_route(&self, destination: &KernelId)
pub fn remove_route(&self, destination: &KernelId)
Remove an indirect route from the unspecified-tenant sub-broker.
Sourcepub fn remove_route_in(&self, tenant_id: TenantId, destination: &KernelId)
pub fn remove_route_in(&self, tenant_id: TenantId, destination: &KernelId)
Remove an indirect route from a specific tenant’s sub-broker.
Sourcepub fn tenant_stats(&self, tenant_id: TenantId) -> Option<TenantStats>
pub fn tenant_stats(&self, tenant_id: TenantId) -> Option<TenantStats>
Get per-tenant stats (useful for billing / dashboards).
Sourcepub fn get_receipt(&self, message_id: &MessageId) -> Option<DeliveryReceipt>
pub fn get_receipt(&self, message_id: &MessageId) -> Option<DeliveryReceipt>
Get delivery receipt for a message.