2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/IntrusiveList.h>
21 #include <folly/io/async/HHWheelTimer.h>
22 #include <folly/io/async/DelayedDestruction.h>
24 namespace folly { namespace wangle {
26 class ConnectionManager;
29 * Interface describing a connection that can be managed by a
30 * container such as an Acceptor.
32 class ManagedConnection:
33 public folly::HHWheelTimer::Callback,
34 public folly::DelayedDestruction {
41 virtual ~Callback() {}
43 /* Invoked when this connection becomes busy */
44 virtual void onActivated(ManagedConnection& conn) = 0;
46 /* Invoked when a connection becomes idle */
47 virtual void onDeactivated(ManagedConnection& conn) = 0;
50 // HHWheelTimer::Callback API (left for subclasses to implement).
51 virtual void timeoutExpired() noexcept = 0;
54 * Print a human-readable description of the connection.
55 * @param os Destination stream.
57 virtual void describe(std::ostream& os) const = 0;
60 * Check whether the connection has any requests outstanding.
62 virtual bool isBusy() const = 0;
65 * Get the idle time of the connection. If it returning 0, that means the idle
66 * connections will never be dropped during pre load shedding stage.
68 virtual std::chrono::milliseconds getIdleTime() const {
69 return std::chrono::milliseconds(0);
73 * Notify the connection that a shutdown is pending. This method will be
74 * called at the beginning of graceful shutdown.
76 virtual void notifyPendingShutdown() = 0;
79 * Instruct the connection that it should shutdown as soon as it is
80 * safe. This is called after notifyPendingShutdown().
82 virtual void closeWhenIdle() = 0;
85 * Forcibly drop a connection.
87 * If a request is in progress, this should cause the connection to be
88 * closed with a reset.
90 virtual void dropConnection() = 0;
93 * Dump the state of the connection to the log
95 virtual void dumpConnectionState(uint8_t loglevel) = 0;
98 * If the connection has a connection manager, reset the timeout countdown to
99 * connection manager's default timeout.
100 * @note If the connection manager doesn't have the connection scheduled
101 * for a timeout already, this method will schedule one. If the
102 * connection manager does have the connection connection scheduled
103 * for a timeout, this method will push back the timeout to N msec
104 * from now, where N is the connection manager's timer interval.
106 virtual void resetTimeout();
109 * If the connection has a connection manager, reset the timeout countdown to
110 * user specified timeout.
112 void resetTimeoutTo(std::chrono::milliseconds);
114 // Schedule an arbitrary timeout on the HHWheelTimer
115 virtual void scheduleTimeout(
116 folly::HHWheelTimer::Callback* callback,
117 std::chrono::milliseconds timeout);
119 ConnectionManager* getConnectionManager() {
120 return connectionManager_;
124 virtual ~ManagedConnection();
127 friend class ConnectionManager;
129 void setConnectionManager(ConnectionManager* mgr) {
130 connectionManager_ = mgr;
133 ConnectionManager* connectionManager_;
135 folly::SafeIntrusiveListHook listHook_;
138 std::ostream& operator<<(std::ostream& os, const ManagedConnection& conn);