proxygen
folly::TimeoutQueue Class Reference

#include <TimeoutQueue.h>

Classes

struct  Event
 

Public Types

typedef int64_t Id
 
typedef std::function< void(Id, int64_t)> Callback
 

Public Member Functions

 TimeoutQueue ()
 
Id add (int64_t now, int64_t delay, Callback callback)
 
Id addRepeating (int64_t now, int64_t interval, Callback callback)
 
bool erase (Id id)
 
int64_t runOnce (int64_t now)
 
int64_t runLoop (int64_t now)
 
int64_t nextExpiration () const
 

Private Types

enum  { BY_ID = 0, BY_EXPIRATION = 1 }
 
typedef boost::multi_index_container< Event, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::member< Event, Id,&Event::id > >, boost::multi_index::ordered_non_unique< boost::multi_index::member< Event, int64_t,&Event::expiration > > > > Set
 

Private Member Functions

int64_t runInternal (int64_t now, bool runOnce)
 
 TimeoutQueue (const TimeoutQueue &)=delete
 
TimeoutQueueoperator= (const TimeoutQueue &)=delete
 

Private Attributes

Set timeouts_
 
Id nextId_
 

Detailed Description

Definition at line 40 of file TimeoutQueue.h.

Member Typedef Documentation

typedef std::function<void(Id, int64_t)> folly::TimeoutQueue::Callback

Definition at line 43 of file TimeoutQueue.h.

Definition at line 42 of file TimeoutQueue.h.

typedef boost::multi_index_container< Event, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::member<Event, Id, &Event::id> >, boost::multi_index::ordered_non_unique< boost::multi_index::member<Event, int64_t, &Event::expiration> > > > folly::TimeoutQueue::Set
private

Definition at line 119 of file TimeoutQueue.h.

Member Enumeration Documentation

anonymous enum
private
Enumerator
BY_ID 
BY_EXPIRATION 

Definition at line 121 of file TimeoutQueue.h.

Constructor & Destructor Documentation

folly::TimeoutQueue::TimeoutQueue ( )
inline

Definition at line 45 of file TimeoutQueue.h.

References add(), addRepeating(), erase(), int64_t, and now().

Referenced by runLoop().

45 : nextId_(1) {}
folly::TimeoutQueue::TimeoutQueue ( const TimeoutQueue )
privatedelete

Member Function Documentation

TimeoutQueue::Id folly::TimeoutQueue::add ( int64_t  now,
int64_t  delay,
Callback  callback 
)

Add a one-time timeout event that will fire "delay" time units from "now" (that is, the first time that run*() is called with a time value >= now

  • delay).

Definition at line 24 of file TimeoutQueue.cpp.

References folly::gen::move, nextId_, and timeouts_.

Referenced by TEST(), and TimeoutQueue().

24  {
25  Id id = nextId_++;
26  timeouts_.insert({id, now + delay, -1, std::move(callback)});
27  return id;
28 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::steady_clock::time_point now()
TimeoutQueue::Id folly::TimeoutQueue::addRepeating ( int64_t  now,
int64_t  interval,
Callback  callback 
)

Add a repeating timeout event that will fire every "interval" time units (it will first fire when run*() is called with a time value >= now + interval).

run*() will always invoke each repeating event at most once, even if more than one "interval" period has passed.

Definition at line 31 of file TimeoutQueue.cpp.

References folly::gen::move, nextId_, and timeouts_.

Referenced by TEST(), and TimeoutQueue().

31  {
32  Id id = nextId_++;
33  timeouts_.insert({id, now + interval, interval, std::move(callback)});
34  return id;
35 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::steady_clock::time_point now()
bool folly::TimeoutQueue::erase ( Id  id)

Erase a given timeout event, returns true if the event was actually erased and false if it didn't exist in our queue.

Definition at line 43 of file TimeoutQueue.cpp.

References BY_ID, and timeouts_.

Referenced by TEST(), and TimeoutQueue().

43  {
44  return timeouts_.get<BY_ID>().erase(id);
45 }
int64_t folly::TimeoutQueue::nextExpiration ( ) const

Return the time that the next event will be due.

Definition at line 37 of file TimeoutQueue.cpp.

References folly::test::begin(), BY_EXPIRATION, max, and timeouts_.

Referenced by runInternal(), and runLoop().

37  {
38  return (
41 }
LogLevel max
Definition: LogLevel.cpp:31
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
static void expiration()
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TimeoutQueue& folly::TimeoutQueue::operator= ( const TimeoutQueue )
privatedelete

Referenced by runLoop().

int64_t folly::TimeoutQueue::runInternal ( int64_t  now,
bool  runOnce 
)
private

Definition at line 47 of file TimeoutQueue.cpp.

References BY_EXPIRATION, folly::test::end(), int64_t, folly::gen::move, nextExpiration(), and timeouts_.

Referenced by runLoop(), and runOnce().

47  {
48  auto& byExpiration = timeouts_.get<BY_EXPIRATION>();
49  int64_t nextExp;
50  do {
51  const auto end = byExpiration.upper_bound(now);
52  std::vector<Event> expired;
53  std::move(byExpiration.begin(), end, std::back_inserter(expired));
54  byExpiration.erase(byExpiration.begin(), end);
55  for (const auto& event : expired) {
56  // Reinsert if repeating, do this before executing callbacks
57  // so the callbacks have a chance to call erase
58  if (event.repeatInterval >= 0) {
59  timeouts_.insert({event.id,
60  now + event.repeatInterval,
61  event.repeatInterval,
62  event.callback});
63  }
64  }
65 
66  // Call callbacks
67  for (const auto& event : expired) {
68  event.callback(event.id, now);
69  }
70  nextExp = nextExpiration();
71  } while (!onceOnly && nextExp <= now);
72  return nextExp;
73 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::steady_clock::time_point now()
int64_t nextExpiration() const
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
int64_t folly::TimeoutQueue::runLoop ( int64_t  now)
inline

Definition at line 90 of file TimeoutQueue.h.

References int64_t, nextExpiration(), operator=(), runInternal(), runOnce(), and TimeoutQueue().

Referenced by TEST().

90  {
91  return runInternal(now, false);
92  }
std::chrono::steady_clock::time_point now()
int64_t runInternal(int64_t now, bool runOnce)
int64_t folly::TimeoutQueue::runOnce ( int64_t  now)
inline

Process all events that are due at times <= "now" by calling their callbacks.

Callbacks are allowed to call back into the queue and add / erase events; they might create more events that are already due. In this case, runOnce() will only go through the queue once, and return a "next expiration" time in the past or present (<= now); runLoop() will process the queue again, until there are no events already due.

Note that it is then possible for runLoop to never return if callbacks re-add themselves to the queue (or if you have repeating callbacks with an interval of 0).

Return the time that the next event will be due (same as nextExpiration(), below)

Definition at line 87 of file TimeoutQueue.h.

References runInternal().

Referenced by runLoop(), and TEST().

87  {
88  return runInternal(now, true);
89  }
std::chrono::steady_clock::time_point now()
int64_t runInternal(int64_t now, bool runOnce)

Member Data Documentation

Id folly::TimeoutQueue::nextId_
private

Definition at line 127 of file TimeoutQueue.h.

Referenced by add(), and addRepeating().

Set folly::TimeoutQueue::timeouts_
private

Definition at line 126 of file TimeoutQueue.h.

Referenced by add(), addRepeating(), erase(), nextExpiration(), and runInternal().


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