2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
21 #include <folly/io/async/TimeoutManager.h>
23 #include <boost/noncopyable.hpp>
34 * AsyncTimeout is used to asynchronously wait for a timeout to occur.
36 class AsyncTimeout : private boost::noncopyable {
38 typedef TimeoutManager::InternalEnum InternalEnum;
41 * Create a new AsyncTimeout object, driven by the specified TimeoutManager.
43 explicit AsyncTimeout(TimeoutManager* timeoutManager);
44 explicit AsyncTimeout(EventBase* eventBase);
47 * Create a new internal AsyncTimeout object.
49 * Internal timeouts are like regular timeouts, but will not stop the
50 * TimeoutManager loop from exiting if the only remaining events are internal
53 * This is useful for implementing fallback timeouts to abort the
54 * TimeoutManager loop if the other events have not been processed within a
55 * specified time period: if the event loop takes too long the timeout will
56 * fire and can stop the event loop. However, if all other events complete,
57 * the event loop will exit even though the internal timeout is still
60 AsyncTimeout(TimeoutManager* timeoutManager, InternalEnum internal);
61 AsyncTimeout(EventBase* eventBase, InternalEnum internal);
64 * Create a new AsyncTimeout object, not yet assigned to a TimeoutManager.
66 * attachEventBase() must be called prior to scheduling the timeout.
71 * AsyncTimeout destructor.
73 * The timeout will be automatically cancelled if it is running.
75 virtual ~AsyncTimeout();
78 * timeoutExpired() is invoked when the timeout period has expired.
80 virtual void timeoutExpired() noexcept = 0;
83 * Schedule the timeout to fire in the specified number of milliseconds.
85 * After the specified number of milliseconds has elapsed, timeoutExpired()
86 * will be invoked by the TimeoutManager's main loop.
88 * If the timeout is already running, it will be rescheduled with the
91 * @param milliseconds The timeout duration, in milliseconds.
93 * @return Returns true if the timeout was successfully scheduled,
94 * and false if an error occurred. After an error, the timeout is
95 * always unscheduled, even if scheduleTimeout() was just
96 * rescheduling an existing timeout.
98 bool scheduleTimeout(uint32_t milliseconds);
99 bool scheduleTimeout(std::chrono::milliseconds timeout);
102 * Cancel the timeout, if it is running.
104 void cancelTimeout();
107 * Returns true if the timeout is currently scheduled.
109 bool isScheduled() const;
112 * Attach the timeout to a TimeoutManager.
114 * This may only be called if the timeout is not currently attached to a
115 * TimeoutManager (either by using the default constructor, or by calling
116 * detachTimeoutManager()).
118 * This method must be invoked in the TimeoutManager's thread.
120 * The internal parameter specifies if this timeout should be treated as an
121 * internal event. TimeoutManager::loop() will return when there are no more
122 * non-internal events remaining.
124 void attachTimeoutManager(TimeoutManager* timeoutManager,
125 InternalEnum internal = InternalEnum::NORMAL);
126 void attachEventBase(EventBase* eventBase,
127 InternalEnum internal = InternalEnum::NORMAL);
130 * Detach the timeout from its TimeoutManager.
132 * This may only be called when the timeout is not running.
133 * Once detached, the timeout may not be scheduled again until it is
134 * re-attached to a EventBase by calling attachEventBase().
136 * This method must be called from the current TimeoutManager's thread.
138 void detachTimeoutManager();
139 void detachEventBase();
142 * Returns the internal handle to the event
144 struct event* getEvent() {
149 static void libeventCallback(int fd, short events, void* arg);
154 * Store a pointer to the TimeoutManager. We only use this
155 * for some assert() statements, to make sure that AsyncTimeout is always
156 * used from the correct thread.
158 TimeoutManager* timeoutManager_;
160 // Save the request context for when the timeout fires.
161 std::shared_ptr<RequestContext> context_;