Get *=default*ed default constructors
[folly.git] / folly / wangle / acceptor / ManagedConnection.h
1 /*
2  * Copyright 2015 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   class Callback {
40   public:
41     virtual ~Callback() = default;
42
43     /* Invoked when this connection becomes busy */
44     virtual void onActivated(ManagedConnection& conn) = 0;
45
46     /* Invoked when a connection becomes idle */
47     virtual void onDeactivated(ManagedConnection& conn) = 0;
48   };
49
50   // HHWheelTimer::Callback API (left for subclasses to implement).
51   virtual void timeoutExpired() noexcept = 0;
52
53   /**
54    * Print a human-readable description of the connection.
55    * @param os Destination stream.
56    */
57   virtual void describe(std::ostream& os) const = 0;
58
59   /**
60    * Check whether the connection has any requests outstanding.
61    */
62   virtual bool isBusy() const = 0;
63
64   /**
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.
67    */
68   virtual std::chrono::milliseconds getIdleTime() const {
69     return std::chrono::milliseconds(0);
70   }
71
72   /**
73    * Notify the connection that a shutdown is pending. This method will be
74    * called at the beginning of graceful shutdown.
75    */
76   virtual void notifyPendingShutdown() = 0;
77
78   /**
79    * Instruct the connection that it should shutdown as soon as it is
80    * safe. This is called after notifyPendingShutdown().
81    */
82   virtual void closeWhenIdle() = 0;
83
84   /**
85    * Forcibly drop a connection.
86    *
87    * If a request is in progress, this should cause the connection to be
88    * closed with a reset.
89    */
90   virtual void dropConnection() = 0;
91
92   /**
93    * Dump the state of the connection to the log
94    */
95   virtual void dumpConnectionState(uint8_t loglevel) = 0;
96
97   /**
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.
105    */
106   virtual void resetTimeout();
107
108   /**
109    * If the connection has a connection manager, reset the timeout countdown to
110    * user specified timeout.
111    */
112   void resetTimeoutTo(std::chrono::milliseconds);
113
114   // Schedule an arbitrary timeout on the HHWheelTimer
115   virtual void scheduleTimeout(
116     folly::HHWheelTimer::Callback* callback,
117     std::chrono::milliseconds timeout);
118
119   ConnectionManager* getConnectionManager() {
120     return connectionManager_;
121   }
122
123  protected:
124   virtual ~ManagedConnection();
125
126  private:
127   friend class ConnectionManager;
128
129   void setConnectionManager(ConnectionManager* mgr) {
130     connectionManager_ = mgr;
131   }
132
133   ConnectionManager* connectionManager_;
134
135   folly::SafeIntrusiveListHook listHook_;
136 };
137
138 std::ostream& operator<<(std::ostream& os, const ManagedConnection& conn);
139
140 }} // folly::wangle