options.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #pragma once
  2. #include <util/network/ip.h>
  3. #include <util/network/init.h>
  4. #include <util/network/address.h>
  5. #include <util/generic/size_literals.h>
  6. #include <util/generic/string.h>
  7. #include <util/generic/vector.h>
  8. #include <util/datetime/base.h>
  9. class IOutputStream;
  10. class THttpServerOptions {
  11. public:
  12. inline THttpServerOptions(ui16 port = 17000) noexcept
  13. : Port(port)
  14. {
  15. }
  16. using TBindAddresses = TVector<TNetworkAddress>;
  17. void BindAddresses(TBindAddresses& ret) const;
  18. inline THttpServerOptions& AddBindAddress(const TString& address, ui16 port) {
  19. const TAddr addr = {
  20. address,
  21. port,
  22. };
  23. BindSockaddr.push_back(addr);
  24. return *this;
  25. }
  26. inline THttpServerOptions& AddBindAddress(const TString& address) {
  27. return AddBindAddress(address, 0);
  28. }
  29. inline THttpServerOptions& EnableKeepAlive(bool enable) noexcept {
  30. KeepAliveEnabled = enable;
  31. return *this;
  32. }
  33. inline THttpServerOptions& EnableCompression(bool enable) noexcept {
  34. CompressionEnabled = enable;
  35. return *this;
  36. }
  37. inline THttpServerOptions& EnableRejectExcessConnections(bool enable) noexcept {
  38. RejectExcessConnections = enable;
  39. return *this;
  40. }
  41. inline THttpServerOptions& EnableReusePort(bool enable) noexcept {
  42. ReusePort = enable;
  43. return *this;
  44. }
  45. inline THttpServerOptions& EnableReuseAddress(bool enable) noexcept {
  46. ReuseAddress = enable;
  47. return *this;
  48. }
  49. inline THttpServerOptions& SetThreads(ui32 threads) noexcept {
  50. nThreads = threads;
  51. return *this;
  52. }
  53. /// Default interface name to bind the server. Used when none of BindAddress are provided.
  54. inline THttpServerOptions& SetHost(const TString& host) noexcept {
  55. Host = host;
  56. return *this;
  57. }
  58. /// Default port to bind the server. Used when none of BindAddress are provided.
  59. inline THttpServerOptions& SetPort(ui16 port) noexcept {
  60. Port = port;
  61. return *this;
  62. }
  63. inline THttpServerOptions& SetMaxConnections(ui32 mc = 0) noexcept {
  64. MaxConnections = mc;
  65. return *this;
  66. }
  67. inline THttpServerOptions& SetMaxQueueSize(ui32 mqs = 0) noexcept {
  68. MaxQueueSize = mqs;
  69. return *this;
  70. }
  71. inline THttpServerOptions& SetClientTimeout(const TDuration& timeout) noexcept {
  72. ClientTimeout = timeout;
  73. return *this;
  74. }
  75. inline THttpServerOptions& SetListenBacklog(int val) noexcept {
  76. ListenBacklog = val;
  77. return *this;
  78. }
  79. inline THttpServerOptions& SetOutputBufferSize(size_t val) noexcept {
  80. OutputBufferSize = val;
  81. return *this;
  82. }
  83. inline THttpServerOptions& SetMaxInputContentLength(ui64 val) noexcept {
  84. MaxInputContentLength = val;
  85. return *this;
  86. }
  87. inline THttpServerOptions& SetMaxRequestsPerConnection(size_t val) noexcept {
  88. MaxRequestsPerConnection = val;
  89. return *this;
  90. }
  91. /// Use TElasticQueue instead of TThreadPool for request queues
  92. inline THttpServerOptions& EnableElasticQueues(bool enable) noexcept {
  93. UseElasticQueues = enable;
  94. return *this;
  95. }
  96. inline THttpServerOptions& SetThreadsName(const TString& listenThreadName, const TString& requestsThreadName, const TString& failRequestsThreadName) noexcept {
  97. ListenThreadName = listenThreadName;
  98. RequestsThreadName = requestsThreadName;
  99. FailRequestsThreadName = failRequestsThreadName;
  100. return *this;
  101. }
  102. inline THttpServerOptions& SetOneShotPoll(bool v) {
  103. OneShotPoll = v;
  104. return *this;
  105. }
  106. inline THttpServerOptions& SetListenerThreads(ui32 val) {
  107. nListenerThreads = val;
  108. return *this;
  109. }
  110. void DebugPrint(IOutputStream& stream) const noexcept;
  111. struct TAddr {
  112. TString Addr;
  113. ui16 Port;
  114. };
  115. typedef TVector<TAddr> TAddrs;
  116. bool KeepAliveEnabled = true;
  117. bool CompressionEnabled = false;
  118. bool RejectExcessConnections = false;
  119. bool ReusePort = false; // set SO_REUSEPORT socket option
  120. bool ReuseAddress = true; // set SO_REUSEADDR socket option
  121. TAddrs BindSockaddr;
  122. ui16 Port = 17000; // The port on which to run the web server
  123. TString Host; // DNS entry
  124. const char* ServerName = "YWS/1.0"; // The Web server name to return in HTTP headers
  125. ui32 nThreads = 0; // Thread count for requests processing
  126. ui32 MaxQueueSize = 0; // Max allowed request count in queue
  127. ui32 nFThreads = 1;
  128. ui32 MaxFQueueSize = 0;
  129. ui32 MaxConnections = 100;
  130. int ListenBacklog = SOMAXCONN;
  131. ui32 EpollMaxEvents = 1;
  132. TDuration ClientTimeout = TDuration::Minutes(1);
  133. size_t OutputBufferSize = 0;
  134. ui64 MaxInputContentLength = sizeof(size_t) <= 4 ? 2_GB : 64_GB;
  135. size_t MaxRequestsPerConnection = 0; // If keep-alive is enabled, request limit before connection is closed
  136. bool UseElasticQueues = false;
  137. TDuration PollTimeout; // timeout of TSocketPoller::WaitT call
  138. TDuration ExpirationTimeout; // drop inactive connections after ExpirationTimeout (should be > 0)
  139. TString ListenThreadName = "HttpListen";
  140. TString RequestsThreadName = "HttpServer";
  141. TString FailRequestsThreadName = "HttpServer";
  142. bool OneShotPoll = false;
  143. ui32 nListenerThreads = 1;
  144. };