Move runAfterDelay/tryRunAfterDelay into TimeoutManager
[folly.git] / folly / io / async / TimeoutManager.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21 #include <folly/io/async/TimeoutManager.h>
22
23 #include <boost/intrusive/list.hpp>
24
25 #include <folly/Exception.h>
26 #include <folly/io/async/AsyncTimeout.h>
27
28 #include <glog/logging.h>
29
30 namespace folly {
31
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 {
36    public:
37     CobTimeout(TimeoutManager* timeoutManager, Func cob, InternalEnum internal)
38         : AsyncTimeout(timeoutManager, internal), cob_(std::move(cob)) {}
39
40     void timeoutExpired() noexcept override {
41       // For now, we just swallow any exceptions that the callback threw.
42       try {
43         cob_();
44       } catch (const std::exception& ex) {
45         LOG(ERROR) << "TimeoutManager::runAfterDelay() callback threw "
46                    << typeid(ex).name() << " exception: " << ex.what();
47       } catch (...) {
48         LOG(ERROR) << "TimeoutManager::runAfterDelay() callback threw "
49                    << "non-exception type";
50       }
51
52       // The CobTimeout object was allocated on the heap by runAfterDelay(),
53       // so delete it now that the it has fired.
54       delete this;
55     }
56
57    private:
58     Func cob_;
59
60    public:
61     using ListHook = boost::intrusive::list_member_hook<
62         boost::intrusive::link_mode<boost::intrusive::auto_unlink>>;
63     ListHook hook;
64     using List = boost::intrusive::list<
65         CobTimeout,
66         boost::intrusive::member_hook<CobTimeout, ListHook, &CobTimeout::hook>,
67         boost::intrusive::constant_time_size<false>>;
68   };
69
70   CobTimeout::List list;
71 };
72
73 TimeoutManager::TimeoutManager()
74     : cobTimeouts_(std::make_unique<CobTimeouts>()) {}
75
76 void TimeoutManager::runAfterDelay(
77     Func cob,
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");
83   }
84 }
85
86 bool TimeoutManager::tryRunAfterDelay(
87     Func cob,
88     uint32_t milliseconds,
89     InternalEnum internal) {
90   if (!cobTimeouts_) {
91     return false;
92   }
93
94   auto timeout =
95       std::make_unique<CobTimeouts::CobTimeout>(this, std::move(cob), internal);
96   if (!timeout->scheduleTimeout(milliseconds)) {
97     return false;
98   }
99   cobTimeouts_->list.push_back(*timeout.release());
100   return true;
101 }
102
103 void TimeoutManager::clearCobTimeouts() {
104   if (!cobTimeouts_) {
105     return;
106   }
107
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();
112     delete timeout;
113   }
114 }
115
116 TimeoutManager::~TimeoutManager() {
117   clearCobTimeouts();
118 }
119 }