proxygen
AsyncTimeout.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
20 
22 
23 #include <boost/noncopyable.hpp>
24 #include <memory>
25 #include <utility>
26 
27 namespace folly {
28 
29 class EventBase;
30 class RequestContext;
31 class TimeoutManager;
32 
36 class AsyncTimeout : private boost::noncopyable {
37  public:
39 
43  explicit AsyncTimeout(TimeoutManager* timeoutManager);
44  explicit AsyncTimeout(EventBase* eventBase);
45 
60  AsyncTimeout(TimeoutManager* timeoutManager, InternalEnum internal);
61  AsyncTimeout(EventBase* eventBase, InternalEnum internal);
62 
68  AsyncTimeout();
69 
75  virtual ~AsyncTimeout();
76 
80  virtual void timeoutExpired() noexcept = 0;
81 
98  bool scheduleTimeout(uint32_t milliseconds);
99  bool scheduleTimeout(TimeoutManager::timeout_type timeout);
100 
104  void cancelTimeout();
105 
109  bool isScheduled() const;
110 
125  TimeoutManager* timeoutManager,
126  InternalEnum internal = InternalEnum::NORMAL);
127  void attachEventBase(
128  EventBase* eventBase,
129  InternalEnum internal = InternalEnum::NORMAL);
130 
140  void detachTimeoutManager();
141  void detachEventBase();
142 
144  return timeoutManager_;
145  }
146 
150  struct event* getEvent() {
151  return &event_;
152  }
153 
178  template <typename TCallback>
179  static std::unique_ptr<AsyncTimeout> make(
180  TimeoutManager& manager,
181  TCallback&& callback);
182 
212  template <typename TCallback>
213  static std::unique_ptr<AsyncTimeout> schedule(
215  TimeoutManager& manager,
216  TCallback&& callback);
217 
218  private:
219  static void libeventCallback(libevent_fd_t fd, short events, void* arg);
220 
221  struct event event_;
222 
223  /*
224  * Store a pointer to the TimeoutManager. We only use this
225  * for some assert() statements, to make sure that AsyncTimeout is always
226  * used from the correct thread.
227  */
229 
230  // Save the request context for when the timeout fires.
231  std::shared_ptr<RequestContext> context_;
232 };
233 
234 namespace detail {
235 
241 template <typename TCallback>
243  template <typename UCallback>
244  async_timeout_wrapper(TimeoutManager* manager, UCallback&& callback)
245  : AsyncTimeout(manager), callback_(std::forward<UCallback>(callback)) {}
246 
247  void timeoutExpired() noexcept override {
248  static_assert(
249  noexcept(std::declval<TCallback>()()),
250  "callback must be declared noexcept, e.g.: `[]() noexcept {}`");
251  callback_();
252  }
253 
254  private:
255  TCallback callback_;
256 };
257 
258 } // namespace detail
259 
260 template <typename TCallback>
261 std::unique_ptr<AsyncTimeout> AsyncTimeout::make(
262  TimeoutManager& manager,
263  TCallback&& callback) {
264  return std::unique_ptr<AsyncTimeout>(
266  std::addressof(manager), std::forward<TCallback>(callback)));
267 }
268 
269 template <typename TCallback>
270 std::unique_ptr<AsyncTimeout> AsyncTimeout::schedule(
272  TimeoutManager& manager,
273  TCallback&& callback) {
274  auto wrapper = AsyncTimeout::make(manager, std::forward<TCallback>(callback));
275  wrapper->scheduleTimeout(timeout);
276  return wrapper;
277 }
278 
279 } // namespace folly
struct event event_
Definition: AsyncTimeout.h:221
std::chrono::milliseconds timeout_type
void timeoutExpired() noexceptoverride
Definition: AsyncTimeout.h:247
const TimeoutManager * getTimeoutManager()
Definition: AsyncTimeout.h:143
STL namespace.
std::shared_ptr< RequestContext > context_
Definition: AsyncTimeout.h:231
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
struct event * getEvent()
Definition: AsyncTimeout.h:150
static std::unique_ptr< AsyncTimeout > make(TimeoutManager &manager, TCallback &&callback)
Definition: AsyncTimeout.h:261
static void libeventCallback(libevent_fd_t fd, short events, void *arg)
void attachTimeoutManager(TimeoutManager *timeoutManager, InternalEnum internal=InternalEnum::NORMAL)
static std::unique_ptr< AsyncTimeout > schedule(TimeoutManager::timeout_type timeout, TimeoutManager &manager, TCallback &&callback)
Definition: AsyncTimeout.h:270
async_timeout_wrapper(TimeoutManager *manager, UCallback &&callback)
Definition: AsyncTimeout.h:244
void attachEventBase(EventBase *eventBase, InternalEnum internal=InternalEnum::NORMAL)
virtual void timeoutExpired() noexcept=0
const
Definition: upload.py:398
bool scheduleTimeout(uint32_t milliseconds)
bool isScheduled() const
folly::Function< void()> callback_
TimeoutManager::InternalEnum InternalEnum
Definition: AsyncTimeout.h:38
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
int libevent_fd_t
Definition: Event.h:37