2 * Copyright 2016 Facebook, Inc.
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
21 #include <folly/io/async/TimeoutManager.h>
23 #include <boost/intrusive/list.hpp>
25 #include <folly/Exception.h>
26 #include <folly/io/async/AsyncTimeout.h>
28 #include <glog/logging.h>
32 struct TimeoutManager::CobTimeouts {
33 // small object used as a callback arg with enough info to execute the
34 // appropriate client-provided Cob
35 class CobTimeout : public AsyncTimeout {
37 CobTimeout(TimeoutManager* timeoutManager, Func cob, InternalEnum internal)
38 : AsyncTimeout(timeoutManager, internal), cob_(std::move(cob)) {}
40 void timeoutExpired() noexcept override {
41 // For now, we just swallow any exceptions that the callback threw.
44 } catch (const std::exception& ex) {
45 LOG(ERROR) << "TimeoutManager::runAfterDelay() callback threw "
46 << typeid(ex).name() << " exception: " << ex.what();
48 LOG(ERROR) << "TimeoutManager::runAfterDelay() callback threw "
49 << "non-exception type";
52 // The CobTimeout object was allocated on the heap by runAfterDelay(),
53 // so delete it now that the it has fired.
61 using ListHook = boost::intrusive::list_member_hook<
62 boost::intrusive::link_mode<boost::intrusive::auto_unlink>>;
64 using List = boost::intrusive::list<
66 boost::intrusive::member_hook<CobTimeout, ListHook, &CobTimeout::hook>,
67 boost::intrusive::constant_time_size<false>>;
70 CobTimeout::List list;
73 TimeoutManager::TimeoutManager()
74 : cobTimeouts_(std::make_unique<CobTimeouts>()) {}
76 void TimeoutManager::runAfterDelay(
78 uint32_t milliseconds,
79 InternalEnum internal) {
80 if (!tryRunAfterDelay(std::move(cob), milliseconds, internal)) {
81 folly::throwSystemError(
82 "error in TimeoutManager::runAfterDelay(), failed to schedule timeout");
86 bool TimeoutManager::tryRunAfterDelay(
88 uint32_t milliseconds,
89 InternalEnum internal) {
95 std::make_unique<CobTimeouts::CobTimeout>(this, std::move(cob), internal);
96 if (!timeout->scheduleTimeout(milliseconds)) {
99 cobTimeouts_->list.push_back(*timeout.release());
103 void TimeoutManager::clearCobTimeouts() {
108 // Delete any unfired callback objects, so that we don't leak memory
109 // Note that we don't fire them.
110 while (!cobTimeouts_->list.empty()) {
111 auto* timeout = &cobTimeouts_->list.front();
116 TimeoutManager::~TimeoutManager() {