options.h 5.3 KB

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