proxygen
folly::AsyncTimeout Class Referenceabstract

#include <AsyncTimeout.h>

Inheritance diagram for folly::AsyncTimeout:
DelayedWrite fizz::AsyncFizzBase::HandshakeTimeout fizz::server::SlidingBloomReplayCache folly::AsyncServerSocket::BackoffTimeout folly::AsyncSocket::WriteTimeout folly::AsyncSSLSocket::Timeout folly::detail::async_timeout_wrapper< TCallback > folly::EventBaseAborter folly::HHWheelTimer folly::MemoryIdlerTimeout folly::TimeoutManager::CobTimeouts::CobTimeout IdleTimeTimeoutSeries proxygen::AsyncTimeoutSet ReschedulingTimeout TestAsyncTransport TestTimeout UDPClient UDPClient wangle::ConnectionManager::DrainHelper

Public Types

typedef TimeoutManager::InternalEnum InternalEnum
 

Public Member Functions

 AsyncTimeout (TimeoutManager *timeoutManager)
 
 AsyncTimeout (EventBase *eventBase)
 
 AsyncTimeout (TimeoutManager *timeoutManager, InternalEnum internal)
 
 AsyncTimeout (EventBase *eventBase, InternalEnum internal)
 
 AsyncTimeout ()
 
virtual ~AsyncTimeout ()
 
virtual void timeoutExpired () noexcept=0
 
bool scheduleTimeout (uint32_t milliseconds)
 
bool scheduleTimeout (TimeoutManager::timeout_type timeout)
 
void cancelTimeout ()
 
bool isScheduled () const
 
void attachTimeoutManager (TimeoutManager *timeoutManager, InternalEnum internal=InternalEnum::NORMAL)
 
void attachEventBase (EventBase *eventBase, InternalEnum internal=InternalEnum::NORMAL)
 
void detachTimeoutManager ()
 
void detachEventBase ()
 
const TimeoutManagergetTimeoutManager ()
 
struct event * getEvent ()
 

Static Public Member Functions

template<typename TCallback >
static std::unique_ptr< AsyncTimeoutmake (TimeoutManager &manager, TCallback &&callback)
 
template<typename TCallback >
static std::unique_ptr< AsyncTimeoutschedule (TimeoutManager::timeout_type timeout, TimeoutManager &manager, TCallback &&callback)
 

Static Private Member Functions

static void libeventCallback (libevent_fd_t fd, short events, void *arg)
 

Private Attributes

struct event event_
 
TimeoutManagertimeoutManager_
 
std::shared_ptr< RequestContextcontext_
 

Detailed Description

AsyncTimeout is used to asynchronously wait for a timeout to occur.

Definition at line 36 of file AsyncTimeout.h.

Member Typedef Documentation

Constructor & Destructor Documentation

folly::AsyncTimeout::AsyncTimeout ( TimeoutManager timeoutManager)
explicit

Create a new AsyncTimeout object, driven by the specified TimeoutManager.

Definition at line 27 of file AsyncTimeout.cpp.

References folly::TimeoutManager::attachTimeoutManager(), event_, folly::folly_event_set(), libeventCallback(), folly::TimeoutManager::NORMAL, and timeoutManager_.

28  : timeoutManager_(timeoutManager) {
30  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
31  event_.ev_base = nullptr;
34 }
struct event event_
Definition: AsyncTimeout.h:221
void folly_event_set(event *e, int fd, short s, EventSetCallback f, void *arg)
Definition: Event.h:50
virtual void attachTimeoutManager(AsyncTimeout *obj, InternalEnum internal)=0
static void libeventCallback(libevent_fd_t fd, short events, void *arg)
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
folly::AsyncTimeout::AsyncTimeout ( EventBase eventBase)
explicit

Definition at line 36 of file AsyncTimeout.cpp.

References folly::TimeoutManager::attachTimeoutManager(), event_, folly::folly_event_set(), libeventCallback(), folly::TimeoutManager::NORMAL, and timeoutManager_.

36  : timeoutManager_(eventBase) {
38  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
39  event_.ev_base = nullptr;
40  if (eventBase) {
43  }
44 }
struct event event_
Definition: AsyncTimeout.h:221
void folly_event_set(event *e, int fd, short s, EventSetCallback f, void *arg)
Definition: Event.h:50
virtual void attachTimeoutManager(AsyncTimeout *obj, InternalEnum internal)=0
static void libeventCallback(libevent_fd_t fd, short events, void *arg)
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
folly::AsyncTimeout::AsyncTimeout ( TimeoutManager timeoutManager,
InternalEnum  internal 
)

Create a new internal AsyncTimeout object.

Internal timeouts are like regular timeouts, but will not stop the TimeoutManager loop from exiting if the only remaining events are internal timeouts.

This is useful for implementing fallback timeouts to abort the TimeoutManager loop if the other events have not been processed within a specified time period: if the event loop takes too long the timeout will fire and can stop the event loop. However, if all other events complete, the event loop will exit even though the internal timeout is still installed.

Definition at line 46 of file AsyncTimeout.cpp.

References folly::TimeoutManager::attachTimeoutManager(), event_, folly::folly_event_set(), libeventCallback(), and timeoutManager_.

49  : timeoutManager_(timeoutManager) {
51  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
52  event_.ev_base = nullptr;
53  timeoutManager_->attachTimeoutManager(this, internal);
54 }
struct event event_
Definition: AsyncTimeout.h:221
void folly_event_set(event *e, int fd, short s, EventSetCallback f, void *arg)
Definition: Event.h:50
virtual void attachTimeoutManager(AsyncTimeout *obj, InternalEnum internal)=0
static void libeventCallback(libevent_fd_t fd, short events, void *arg)
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
folly::AsyncTimeout::AsyncTimeout ( EventBase eventBase,
InternalEnum  internal 
)

Definition at line 56 of file AsyncTimeout.cpp.

References folly::TimeoutManager::attachTimeoutManager(), event_, folly::folly_event_set(), libeventCallback(), and timeoutManager_.

57  : timeoutManager_(eventBase) {
59  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
60  event_.ev_base = nullptr;
61  timeoutManager_->attachTimeoutManager(this, internal);
62 }
struct event event_
Definition: AsyncTimeout.h:221
void folly_event_set(event *e, int fd, short s, EventSetCallback f, void *arg)
Definition: Event.h:50
virtual void attachTimeoutManager(AsyncTimeout *obj, InternalEnum internal)=0
static void libeventCallback(libevent_fd_t fd, short events, void *arg)
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
folly::AsyncTimeout::AsyncTimeout ( )

Create a new AsyncTimeout object, not yet assigned to a TimeoutManager.

attachEventBase() must be called prior to scheduling the timeout.

Definition at line 64 of file AsyncTimeout.cpp.

References event_, folly::folly_event_set(), and libeventCallback().

64  : timeoutManager_(nullptr) {
66  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
67  event_.ev_base = nullptr;
68 }
struct event event_
Definition: AsyncTimeout.h:221
void folly_event_set(event *e, int fd, short s, EventSetCallback f, void *arg)
Definition: Event.h:50
static void libeventCallback(libevent_fd_t fd, short events, void *arg)
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
folly::AsyncTimeout::~AsyncTimeout ( )
virtual

AsyncTimeout destructor.

The timeout will be automatically cancelled if it is running.

Definition at line 70 of file AsyncTimeout.cpp.

References cancelTimeout().

70  {
71  cancelTimeout();
72 }

Member Function Documentation

void folly::AsyncTimeout::attachEventBase ( EventBase eventBase,
InternalEnum  internal = InternalEnum::NORMAL 
)

Definition at line 106 of file AsyncTimeout.cpp.

References attachTimeoutManager().

Referenced by fizz::AsyncFizzBase::attachEventBase(), folly::AsyncSocket::attachEventBase(), folly::AsyncSSLSocket::attachEventBase(), and folly::HHWheelTimer::isDetachable().

108  {
109  attachTimeoutManager(eventBase, internal);
110 }
void attachTimeoutManager(TimeoutManager *timeoutManager, InternalEnum internal=InternalEnum::NORMAL)
void folly::AsyncTimeout::attachTimeoutManager ( TimeoutManager timeoutManager,
InternalEnum  internal = InternalEnum::NORMAL 
)

Attach the timeout to a TimeoutManager.

This may only be called if the timeout is not currently attached to a TimeoutManager (either by using the default constructor, or by calling detachTimeoutManager()).

This method must be invoked in the TimeoutManager's thread.

The internal parameter specifies if this timeout should be treated as an internal event. TimeoutManager::loop() will return when there are no more non-internal events remaining.

Definition at line 95 of file AsyncTimeout.cpp.

References folly::TimeoutManager::attachTimeoutManager(), folly::TimeoutManager::isInTimeoutManagerThread(), and timeoutManager_.

Referenced by attachEventBase(), fizz::AsyncFizzBase::attachTimeoutManager(), and folly::AsyncSSLSocket::attachTimeoutManager().

97  {
98  // This also implies no timeout is scheduled.
99  assert(timeoutManager_ == nullptr);
100  assert(timeoutManager->isInTimeoutManagerThread());
101  timeoutManager_ = timeoutManager;
102 
103  timeoutManager_->attachTimeoutManager(this, internal);
104 }
virtual void attachTimeoutManager(AsyncTimeout *obj, InternalEnum internal)=0
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
void folly::AsyncTimeout::detachTimeoutManager ( )

Detach the timeout from its TimeoutManager.

This may only be called when the timeout is not running. Once detached, the timeout may not be scheduled again until it is re-attached to a EventBase by calling attachEventBase().

This method must be called from the current TimeoutManager's thread.

Definition at line 112 of file AsyncTimeout.cpp.

References folly::TimeoutManager::detachTimeoutManager(), folly::FATAL, isScheduled(), and timeoutManager_.

Referenced by detachEventBase(), fizz::AsyncFizzBase::detachTimeoutManager(), and folly::AsyncSSLSocket::detachTimeoutManager().

112  {
113  // Only allow the event base to be changed if the timeout is not
114  // currently installed.
115  if (isScheduled()) {
116  // Programmer bug. Abort the program.
117  LOG(FATAL) << "detachEventBase() called on scheduled timeout; aborting";
118  }
119 
120  if (timeoutManager_) {
122  timeoutManager_ = nullptr;
123  }
124 }
virtual void detachTimeoutManager(AsyncTimeout *obj)=0
bool isScheduled() const
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
struct event* folly::AsyncTimeout::getEvent ( )
inline
const TimeoutManager* folly::AsyncTimeout::getTimeoutManager ( )
inline

Definition at line 143 of file AsyncTimeout.h.

References timeoutManager_.

Referenced by folly::HHWheelTimer::isDetachable().

143  {
144  return timeoutManager_;
145  }
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
void folly::AsyncTimeout::libeventCallback ( libevent_fd_t  fd,
short  events,
void *  arg 
)
staticprivate

Definition at line 130 of file AsyncTimeout.cpp.

References folly::TimeoutManager::bumpHandlingTime(), context_, event_, folly::event_ref_flags(), folly::libeventFdToFd(), folly::detail::timeout, timeoutExpired(), and timeoutManager_.

Referenced by AsyncTimeout(), and getEvent().

130  {
131  AsyncTimeout* timeout = reinterpret_cast<AsyncTimeout*>(arg);
132  assert(libeventFdToFd(fd) == -1);
133  assert(events == EV_TIMEOUT);
134  // prevent unused variable warnings
135  (void)fd;
136  (void)events;
137 
138  // double check that ev_flags gets reset when the timeout is not running
139  assert((event_ref_flags(&timeout->event_) & ~EVLIST_INTERNAL) == EVLIST_INIT);
140 
141  // this can't possibly fire if timeout->eventBase_ is nullptr
142  timeout->timeoutManager_->bumpHandlingTime();
143 
144  RequestContextScopeGuard rctx(timeout->context_);
145 
146  timeout->timeoutExpired();
147 }
auto event_ref_flags(struct event *ev) -> decltype(std::ref(ev->ev_flags))
Definition: EventUtil.h:41
int libeventFdToFd(libevent_fd_t fd)
Definition: Event.h:44
template<typename TCallback >
std::unique_ptr< AsyncTimeout > folly::AsyncTimeout::make ( TimeoutManager manager,
TCallback &&  callback 
)
static

Convenience function that wraps a function object as an AsyncTimeout instance and returns the wrapper.

Specially useful when using lambdas as AsyncTimeout observers.

Example:

void foo(TimeoutManager &manager) { std::atomic_bool done = false;

auto observer = AsyncTimeout::make(manager, [&] { std::cout << "hello, world!" << std::endl; done = true; });

observer->scheduleTimeout(std::chrono::seconds(5));

while (!done); // busy wait }

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 261 of file AsyncTimeout.h.

Referenced by getEvent(), schedule(), and folly::TEST().

263  {
264  return std::unique_ptr<AsyncTimeout>(
266  std::addressof(manager), std::forward<TCallback>(callback)));
267 }
PskType type
template<typename TCallback >
std::unique_ptr< AsyncTimeout > folly::AsyncTimeout::schedule ( TimeoutManager::timeout_type  timeout,
TimeoutManager manager,
TCallback &&  callback 
)
static

Convenience function that wraps a function object as an AsyncTimeout instance and returns the wrapper after scheduling it using the given TimeoutManager.

This is equivalent to calling make_async_timeout followed by a scheduleTimeout on the resulting wrapper.

Specially useful when using lambdas as AsyncTimeout observers.

Example:

void foo(TimeoutManager &manager) { std::atomic_bool done = false;

auto observer = AsyncTimeout::schedule( std::chrono::seconds(5), manager, [&] { std::cout << "hello, world!" << std::endl; done = true; } );

while (!done); // busy wait }

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 270 of file AsyncTimeout.h.

References make().

Referenced by getEvent(), and folly::TEST().

273  {
274  auto wrapper = AsyncTimeout::make(manager, std::forward<TCallback>(callback));
275  wrapper->scheduleTimeout(timeout);
276  return wrapper;
277 }
static std::unique_ptr< AsyncTimeout > make(TimeoutManager &manager, TCallback &&callback)
Definition: AsyncTimeout.h:261
bool folly::AsyncTimeout::scheduleTimeout ( uint32_t  milliseconds)

Schedule the timeout to fire in the specified number of milliseconds.

After the specified number of milliseconds has elapsed, timeoutExpired() will be invoked by the TimeoutManager's main loop.

If the timeout is already running, it will be rescheduled with the new timeout value.

Parameters
millisecondsThe timeout duration, in milliseconds.
Returns
Returns true if the timeout was successfully scheduled, and false if an error occurred. After an error, the timeout is always unscheduled, even if scheduleTimeout() was just rescheduling an existing timeout.

Definition at line 80 of file AsyncTimeout.cpp.

Referenced by folly::AsyncServerSocket::enterBackoff(), TestAsyncTransport::fireNextReadEvent(), folly::AsyncSocket::handleWrite(), proxygen::AsyncTimeoutSet::headChanged(), folly::MemoryIdlerTimeout::runLoopCallback(), folly::AsyncSocket::scheduleConnectTimeout(), TestAsyncTransport::scheduleNextReadEvent(), folly::HHWheelTimer::scheduleNextTimeout(), folly::AsyncSSLSocket::Timeout::scheduleTimeout(), proxygen::AsyncTimeoutSet::scheduleTimeout(), folly::AsyncSocket::setSendTimeout(), fizz::server::SlidingBloomReplayCache::SlidingBloomReplayCache(), fizz::AsyncFizzBase::startHandshakeTimeout(), TEST(), fizz::server::SlidingBloomReplayCache::timeoutExpired(), proxygen::AsyncTimeoutSet::timeoutExpired(), and folly::AsyncSocket::writeImpl().

80  {
81  return scheduleTimeout(TimeoutManager::timeout_type(milliseconds));
82 }
std::chrono::milliseconds timeout_type
bool scheduleTimeout(uint32_t milliseconds)
bool folly::AsyncTimeout::scheduleTimeout ( TimeoutManager::timeout_type  timeout)

Definition at line 74 of file AsyncTimeout.cpp.

References context_, folly::RequestContext::saveContext(), folly::TimeoutManager::scheduleTimeout(), and timeoutManager_.

74  {
75  assert(timeoutManager_ != nullptr);
77  return timeoutManager_->scheduleTimeout(this, timeout);
78 }
virtual bool scheduleTimeout(AsyncTimeout *obj, timeout_type timeout)=0
std::shared_ptr< RequestContext > context_
Definition: AsyncTimeout.h:231
static std::shared_ptr< RequestContext > saveContext()
Definition: Request.h:196
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228

Member Data Documentation

std::shared_ptr<RequestContext> folly::AsyncTimeout::context_
private

Definition at line 231 of file AsyncTimeout.h.

Referenced by cancelTimeout(), libeventCallback(), and scheduleTimeout().

struct event folly::AsyncTimeout::event_
private

Definition at line 221 of file AsyncTimeout.h.

Referenced by AsyncTimeout(), getEvent(), isScheduled(), and libeventCallback().

TimeoutManager* folly::AsyncTimeout::timeoutManager_
private

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