TensorFlow Serving C++ API Documentation
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
tensorflow::serving::EventBus< E > Class Template Reference

#include <event_bus.h>

Inheritance diagram for tensorflow::serving::EventBus< E >:
Inheritance graph
[legend]
Collaboration diagram for tensorflow::serving::EventBus< E >:
Collaboration graph
[legend]

Classes

struct  EventAndTime
 Event and the publish time associated with it. More...
 
struct  Options
 
class  Subscription
 

Public Types

using Callback = std::function< void(const EventAndTime &)>
 

Public Member Functions

std::unique_ptr< SubscriptionSubscribe (const Callback &callback) TF_LOCKS_EXCLUDED(mutex_) TF_MUST_USE_RESULT
 
void Publish (const E &event) TF_LOCKS_EXCLUDED(mutex_)
 Publishes an event to all subscribers.
 

Static Public Member Functions

static std::shared_ptr< EventBusCreateEventBus (const Options &options={})
 

Detailed Description

template<typename E>
class tensorflow::serving::EventBus< E >

EventBus enables basic publish / subscribe semantics for events amongst one or more publishers and subscribers. The purpose of EventBus for serving is to de-couple the code for such events, and is optimized for that use-case.

EventBus and Subscriptions can be destroyed safely in any order. There is a strict requirement for memory safety that a Subscription must be destroyed before any of the objects or memory that a subscriber's callback accesses.

Important scaling and threading limitations:

Scaling: The EventBus is not currently optimized for high scale, either in the number of subscribers or frequency of events. For such use-cases, consider alternate implementations or upgrades to this class.

Threading: EventBus is thread-safe. However, if any subscriber callback calls any method in the EventBus, it will deadlock. Subscribers are notified serially on the event publisher's thread. Thus, the amount of work done in a subscriber's callback should be very minimal.

This implementation is single-binary and does not communicate across tasks.

Note that the types used for typename E in EventBus must be moveable and thread-safe. Future implementations may read Events of type E in multiple threads.

Definition at line 63 of file event_bus.h.

Member Typedef Documentation

◆ Callback

template<typename E >
using tensorflow::serving::EventBus< E >::Callback = std::function<void(const EventAndTime&)>

The function type for EventBus Callbacks to be implemented by clients. Important Warnings:

  • Callbacks must not themselves callback to the EventBus for any purpose including subscribing, publishing or unsubscribing. This will cause a circular deadlock.
  • Callbacks must do very little work as they are invoked on the publisher's thread. Any costly work should be performed asynchronously.

Definition at line 115 of file event_bus.h.

Member Function Documentation

◆ CreateEventBus()

template<typename E >
std::shared_ptr< EventBus< E > > tensorflow::serving::EventBus< E >::CreateEventBus ( const Options options = {})
static

Creates an EventBus and returns a shared_ptr to it. This is the only allowed public mechanism for creating an EventBus so that we can track references to an EventBus uniformly.

Definition at line 191 of file event_bus.h.

◆ Subscribe()

template<typename E >
std::unique_ptr< typename EventBus< E >::Subscription > tensorflow::serving::EventBus< E >::Subscribe ( const Callback callback)

Subscribes to all events on the EventBus.

Returns a Subscription RAII object that can be used to unsubscribe, or will automatically unsubscribe on destruction. Returns a unique_ptr so that we can use the subscription's address to Unsubscribe.

Important contract for unsubscribing (deleting the RAII object):

  • Unsubscribing (deleting the RAII object) may block while currently scheduled callback invocation(s) finish.
  • Once it returns no callback invocations will occur. Callers' destructors must use the sequence: (1) Unsubscribe. (2) Tear down anything that the callback references.

Definition at line 178 of file event_bus.h.


The documentation for this class was generated from the following file: