Create dedicate crate for php_escaping.rs
[hiphop-php.git] / hphp / runtime / server / warmup-request-handler.h
blob3a6db919069f15393266522ee9fd55abf352f6da
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_WARMUP_REQUEST_HANDLER_H_
18 #define incl_HPHP_WARMUP_REQUEST_HANDLER_H_
20 #include <memory>
22 #include "hphp/runtime/server/server.h"
23 #include "hphp/runtime/server/http-request-handler.h"
24 #include "hphp/util/job-queue.h"
26 namespace HPHP {
27 ///////////////////////////////////////////////////////////////////////////////
29 struct WarmupRequestHandlerFactory;
31 /**
32 * WarmupRequestHandler is a small shim on top of HttpRequestHandler.
33 * It counts the number of requests, and adds additional worker threads to the
34 * server after a specified threshold.
36 struct WarmupRequestHandler : RequestHandler {
37 explicit WarmupRequestHandler(
38 int timeout,
39 const std::shared_ptr<WarmupRequestHandlerFactory>& factory)
40 : RequestHandler(timeout), m_factory(factory), m_reqHandler(timeout) {}
42 void setupRequest(Transport* transport) override;
43 void teardownRequest(Transport* transport) noexcept override;
44 void handleRequest(Transport* transport) override;
45 void abortRequest(Transport* transport) override;
46 void logToAccessLog(Transport* transport) override;
48 private:
49 std::shared_ptr<WarmupRequestHandlerFactory> m_factory;
50 HttpRequestHandler m_reqHandler;
53 struct WarmupRequestHandlerFactory
54 : std::enable_shared_from_this<WarmupRequestHandlerFactory>
56 WarmupRequestHandlerFactory(Server *server,
57 uint32_t reqCount,
58 int timeout)
59 : m_reqNumber(0),
60 m_warmupReqThreshold(reqCount),
61 m_timeout(timeout),
62 m_server(server) {}
64 std::unique_ptr<RequestHandler> createHandler();
66 void bumpReqCount();
68 private:
69 std::atomic<uint32_t> m_reqNumber;
70 uint32_t const m_warmupReqThreshold;
71 int m_timeout;
72 // The server owns this object so will by definition outlive us
73 Server *m_server;
76 struct WarmupJob {
77 const std::string hdfFile;
78 unsigned index;
81 struct InternalWarmupWorker : JobQueueWorker<WarmupJob> {
82 void doJob(WarmupJob job) override;
85 struct InternalWarmupRequestPlayer : JobQueueDispatcher<InternalWarmupWorker> {
86 // Don't inline into header file without testing performance on MacOS:
87 // https://github.com/facebook/hhvm/issues/8515
88 // Problem tested on 2019-06-11 on MacOS High Sierra and Mojave
89 // Apple LLVM version 10.0.1 (clang-1001.0.46.4)
90 // Target: x86_64-apple-darwin18.5.0
91 explicit InternalWarmupRequestPlayer(int threadCount);
92 ~InternalWarmupRequestPlayer();
94 // Start running after an optional delay.
95 void runAfterDelay(const std::vector<std::string>& files,
96 unsigned nTimes = 1,
97 unsigned delaySeconds = 0);
100 ///////////////////////////////////////////////////////////////////////////////
103 #endif // incl_HPHP_WARMUP_REQUEST_HANDLER_H_