proxygen
ManagedConnection.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present 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>
23 
24 namespace wangle {
25 
26 class ConnectionManager;
27 
35  public:
36 
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  void timeoutExpired() noexcept override = 0;
52 
57  virtual void describe(std::ostream& os) const = 0;
58 
62  virtual bool isBusy() const = 0;
63 
68  virtual std::chrono::milliseconds getIdleTime() const {
69  return std::chrono::milliseconds(0);
70  }
71 
76  virtual void notifyPendingShutdown() = 0;
77 
79  if (state_ == DrainState::NONE) {
82  }
83  }
84 
89  virtual void closeWhenIdle() = 0;
90 
91  void fireCloseWhenIdle(bool force_to_close = false) {
92  if (force_to_close || state_ == DrainState::SENT_NOTIFY_PENDING_SHUTDOWN) {
94  closeWhenIdle();
95  }
96  }
97 
104  virtual void dropConnection() = 0;
105 
109  virtual void dumpConnectionState(uint8_t loglevel) = 0;
110 
120  virtual void resetTimeout();
121 
126  void resetTimeoutTo(std::chrono::milliseconds);
127 
128  // Schedule an arbitrary timeout on the HHWheelTimer
129  virtual void scheduleTimeout(
131  std::chrono::milliseconds timeout);
132 
134  return connectionManager_;
135  }
136 
137  protected:
138  ~ManagedConnection() override;
139 
140  private:
141  enum class DrainState {
142  NONE,
143  SENT_NOTIFY_PENDING_SHUTDOWN,
144  SENT_CLOSE_WHEN_IDLE,
145  };
146 
148 
149  friend class ConnectionManager;
150 
152  connectionManager_ = mgr;
153  }
154 
156 
158 
159 };
160 
161 std::ostream& operator<<(std::ostream& os, const ManagedConnection& conn);
162 
163 } // wangle
virtual void notifyPendingShutdown()=0
virtual void scheduleTimeout(folly::HHWheelTimer::Callback *callback, std::chrono::milliseconds timeout)
virtual void onActivated(ManagedConnection &conn)=0
folly::SafeIntrusiveListHook listHook_
requires E e noexcept(noexcept(s.error(std::move(e))))
virtual void dropConnection()=0
void resetTimeoutTo(std::chrono::milliseconds)
void setConnectionManager(ConnectionManager *mgr)
virtual void onDeactivated(ManagedConnection &conn)=0
virtual void describe(std::ostream &os) const =0
void fireCloseWhenIdle(bool force_to_close=false)
ConnectionManager * getConnectionManager()
void timeoutExpired() noexceptoverride=0
virtual bool isBusy() const =0
boost::intrusive::list_member_hook< boost::intrusive::link_mode< boost::intrusive::safe_link >> SafeIntrusiveListHook
Definition: IntrusiveList.h:74
ConnectionManager * connectionManager_
std::ostream & operator<<(std::ostream &os, const ManagedConnection &conn)
virtual std::chrono::milliseconds getIdleTime() const
virtual void closeWhenIdle()=0
virtual void dumpConnectionState(uint8_t loglevel)=0