aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/server/options.h
blob: f837ef0b0873d7be854e118983ab7e242570fca8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once

#include <util/network/ip.h>
#include <util/network/init.h>
#include <util/network/address.h>
#include <util/generic/size_literals.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/datetime/base.h>

class THttpServerOptions {
public:
    inline THttpServerOptions(ui16 port = 17000) noexcept
        : Port(port)
    {
    }

    using TBindAddresses = TVector<TNetworkAddress>;
    void BindAddresses(TBindAddresses& ret) const;

    inline THttpServerOptions& AddBindAddress(const TString& address, ui16 port) { 
        const TAddr addr = {
            address,
            port,
        };

        BindSockaddr.push_back(addr);
        return *this; 
    }

    inline THttpServerOptions& AddBindAddress(const TString& address) { 
        return AddBindAddress(address, 0); 
    }

    inline THttpServerOptions& EnableKeepAlive(bool enable) noexcept {
        KeepAliveEnabled = enable;

        return *this;
    }

    inline THttpServerOptions& EnableCompression(bool enable) noexcept {
        CompressionEnabled = enable;

        return *this;
    }

    inline THttpServerOptions& EnableRejectExcessConnections(bool enable) noexcept {
        RejectExcessConnections = enable;

        return *this;
    }

    inline THttpServerOptions& EnableReusePort(bool enable) noexcept {
        ReusePort = enable;

        return *this;
    }

    inline THttpServerOptions& EnableReuseAddress(bool enable) noexcept {
        ReuseAddress = enable;

        return *this;
    }

    inline THttpServerOptions& SetThreads(ui32 threads) noexcept {
        nThreads = threads;

        return *this;
    }

    /// Default interface name to bind the server. Used when none of BindAddress are provided.
    inline THttpServerOptions& SetHost(const TString& host) noexcept {
        Host = host;

        return *this;
    }

    /// Default port to bind the server. Used when none of BindAddress are provided.
    inline THttpServerOptions& SetPort(ui16 port) noexcept {
        Port = port;

        return *this;
    }

    inline THttpServerOptions& SetMaxConnections(ui32 mc = 0) noexcept {
        MaxConnections = mc;

        return *this;
    }

    inline THttpServerOptions& SetMaxQueueSize(ui32 mqs = 0) noexcept {
        MaxQueueSize = mqs;

        return *this;
    }

    inline THttpServerOptions& SetClientTimeout(const TDuration& timeout) noexcept {
        ClientTimeout = timeout;

        return *this;
    }

    inline THttpServerOptions& SetListenBacklog(int val) noexcept {
        ListenBacklog = val;

        return *this;
    }

    inline THttpServerOptions& SetOutputBufferSize(size_t val) noexcept {
        OutputBufferSize = val;

        return *this;
    }

    inline THttpServerOptions& SetMaxInputContentLength(ui64 val) noexcept {
        MaxInputContentLength = val;

        return *this;
    }

    inline THttpServerOptions& SetMaxRequestsPerConnection(size_t val) noexcept {
        MaxRequestsPerConnection = val;

        return *this;
    }

    /// Use TElasticQueue instead of TThreadPool for request queues
    inline THttpServerOptions& EnableElasticQueues(bool enable) noexcept {
        UseElasticQueues = enable;

        return *this;
    }

    inline THttpServerOptions& SetThreadsName(const TString& listenThreadName, const TString& requestsThreadName, const TString& failRequestsThreadName) noexcept {
        ListenThreadName = listenThreadName;
        RequestsThreadName = requestsThreadName;
        FailRequestsThreadName = failRequestsThreadName;

        return *this;
    }

    struct TAddr {
        TString Addr;
        ui16 Port;
    };

    typedef TVector<TAddr> TAddrs;

    bool KeepAliveEnabled = true;
    bool CompressionEnabled = false;
    bool RejectExcessConnections = false;
    bool ReusePort = false; // set SO_REUSEPORT socket option
    bool ReuseAddress = true; // set SO_REUSEADDR socket option
    TAddrs BindSockaddr;
    ui16 Port = 17000;                  // The port on which to run the web server
    TString Host;                       // DNS entry
    const char* ServerName = "YWS/1.0"; // The Web server name to return in HTTP headers
    ui32 nThreads = 0;                  // Thread count for requests processing
    ui32 MaxQueueSize = 0;              // Max allowed request count in queue
    ui32 nFThreads = 1;
    ui32 MaxFQueueSize = 0;
    ui32 MaxConnections = 100;
    int ListenBacklog = SOMAXCONN;
    TDuration ClientTimeout;
    size_t OutputBufferSize = 0;
    ui64 MaxInputContentLength = sizeof(size_t) <= 4 ? 2_GB : 64_GB;
    size_t MaxRequestsPerConnection = 0;  // If keep-alive is enabled, request limit before connection is closed
    bool UseElasticQueues = false;

    TDuration PollTimeout; // timeout of TSocketPoller::WaitT call
    TDuration ExpirationTimeout; // drop inactive connections after ExpirationTimeout (should be > 0)

    TString ListenThreadName = "HttpListen";
    TString RequestsThreadName = "HttpServer";
    TString FailRequestsThreadName = "HttpServer";
};