0fd419774fd0f0b713a1e76a977c0a0519e7cb12
[folly.git] / folly / wangle / acceptor / ManagedConnection.h
1 /*
2  * Copyright 2014 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
17 #pragma once
18
19 #include <folly/IntrusiveList.h>
20 #include <ostream>
21 #include <folly/io/async/HHWheelTimer.h>
22 #include <folly/io/async/DelayedDestruction.h>
23
24 namespace folly { namespace wangle {
25
26 class ConnectionManager;
27
28 /**
29  * Interface describing a connection that can be managed by a
30  * container such as an Acceptor.
31  */
32 class ManagedConnection:
33     public folly::HHWheelTimer::Callback,
34     public folly::DelayedDestruction {
35  public:
36
37   ManagedConnection();
38
39   // HHWheelTimer::Callback API (left for subclasses to implement).
40   virtual void timeoutExpired() noexcept = 0;
41
42   /**
43    * Print a human-readable description of the connection.
44    * @param os Destination stream.
45    */
46   virtual void describe(std::ostream& os) const = 0;
47
48   /**
49    * Check whether the connection has any requests outstanding.
50    */
51   virtual bool isBusy() const = 0;
52
53   /**
54    * Notify the connection that a shutdown is pending. This method will be
55    * called at the beginning of graceful shutdown.
56    */
57   virtual void notifyPendingShutdown() = 0;
58
59   /**
60    * Instruct the connection that it should shutdown as soon as it is
61    * safe. This is called after notifyPendingShutdown().
62    */
63   virtual void closeWhenIdle() = 0;
64
65   /**
66    * Forcibly drop a connection.
67    *
68    * If a request is in progress, this should cause the connection to be
69    * closed with a reset.
70    */
71   virtual void dropConnection() = 0;
72
73   /**
74    * Dump the state of the connection to the log
75    */
76   virtual void dumpConnectionState(uint8_t loglevel) = 0;
77
78   /**
79    * If the connection has a connection manager, reset the timeout countdown to
80    * connection manager's default 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.
86    */
87   virtual void resetTimeout();
88
89   /**
90    * If the connection has a connection manager, reset the timeout countdown to
91    * user specified timeout.
92    */
93   void resetTimeoutTo(std::chrono::milliseconds);
94
95   // Schedule an arbitrary timeout on the HHWheelTimer
96   virtual void scheduleTimeout(
97     folly::HHWheelTimer::Callback* callback,
98     std::chrono::milliseconds timeout);
99
100   ConnectionManager* getConnectionManager() {
101     return connectionManager_;
102   }
103
104  protected:
105   virtual ~ManagedConnection();
106
107  private:
108   friend class ConnectionManager;
109
110   void setConnectionManager(ConnectionManager* mgr) {
111     connectionManager_ = mgr;
112   }
113
114   ConnectionManager* connectionManager_;
115
116   folly::SafeIntrusiveListHook listHook_;
117 };
118
119 std::ostream& operator<<(std::ostream& os, const ManagedConnection& conn);
120
121 }} // folly::wangle