2 * Copyright 2014 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 {
39 // HHWheelTimer::Callback API (left for subclasses to implement).
40 virtual void timeoutExpired() noexcept = 0;
43 * Print a human-readable description of the connection.
44 * @param os Destination stream.
46 virtual void describe(std::ostream& os) const = 0;
49 * Check whether the connection has any requests outstanding.
51 virtual bool isBusy() const = 0;
54 * Notify the connection that a shutdown is pending. This method will be
55 * called at the beginning of graceful shutdown.
57 virtual void notifyPendingShutdown() = 0;
60 * Instruct the connection that it should shutdown as soon as it is
61 * safe. This is called after notifyPendingShutdown().
63 virtual void closeWhenIdle() = 0;
66 * Forcibly drop a connection.
68 * If a request is in progress, this should cause the connection to be
69 * closed with a reset.
71 virtual void dropConnection() = 0;
74 * Dump the state of the connection to the log
76 virtual void dumpConnectionState(uint8_t loglevel) = 0;
79 * If the connection has a connection manager, reset the timeout
81 * @note If the connection manager doesn't have the connection scheduled
82 * for a timeout already, this method will schedule one. If the
83 * connection manager does have the connection connection scheduled
84 * for a timeout, this method will push back the timeout to N msec
85 * from now, where N is the connection manager's timer interval.
87 virtual void resetTimeout();
89 // Schedule an arbitrary timeout on the HHWheelTimer
90 virtual void scheduleTimeout(
91 folly::HHWheelTimer::Callback* callback,
92 std::chrono::milliseconds timeout);
94 ConnectionManager* getConnectionManager() {
95 return connectionManager_;
99 virtual ~ManagedConnection();
102 friend class ConnectionManager;
104 void setConnectionManager(ConnectionManager* mgr) {
105 connectionManager_ = mgr;
108 ConnectionManager* connectionManager_;
110 folly::SafeIntrusiveListHook listHook_;
113 std::ostream& operator<<(std::ostream& os, const ManagedConnection& conn);