proxygen
Acceptor.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 #pragma once
17 
27 
30 #include <wangle/ssl/SSLStats.h>
31 
32 #include <chrono>
33 #include <event.h>
34 #include <folly/ExceptionWrapper.h>
38 
39 namespace wangle {
40 
41 class AsyncTransport;
42 class ManagedConnection;
43 class SecurityProtocolContextManager;
44 class SSLContextManager;
45 
58 class Acceptor :
62  public:
63 
64  enum class State : uint32_t {
65  kInit, // not yet started
66  kRunning, // processing requests normally
67  kDraining, // processing outstanding conns, but not accepting new ones
68  kDone, // no longer accepting, and all connections finished
69  };
70 
71  explicit Acceptor(const ServerSocketConfig& accConfig);
72  ~Acceptor() override;
73 
78  virtual void setSSLCacheProvider(
79  const std::shared_ptr<SSLCacheProvider>& cacheProvider) {
80  cacheProvider_ = cacheProvider;
81  }
82 
90  virtual void init(folly::AsyncServerSocket* serverSocket,
91  folly::EventBase* eventBase,
92  SSLStats* stats = nullptr);
93 
97  virtual void resetSSLContextConfigs();
98 
102  void addSSLContextConfig(const SSLContextConfig& sslCtxConfig);
103 
105  return sslCtxManager_.get();
106  }
107 
111  virtual void setTLSTicketSecrets(
112  const std::vector<std::string>& oldSecrets,
113  const std::vector<std::string>& currentSecrets,
114  const std::vector<std::string>& newSecrets);
115 
121  (uint32_t)downstreamConnectionManager_->getNumConnections() : 0;
122  }
123 
127  virtual folly::EventBase* getEventBase() const { return base_; }
128 
133  return downstreamConnectionManager_.get();
134  }
135 
143  void addConnection(wangle::ManagedConnection* connection);
144 
148  State getState() const {
149  return state_;
150  }
151 
155  std::chrono::milliseconds getConnTimeout() const;
156 
162  const std::string& getName() const {
163  return accConfig_.name;
164  }
165 
169  std::chrono::milliseconds getSSLHandshakeTimeout() const {
171  }
172 
178  void setGracefulShutdownTimeout(std::chrono::milliseconds gracefulShutdown) {
179  gracefulShutdownTimeout_ = gracefulShutdown;
180  }
181 
182  std::chrono::milliseconds getGracefulShutdownTimeout() const {
184  }
185 
193  virtual void forceStop();
194 
195  bool isSSL() const { return accConfig_.isSSL(); }
196 
197  const ServerSocketConfig& getConfig() const { return accConfig_; }
198 
200  return totalNumPendingSSLConns_.load();
201  }
202 
207  virtual void onDoneAcceptingConnection(
208  int fd,
209  const folly::SocketAddress& clientAddr,
210  std::chrono::steady_clock::time_point acceptTime
211  ) noexcept;
212 
217  int fd,
218  const folly::SocketAddress& clientAddr,
219  std::chrono::steady_clock::time_point acceptTime,
220  TransportInfo& tinfo
221  ) noexcept;
222 
226  virtual void startHandshakeManager(
228  Acceptor* acceptor,
229  const folly::SocketAddress& clientAddr,
230  std::chrono::steady_clock::time_point acceptTime,
231  TransportInfo& tinfo) noexcept;
232 
237  void drainAllConnections();
238 
242  virtual void drainConnections(double pctToDrain);
243 
250  void dropAllConnections();
251 
259  virtual void dropConnections(double pctToDrop);
260 
265  virtual void plaintextConnectionReady(
267  const folly::SocketAddress& clientAddr,
268  const std::string& nextProtocolName,
269  SecureTransportType secureTransportType,
270  TransportInfo& tinfo);
271 
278  void connectionReady(
280  const folly::SocketAddress& clientAddr,
281  const std::string& nextProtocolName,
282  SecureTransportType secureTransportType,
283  TransportInfo& tinfo);
284 
290  const folly::SocketAddress& clientAddr,
291  const std::string& nextProtocol,
292  SecureTransportType secureTransportType,
293  TransportInfo& tinfo);
294 
298  virtual void sslConnectionError(const folly::exception_wrapper& ex);
299 
305  virtual void updateSSLStats(
306  const folly::AsyncTransportWrapper* /*sock*/,
307  std::chrono::milliseconds /*acceptLatency*/,
308  SSLErrorEnum /*error*/) noexcept {}
309 
310  protected:
311 
320 
321  virtual uint64_t getConnectionCountForLoadShedding(void) const { return 0; }
322  virtual uint64_t getActiveConnectionCountForLoadShedding() const { return 0; }
325  }
326 
331  virtual bool canAccept(const folly::SocketAddress&);
332 
350  virtual void onNewConnection(
352  const folly::SocketAddress* /*address*/,
353  const std::string& /*nextProtocolName*/,
354  SecureTransportType /*secureTransportType*/,
355  const TransportInfo& /*tinfo*/) {}
356 
357  void onListenStarted() noexcept override {}
358  void onListenStopped() noexcept override {}
360  std::shared_ptr<folly::AsyncUDPSocket> /*socket*/,
361  const folly::SocketAddress&,
362  std::unique_ptr<folly::IOBuf>,
363  bool) noexcept override {}
364 
366  folly::EventBase* base,
367  int fd) {
369  new folly::AsyncSocket(base, fd));
370  }
371 
373  const std::shared_ptr<folly::SSLContext>& ctx, folly::EventBase* base, int fd) {
376  ctx,
377  base,
378  fd,
379  true, /* set server */
380  true /* defer the security negotiation until sslAccept */));
381  }
382 
383  protected:
384 
392  virtual void onConnectionsDrained() {}
393 
394  // AsyncServerSocket::AcceptCallback methods
395  void connectionAccepted(
396  int fd,
397  const folly::SocketAddress& clientAddr) noexcept override;
398  void acceptError(const std::exception& ex) noexcept override;
399  void acceptStopped() noexcept override;
400 
401  // ConnectionManager::Callback methods
402  void onEmpty(const wangle::ConnectionManager& cm) override;
403  void onConnectionAdded(const ManagedConnection*) override {}
404  void onConnectionRemoved(const ManagedConnection*) override {}
405 
406  protected:
408  void setLoadShedConfig(
409  std::shared_ptr<const LoadShedConfiguration> loadShedConfig,
410  const IConnectionCounter* counter);
411 
412  // Helper function to initialize downstreamConnectionManager_
413  virtual void initDownstreamConnectionManager(folly::EventBase* eventBase);
415  return &defaultFizzPeeker_;
416  }
417  virtual std::shared_ptr<fizz::server::FizzServerContext> createFizzContext();
418  virtual std::shared_ptr<fizz::server::TicketCipher>
421 
426 
427  std::unique_ptr<SSLContextManager> sslCtxManager_;
428 
433 
437 
439 
440  std::shared_ptr<SSLCacheProvider> cacheProvider_;
442 
443  private:
444 
445  // Forbidden copy constructor and assignment opererator
446  Acceptor(Acceptor const &) = delete;
447  Acceptor& operator=(Acceptor const &) = delete;
448 
449  void checkDrained();
450 
453 
454  static std::atomic<uint64_t> totalNumPendingSSLConns_;
455 
457  std::shared_ptr<const LoadShedConfiguration> loadShedConfig_{nullptr};
459  std::chrono::milliseconds gracefulShutdownTimeout_{5000};
460 };
461 
463  public:
464  virtual std::shared_ptr<Acceptor> newAcceptor(folly::EventBase*) = 0;
465  virtual ~AcceptorFactory() = default;
466 };
467 
468 } // namespace
std::chrono::milliseconds getConnTimeout() const
Definition: Acceptor.cpp:497
void onEmpty(const wangle::ConnectionManager &cm) override
Definition: Acceptor.cpp:458
const ServerSocketConfig & getConfig() const
Definition: Acceptor.h:197
virtual uint64_t getWorkerMaxConnections() const
Definition: Acceptor.h:323
virtual folly::AsyncSocket::UniquePtr makeNewAsyncSocket(folly::EventBase *base, int fd)
Definition: Acceptor.h:365
void connectionReady(folly::AsyncTransportWrapper::UniquePtr sock, const folly::SocketAddress &clientAddr, const std::string &nextProtocolName, SecureTransportType secureTransportType, TransportInfo &tinfo)
Definition: Acceptor.cpp:365
void setLoadShedConfig(std::shared_ptr< const LoadShedConfiguration > loadShedConfig, const IConnectionCounter *counter)
Definition: Acceptor.cpp:221
uint32_t getNumConnections() const
Definition: Acceptor.h:119
void addSSLContextConfig(const SSLContextConfig &sslCtxConfig)
Definition: Acceptor.cpp:187
~Acceptor() override
Definition: Acceptor.cpp:184
virtual void setTLSTicketSecrets(const std::vector< std::string > &oldSecrets, const std::vector< std::string > &currentSecrets, const std::vector< std::string > &newSecrets)
Definition: Acceptor.cpp:195
State getState() const
Definition: Acceptor.h:148
SSLErrorEnum
Definition: SSLUtil.h:42
std::chrono::milliseconds getSSLHandshakeTimeout() const
Definition: Acceptor.h:169
wangle::TLSTicketKeySeeds currentSecrets_
Definition: Acceptor.h:441
uint64_t numPendingSSLConns_
Definition: Acceptor.h:452
void processEstablishedConnection(int fd, const folly::SocketAddress &clientAddr, std::chrono::steady_clock::time_point acceptTime, TransportInfo &tinfo) noexcept
Definition: Acceptor.cpp:304
std::unique_ptr< ConnectionManager, Destructor > UniquePtr
virtual uint64_t getMaxConnections() const =0
const IConnectionCounter * connectionCounter_
Definition: Acceptor.h:458
static uint64_t getTotalNumPendingSSLConns()
Definition: Acceptor.h:199
std::shared_ptr< SSLCacheProvider > cacheProvider_
Definition: Acceptor.h:440
void checkDrained()
Definition: Acceptor.cpp:466
SSLContextManager * getSSLContextManager() const
Definition: Acceptor.h:104
virtual void initDownstreamConnectionManager(folly::EventBase *eventBase)
Definition: Acceptor.cpp:126
wangle::ConnectionManager::UniquePtr downstreamConnectionManager_
Definition: Acceptor.h:438
bool forceShutdownInProgress_
Definition: Acceptor.h:456
std::shared_ptr< const LoadShedConfiguration > loadShedConfig_
Definition: Acceptor.h:457
virtual void onDoneAcceptingConnection(int fd, const folly::SocketAddress &clientAddr, std::chrono::steady_clock::time_point acceptTime) noexcept
Definition: Acceptor.cpp:295
virtual void dropConnections(double pctToDrop)
Definition: Acceptor.cpp:530
requires E e noexcept(noexcept(s.error(std::move(e))))
virtual void setSSLCacheProvider(const std::shared_ptr< SSLCacheProvider > &cacheProvider)
Definition: Acceptor.h:78
static std::atomic< uint64_t > totalNumPendingSSLConns_
Definition: Acceptor.h:454
virtual void drainConnections(double pctToDrain)
Definition: Acceptor.cpp:485
virtual void updateSSLStats(const folly::AsyncTransportWrapper *, std::chrono::milliseconds, SSLErrorEnum) noexcept
Definition: Acceptor.h:305
virtual std::shared_ptr< fizz::server::TicketCipher > createFizzTicketCipher(folly::Optional< std::string >=folly::none)
Definition: Acceptor.cpp:140
void onDataAvailable(std::shared_ptr< folly::AsyncUDPSocket >, const folly::SocketAddress &, std::unique_ptr< folly::IOBuf >, bool) noexceptoverride
Definition: Acceptor.h:359
void acceptStopped() noexceptoverride
Definition: Acceptor.cpp:441
std::chrono::milliseconds getGracefulShutdownTimeout() const
Definition: Acceptor.h:182
virtual void startHandshakeManager(folly::AsyncSSLSocket::UniquePtr sslSock, Acceptor *acceptor, const folly::SocketAddress &clientAddr, std::chrono::steady_clock::time_point acceptTime, TransportInfo &tinfo) noexcept
Definition: Acceptor.cpp:353
void acceptError(const std::exception &ex) noexceptoverride
Definition: Acceptor.cpp:432
folly::AsyncSocket::OptionMap socketOptions_
Definition: Acceptor.h:425
const std::string & getName() const
Definition: Acceptor.h:162
std::unique_ptr< AsyncSSLSocket, Destructor > UniquePtr
std::map< OptionKey, int > OptionMap
Definition: AsyncSocket.h:376
std::unique_ptr< AsyncTransportWrapper, Destructor > UniquePtr
virtual void onConnectionsDrained()
Definition: Acceptor.h:392
bool isSSL() const
Definition: Acceptor.h:195
std::chrono::milliseconds sslHandshakeTimeout
void updateFizzContext(fizz::server::FizzServerContext *)
Definition: Acceptor.cpp:149
Acceptor(const ServerSocketConfig &accConfig)
Definition: Acceptor.cpp:54
virtual void forceStop()
Definition: Acceptor.cpp:508
virtual DefaultToFizzPeekingCallback * getFizzPeeker()
Definition: Acceptor.h:414
const ServerSocketConfig accConfig_
Definition: Acceptor.h:407
void addConnection(wangle::ManagedConnection *connection)
Definition: Acceptor.cpp:501
virtual uint64_t getActiveConnectionCountForLoadShedding() const
Definition: Acceptor.h:322
virtual void plaintextConnectionReady(folly::AsyncTransportWrapper::UniquePtr sock, const folly::SocketAddress &clientAddr, const std::string &nextProtocolName, SecureTransportType secureTransportType, TransportInfo &tinfo)
Definition: Acceptor.cpp:388
virtual uint64_t getConnectionCountForLoadShedding(void) const
Definition: Acceptor.h:321
DefaultToSSLPeekingCallback defaultPeekingCallback_
Definition: Acceptor.h:435
SecurityProtocolContextManager securityProtocolCtxManager_
Definition: Acceptor.h:432
virtual void sslConnectionReady(folly::AsyncTransportWrapper::UniquePtr sock, const folly::SocketAddress &clientAddr, const std::string &nextProtocol, SecureTransportType secureTransportType, TransportInfo &tinfo)
Definition: Acceptor.cpp:403
void onListenStarted() noexceptoverride
Definition: Acceptor.h:357
void onListenStopped() noexceptoverride
Definition: Acceptor.h:358
virtual std::shared_ptr< fizz::server::FizzServerContext > createFizzContext()
Definition: Acceptor.cpp:135
virtual wangle::ConnectionManager * getConnectionManager()
Definition: Acceptor.h:132
std::atomic< int > counter
const char * string
Definition: Conv.cpp:212
std::chrono::milliseconds gracefulShutdownTimeout_
Definition: Acceptor.h:459
virtual bool canAccept(const folly::SocketAddress &)
Definition: Acceptor.cpp:228
virtual void sslConnectionError(const folly::exception_wrapper &ex)
Definition: Acceptor.cpp:422
void connectionAccepted(int fd, const folly::SocketAddress &clientAddr) noexceptoverride
Definition: Acceptor.cpp:277
void onConnectionRemoved(const ManagedConnection *) override
Definition: Acceptor.h:404
void setGracefulShutdownTimeout(std::chrono::milliseconds gracefulShutdown)
Definition: Acceptor.h:178
TLSPlaintextPeekingCallback tlsPlaintextPeekingCallback_
Definition: Acceptor.h:434
virtual void resetSSLContextConfigs()
Definition: Acceptor.cpp:162
virtual folly::EventBase * getEventBase() const
Definition: Acceptor.h:127
DefaultToFizzPeekingCallback defaultFizzPeeker_
Definition: Acceptor.h:436
std::unique_ptr< AsyncSocket, Destructor > UniquePtr
Definition: AsyncSocket.h:83
void onConnectionAdded(const ManagedConnection *) override
Definition: Acceptor.h:403
std::unique_ptr< SSLContextManager > sslCtxManager_
Definition: Acceptor.h:427
virtual folly::AsyncSSLSocket::UniquePtr makeNewAsyncSSLSocket(const std::shared_ptr< folly::SSLContext > &ctx, folly::EventBase *base, int fd)
Definition: Acceptor.h:372
folly::EventBase * base_
Definition: Acceptor.h:319
virtual void init(folly::AsyncServerSocket *serverSocket, folly::EventBase *eventBase, SSLStats *stats=nullptr)
Definition: Acceptor.cpp:60
virtual void onNewConnection(folly::AsyncTransportWrapper::UniquePtr, const folly::SocketAddress *, const std::string &, SecureTransportType, const TransportInfo &)
Definition: Acceptor.h:350
constexpr None none
Definition: Optional.h:87
void drainAllConnections()
Definition: Acceptor.cpp:214
void dropAllConnections()
Definition: Acceptor.cpp:513
Acceptor & operator=(Acceptor const &)=delete