Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Event Bus

From Leeroopedia
Knowledge Sources
Domains Messaging, Utility
Last Updated 2026-02-13 00:00 GMT

Overview

A thread-safe, in-process publish-subscribe event bus that decouples event publishers from subscribers using RAII-based subscription management.

Description

EventBus<E> is a templated class that enables basic publish/subscribe semantics for events of moveable type E. It is created via the static CreateEventBus() factory method which returns a shared_ptr (the bus inherits from enable_shared_from_this). Subscribers register a callback via Subscribe() and receive a Subscription RAII object; when the Subscription is destroyed, it automatically unsubscribes. The Subscription holds only a weak_ptr to the EventBus, so the bus and subscriptions can be destroyed in any order without dangling references. Publishing an event (Publish()) iterates through all subscriptions under a mutex lock, invoking each callback serially on the publisher's thread with the event and a microsecond-precision timestamp. Callbacks must do minimal work and must not call back into the EventBus to avoid deadlock. The implementation is optimized for simplicity and decoupling rather than high throughput.

Usage

Use this when different components in the serving system need to be notified of events (such as model version changes or state transitions) without tight coupling between the producer and consumer code.

Code Reference

Source Location

Signature

template <typename E>
class EventBus : public std::enable_shared_from_this<EventBus<E>> {
 public:
  struct EventAndTime {
    const E& event;
    uint64_t event_time_micros;
  };
  using Callback = std::function<void(const EventAndTime&)>;

  static std::shared_ptr<EventBus> CreateEventBus(const Options& options = {});
  std::unique_ptr<Subscription> Subscribe(const Callback& callback);
  void Publish(const E& event);
};

Import

#include "tensorflow_serving/util/event_bus.h"

I/O Contract

Inputs

Name Type Required Description
callback std::function<void(const EventAndTime&)> Yes (Subscribe) Function invoked for each published event
event const E& Yes (Publish) The event object to publish to all subscribers
options Options No Configuration including the Env for time

Outputs

Name Type Description
Subscription std::unique_ptr<Subscription> RAII handle; destroying it unsubscribes the callback

Usage Examples

Subscribe and Publish

// Create the event bus
auto bus = EventBus<MyEvent>::CreateEventBus();

// Subscribe
auto subscription = bus->Subscribe([](const EventBus<MyEvent>::EventAndTime& e) {
  LOG(INFO) << "Received event at time " << e.event_time_micros;
});

// Publish an event
MyEvent event = ...;
bus->Publish(event);

// Unsubscribe by destroying the subscription
subscription.reset();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment