proxygen
wangle::EvbHandshakeHelper Class Reference

#include <EvbHandshakeHelper.h>

Inheritance diagram for wangle::EvbHandshakeHelper:
wangle::AcceptorHandshakeHelper wangle::AcceptorHandshakeHelper::Callback folly::DelayedDestruction folly::DelayedDestructionBase

Public Member Functions

 EvbHandshakeHelper (AcceptorHandshakeHelper::UniquePtr helper, folly::EventBase *handshakeEvb)
 
void start (folly::AsyncSSLSocket::UniquePtr sock, AcceptorHandshakeHelper::Callback *callback) noexceptoverride
 
void dropConnection (SSLErrorEnum reason=SSLErrorEnum::NO_ERROR) override
 
virtual void connectionReady (folly::AsyncTransportWrapper::UniquePtr transport, std::string nextProtocol, SecureTransportType secureTransportType, folly::Optional< SSLErrorEnum > sslErr) noexceptoverride
 
virtual void connectionError (folly::AsyncTransportWrapper *transport, folly::exception_wrapper ex, folly::Optional< SSLErrorEnum > sslErr) noexceptoverride
 
- Public Member Functions inherited from folly::DelayedDestruction
virtual void destroy ()
 
bool getDestroyPending () const
 
- Public Member Functions inherited from folly::DelayedDestructionBase
virtual ~DelayedDestructionBase ()=default
 
- Public Member Functions inherited from wangle::AcceptorHandshakeHelper::Callback
virtual ~Callback ()=default
 

Protected Types

enum  HandshakeState : unsigned { HandshakeState::Invalid, HandshakeState::Started, HandshakeState::Dropped, HandshakeState::Callback }
 

Protected Member Functions

 ~EvbHandshakeHelper ()
 
- Protected Member Functions inherited from folly::DelayedDestruction
 ~DelayedDestruction () override=default
 
 DelayedDestruction ()
 
- Protected Member Functions inherited from folly::DelayedDestructionBase
 DelayedDestructionBase ()
 
uint32_t getDestructorGuardCount () const
 

Protected Attributes

AcceptorHandshakeHelper::UniquePtr helper_
 
AcceptorHandshakeHelper::Callbackcallback_
 
folly::EventBaseoriginalEvb_
 
folly::EventBasehandshakeEvb_
 
std::atomic< HandshakeStatestate_ {HandshakeState::Invalid}
 
folly::Optional< folly::DelayedDestruction::DestructorGuarddropConnectionGuard_
 

Private Member Functions

std::pair< bool, HandshakeStatetryTransition (HandshakeState expected, HandshakeState next)
 

Additional Inherited Members

- Public Types inherited from wangle::AcceptorHandshakeHelper
using UniquePtr = std::unique_ptr< AcceptorHandshakeHelper, folly::DelayedDestruction::Destructor >
 

Detailed Description

Wraps an existing AcceptorHandshakeHelper and callback. Performs handshakes under a specified handshake eventbase and delivers callback notifications in the original EventBase.

An example use case of this class would be to offload TLS handshakes under a separate EventBase thread rather than the Acceptor thread.

Definition at line 34 of file EvbHandshakeHelper.h.

Member Enumeration Documentation

enum wangle::EvbHandshakeHelper::HandshakeState : unsigned
strongprotected
Enumerator
Invalid 
Started 
Dropped 
Callback 

Definition at line 63 of file EvbHandshakeHelper.h.

63  : unsigned {
64  Invalid,
65  Started,
66  Dropped,
67  Callback,
68  };

Constructor & Destructor Documentation

wangle::EvbHandshakeHelper::EvbHandshakeHelper ( AcceptorHandshakeHelper::UniquePtr  helper,
folly::EventBase handshakeEvb 
)
inlineexplicit

Definition at line 37 of file EvbHandshakeHelper.h.

References connectionError(), connectionReady(), dropConnection(), wangle::NO_ERROR, folly::pushmi::__adl::noexcept(), start(), and string.

40  : helper_(std::move(helper)),
41  originalEvb_(nullptr),
42  handshakeEvb_(handshakeEvb) {}
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::EventBase * originalEvb_
folly::EventBase * handshakeEvb_
AcceptorHandshakeHelper::UniquePtr helper_
wangle::EvbHandshakeHelper::~EvbHandshakeHelper ( )
protected

Definition at line 166 of file EvbHandshakeHelper.cpp.

166  {
167  VLOG(5) << "evbhandshakehelper is destroyed";
168 }

Member Function Documentation

void wangle::EvbHandshakeHelper::connectionError ( folly::AsyncTransportWrapper transport,
folly::exception_wrapper  ex,
folly::Optional< SSLErrorEnum sslErr 
)
overridevirtualnoexcept

Called if an error was encountered while performing handshake.

If sslErr is set, Acceptor::updateSSLStats will be called.

Implements wangle::AcceptorHandshakeHelper::Callback.

Definition at line 126 of file EvbHandshakeHelper.cpp.

References Callback, callback_, folly::Optional< Value >::clear(), wangle::AcceptorHandshakeHelper::Callback::connectionError(), dropConnectionGuard_, handshakeEvb_, folly::Optional< Value >::hasValue(), helper_, folly::gen::move, originalEvb_, folly::EventBase::runInEventBaseThread(), Started, and tryTransition().

Referenced by EvbHandshakeHelper().

129  {
130  DCHECK(transport->getEventBase() == handshakeEvb_);
131 
132  auto transition =
134  if (!transition.first) {
135  VLOG(5) << "Ignoring call to connectionError(), expected state to be "
136  << static_cast<unsigned>(HandshakeState::Started)
137  << " but actual state was "
138  << static_cast<unsigned>(transition.second);
139  return;
140  }
141 
142  helper_.reset();
144  [this, sslErr, ex = std::move(ex)]() mutable {
145  DCHECK(callback_);
146  VLOG(5) << "calling underlying callback connectionError";
147 
148  // If a dropConnection call occured by the time this lambda runs, we
149  // don't want to fire the callback. (See Case 2)
152  return;
153  }
154  callback_->connectionError(nullptr, std::move(ex), sslErr);
155  });
156 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
AcceptorHandshakeHelper::Callback * callback_
folly::EventBase * originalEvb_
virtual EventBase * getEventBase() const =0
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
std::pair< bool, HandshakeState > tryTransition(HandshakeState expected, HandshakeState next)
folly::EventBase * handshakeEvb_
virtual void connectionError(folly::AsyncTransportWrapper *transport, folly::exception_wrapper ex, folly::Optional< SSLErrorEnum > sslErr) noexcept=0
AcceptorHandshakeHelper::UniquePtr helper_
folly::Optional< folly::DelayedDestruction::DestructorGuard > dropConnectionGuard_
void clear() noexcept
Definition: Optional.h:251
void wangle::EvbHandshakeHelper::connectionReady ( folly::AsyncTransportWrapper::UniquePtr  transport,
std::string  nextProtocol,
SecureTransportType  secureTransportType,
folly::Optional< SSLErrorEnum sslErr 
)
overridevirtualnoexcept

Called after handshake has been completed successfully.

If sslErr is set, Acceptor::updateSSLStats will be called.

Implements wangle::AcceptorHandshakeHelper::Callback.

Definition at line 81 of file EvbHandshakeHelper.cpp.

References Callback, callback_, folly::Optional< Value >::clear(), wangle::AcceptorHandshakeHelper::Callback::connectionReady(), dropConnectionGuard_, handshakeEvb_, folly::Optional< Value >::hasValue(), folly::gen::move, originalEvb_, folly::EventBase::runInEventBaseThread(), Started, and tryTransition().

Referenced by EvbHandshakeHelper().

85  {
86  DCHECK_EQ(transport->getEventBase(), handshakeEvb_);
87 
88  auto transition =
90  if (!transition.first) {
91  VLOG(5) << "Ignoring call to connectionReady(), expected state to be "
92  << static_cast<unsigned>(HandshakeState::Started)
93  << " but actual state was "
94  << static_cast<unsigned>(transition.second);
95  return;
96  }
97 
98  transport->detachEventBase();
99 
101  this,
102  secureTransportType,
103  sslErr,
104  transport = std::move(transport),
105  nextProtocol = std::move(nextProtocol)
106  ]() mutable {
107  DCHECK(callback_);
108  VLOG(5) << "calling underlying callback connectionReady";
109  transport->attachEventBase(originalEvb_);
110 
111  // If a dropConnection call occured by the time this lambda runs, we don't
112  // want to fire the callback. (See Case 2)
115  return;
116  }
117 
119  std::move(transport),
120  std::move(nextProtocol),
121  secureTransportType,
122  sslErr);
123  });
124 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
AcceptorHandshakeHelper::Callback * callback_
folly::EventBase * originalEvb_
virtual void connectionReady(folly::AsyncTransportWrapper::UniquePtr transport, std::string nextProtocol, SecureTransportType secureTransportType, folly::Optional< SSLErrorEnum > sslErr) noexcept=0
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
std::pair< bool, HandshakeState > tryTransition(HandshakeState expected, HandshakeState next)
folly::EventBase * handshakeEvb_
folly::Optional< folly::DelayedDestruction::DestructorGuard > dropConnectionGuard_
void clear() noexcept
Definition: Optional.h:251
void wangle::EvbHandshakeHelper::dropConnection ( SSLErrorEnum  reason = SSLErrorEnum::NO_ERROR)
overridevirtual

Implements wangle::AcceptorHandshakeHelper.

Definition at line 42 of file EvbHandshakeHelper.cpp.

References callback_, folly::Optional< Value >::clear(), wangle::AcceptorHandshakeHelper::Callback::connectionError(), folly::EventBase::dcheckIsInEventBaseThread(), dropConnectionGuard_, Dropped, folly::Optional< Value >::emplace(), handshakeEvb_, helper_, originalEvb_, folly::EventBase::runInEventBaseThread(), Started, and tryTransition().

Referenced by EvbHandshakeHelper().

42  {
43  CHECK(originalEvb_);
45 
46  auto transition =
48 
49  // Regardless of whether or not we win the race or not, we will set
50  // dropConnectionGuard_ (see case 2) to let a potential C' know that the
51  // connection was dropped. C', seeing this, is responsible for clearing
52  // the dropConnectionGuard.
55  nullptr,
56  folly::make_exception_wrapper<std::runtime_error>("connection dropped"),
57  reason);
58 
59  if (transition.first) {
60  // If we win the race to update the state, though, we are responsible
61  // for ensuring that dropConnection() is called on the handshake thread,
62  // and we are also the one responsible for clearing the dropConnectionGuard_
63  // that we set earlier.
64  handshakeEvb_->runInEventBaseThread([this, reason] {
65  VLOG(5) << "callback has not been received. dropConnection "
66  << "calling underlying helper";
67 
68  helper_->dropConnection(reason);
69 
70  // We need to ensure that the transport is destroyed in the handshake
71  // thread since the transport is currently attached to the handshake's
72  // event base.
73  helper_.reset();
74 
76  [this] { dropConnectionGuard_.clear(); });
77  });
78  }
79 }
AcceptorHandshakeHelper::Callback * callback_
folly::EventBase * originalEvb_
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
std::pair< bool, HandshakeState > tryTransition(HandshakeState expected, HandshakeState next)
folly::EventBase * handshakeEvb_
virtual void connectionError(folly::AsyncTransportWrapper *transport, folly::exception_wrapper ex, folly::Optional< SSLErrorEnum > sslErr) noexcept=0
Value & emplace(Args &&...args)
Definition: Optional.h:231
void dcheckIsInEventBaseThread() const
Definition: EventBase.h:520
AcceptorHandshakeHelper::UniquePtr helper_
folly::Optional< folly::DelayedDestruction::DestructorGuard > dropConnectionGuard_
void clear() noexcept
Definition: Optional.h:251
void wangle::EvbHandshakeHelper::start ( folly::AsyncSSLSocket::UniquePtr  sock,
AcceptorHandshakeHelper::Callback callback 
)
overridevirtualnoexcept

Implements wangle::AcceptorHandshakeHelper.

Definition at line 20 of file EvbHandshakeHelper.cpp.

References callback_, testing::internal::Invalid(), and folly::gen::move.

Referenced by EvbHandshakeHelper().

22  {
23  auto transition =
25  if (!transition.first) {
26  VLOG(5) << "Ignoring call to start(), since state is currently "
27  << static_cast<unsigned>(transition.second);
28  }
29 
30  callback_ = callback;
31  originalEvb_ = sock->getEventBase();
32  CHECK(originalEvb_);
33 
34  sock->detachEventBase();
36  [ this, sock = std::move(sock) ]() mutable {
37  sock->attachEventBase(handshakeEvb_);
38  helper_->start(std::move(sock), this);
39  });
40 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
AcceptorHandshakeHelper::Callback * callback_
folly::EventBase * originalEvb_
EventBase * getEventBase() override
Implements the IOExecutor interface.
Definition: EventBase.cpp:776
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
std::pair< bool, HandshakeState > tryTransition(HandshakeState expected, HandshakeState next)
folly::EventBase * handshakeEvb_
AcceptorHandshakeHelper::UniquePtr helper_
std::pair< bool, EvbHandshakeHelper::HandshakeState > wangle::EvbHandshakeHelper::tryTransition ( HandshakeState  expected,
HandshakeState  next 
)
private

Definition at line 159 of file EvbHandshakeHelper.cpp.

References state_.

Referenced by connectionError(), connectionReady(), and dropConnection().

161  {
162  bool res = state_.compare_exchange_strong(expected, next);
163  return std::make_pair(res, expected);
164 }
std::atomic< HandshakeState > state_
def next(obj)
Definition: ast.py:58

Member Data Documentation

AcceptorHandshakeHelper::Callback* wangle::EvbHandshakeHelper::callback_
protected

Definition at line 73 of file EvbHandshakeHelper.h.

Referenced by connectionError(), connectionReady(), and dropConnection().

folly::Optional<folly::DelayedDestruction::DestructorGuard> wangle::EvbHandshakeHelper::dropConnectionGuard_
protected

Definition at line 210 of file EvbHandshakeHelper.h.

Referenced by connectionError(), connectionReady(), and dropConnection().

folly::EventBase* wangle::EvbHandshakeHelper::handshakeEvb_
protected

Definition at line 76 of file EvbHandshakeHelper.h.

Referenced by connectionError(), connectionReady(), and dropConnection().

AcceptorHandshakeHelper::UniquePtr wangle::EvbHandshakeHelper::helper_
protected

Definition at line 72 of file EvbHandshakeHelper.h.

Referenced by connectionError(), and dropConnection().

folly::EventBase* wangle::EvbHandshakeHelper::originalEvb_
protected

Definition at line 75 of file EvbHandshakeHelper.h.

Referenced by connectionError(), connectionReady(), and dropConnection().

std::atomic<HandshakeState> wangle::EvbHandshakeHelper::state_ {HandshakeState::Invalid}
protected

Definition at line 204 of file EvbHandshakeHelper.h.

Referenced by tryTransition().


The documentation for this class was generated from the following files: