TimeoutManager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2016-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. #include <folly/io/async/TimeoutManager.h>
  17. #include <boost/intrusive/list.hpp>
  18. #include <folly/Exception.h>
  19. #include <folly/Memory.h>
  20. #include <folly/io/async/AsyncTimeout.h>
  21. #include <glog/logging.h>
  22. namespace folly {
  23. struct TimeoutManager::CobTimeouts {
  24. // small object used as a callback arg with enough info to execute the
  25. // appropriate client-provided Cob
  26. class CobTimeout : public AsyncTimeout {
  27. public:
  28. CobTimeout(TimeoutManager* timeoutManager, Func cob, InternalEnum internal)
  29. : AsyncTimeout(timeoutManager, internal), cob_(std::move(cob)) {}
  30. void timeoutExpired() noexcept override {
  31. // For now, we just swallow any exceptions that the callback threw.
  32. try {
  33. cob_();
  34. } catch (const std::exception& ex) {
  35. LOG(ERROR) << "TimeoutManager::runAfterDelay() callback threw "
  36. << typeid(ex).name() << " exception: " << ex.what();
  37. } catch (...) {
  38. LOG(ERROR) << "TimeoutManager::runAfterDelay() callback threw "
  39. << "non-exception type";
  40. }
  41. // The CobTimeout object was allocated on the heap by runAfterDelay(),
  42. // so delete it now that the it has fired.
  43. delete this;
  44. }
  45. private:
  46. Func cob_;
  47. public:
  48. using ListHook = boost::intrusive::list_member_hook<
  49. boost::intrusive::link_mode<boost::intrusive::auto_unlink>>;
  50. ListHook hook;
  51. using List = boost::intrusive::list<
  52. CobTimeout,
  53. boost::intrusive::member_hook<CobTimeout, ListHook, &CobTimeout::hook>,
  54. boost::intrusive::constant_time_size<false>>;
  55. };
  56. CobTimeout::List list;
  57. };
  58. TimeoutManager::TimeoutManager()
  59. : cobTimeouts_(std::make_unique<CobTimeouts>()) {}
  60. void TimeoutManager::runAfterDelay(
  61. Func cob,
  62. uint32_t milliseconds,
  63. InternalEnum internal) {
  64. if (!tryRunAfterDelay(std::move(cob), milliseconds, internal)) {
  65. folly::throwSystemError(
  66. "error in TimeoutManager::runAfterDelay(), failed to schedule timeout");
  67. }
  68. }
  69. bool TimeoutManager::tryRunAfterDelay(
  70. Func cob,
  71. uint32_t milliseconds,
  72. InternalEnum internal) {
  73. if (!cobTimeouts_) {
  74. return false;
  75. }
  76. auto timeout =
  77. std::make_unique<CobTimeouts::CobTimeout>(this, std::move(cob), internal);
  78. if (!timeout->scheduleTimeout(milliseconds)) {
  79. return false;
  80. }
  81. cobTimeouts_->list.push_back(*timeout.release());
  82. return true;
  83. }
  84. void TimeoutManager::clearCobTimeouts() {
  85. if (!cobTimeouts_) {
  86. return;
  87. }
  88. // Delete any unfired callback objects, so that we don't leak memory
  89. // Note that we don't fire them.
  90. while (!cobTimeouts_->list.empty()) {
  91. auto* timeout = &cobTimeouts_->list.front();
  92. delete timeout;
  93. }
  94. }
  95. TimeoutManager::~TimeoutManager() {
  96. clearCobTimeouts();
  97. }
  98. } // namespace folly