(wangle) fix after-delete assert
[folly.git] / folly / experimental / wangle / 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
80    * countdown.
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   // Schedule an arbitrary timeout on the HHWheelTimer
90   virtual void scheduleTimeout(
91     folly::HHWheelTimer::Callback* callback,
92     std::chrono::milliseconds timeout);
93
94   ConnectionManager* getConnectionManager() {
95     return connectionManager_;
96   }
97
98  protected:
99   virtual ~ManagedConnection();
100
101  private:
102   friend class ConnectionManager;
103
104   void setConnectionManager(ConnectionManager* mgr) {
105     connectionManager_ = mgr;
106   }
107
108   ConnectionManager* connectionManager_;
109
110   folly::SafeIntrusiveListHook listHook_;
111 };
112
113 std::ostream& operator<<(std::ostream& os, const ManagedConnection& conn);
114
115 }} // folly::wangle