aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/config/session_config.cpp
blob: 9b430033f3da48b829b57ee45f89b97a99487ba1 (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
#include "session_config.h"

#include <util/generic/strbuf.h>
#include <util/string/hex.h>

using namespace NBus;

TBusSessionConfig::TSecret::TSecret()
    : TimeoutPeriod(TDuration::Seconds(1))
    , StatusFlushPeriod(TDuration::MilliSeconds(400))
{
}

TBusSessionConfig::TBusSessionConfig()
    : BUS_SESSION_CONFIG_MAP(STRUCT_FIELD_INIT, COMMA)
{
}

TString TBusSessionConfig::PrintToString() const { 
    TStringStream ss;
    BUS_SESSION_CONFIG_MAP(STRUCT_FIELD_PRINT, )
    return ss.Str();
}

static int ParseDurationForMessageBus(const char* option) {
    return TDuration::Parse(option).MilliSeconds();
}

static int ParseToSForMessageBus(const char* option) {
    int tos;
    TStringBuf str(option);
    if (str.StartsWith("0x")) {
        str = str.Tail(2);
        Y_VERIFY(str.length() == 2, "ToS must be a number between 0x00 and 0xFF");
        tos = String2Byte(str.data());
    } else {
        tos = FromString<int>(option);
    }
    Y_VERIFY(tos >= 0 && tos <= 255, "ToS must be between 0x00 and 0xFF");
    return tos;
}

template <class T>
static T ParseWithKmgSuffixT(const char* option) {
    TStringBuf str(option);
    T multiplier = 1;
    if (str.EndsWith('k')) {
        multiplier = 1024;
        str = str.Head(str.size() - 1);
    } else if (str.EndsWith('m')) {
        multiplier = 1024 * 1024;
        str = str.Head(str.size() - 1);
    } else if (str.EndsWith('g')) {
        multiplier = 1024 * 1024 * 1024;
        str = str.Head(str.size() - 1);
    }
    return FromString<T>(str) * multiplier;
}

static ui64 ParseWithKmgSuffix(const char* option) {
    return ParseWithKmgSuffixT<ui64>(option);
}

static i64 ParseWithKmgSuffixS(const char* option) {
    return ParseWithKmgSuffixT<i64>(option);
}

void TBusSessionConfig::ConfigureLastGetopt(NLastGetopt::TOpts& opts,
                                            const TString& prefix) { 
    opts.AddLongOption(prefix + "total-timeout")
        .RequiredArgument("MILLISECONDS")
        .DefaultValue(ToString(TotalTimeout))
        .StoreMappedResultT<const char*>(&TotalTimeout,
                                         &ParseDurationForMessageBus);
    opts.AddLongOption(prefix + "connect-timeout")
        .RequiredArgument("MILLISECONDS")
        .DefaultValue(ToString(ConnectTimeout))
        .StoreMappedResultT<const char*>(&ConnectTimeout,
                                         &ParseDurationForMessageBus);
    opts.AddLongOption(prefix + "send-timeout")
        .RequiredArgument("MILLISECONDS")
        .DefaultValue(ToString(SendTimeout))
        .StoreMappedResultT<const char*>(&SendTimeout,
                                         &ParseDurationForMessageBus);
    opts.AddLongOption(prefix + "send-threshold")
        .RequiredArgument("BYTES")
        .DefaultValue(ToString(SendThreshold))
        .StoreMappedResultT<const char*>(&SendThreshold, &ParseWithKmgSuffix);

    opts.AddLongOption(prefix + "max-in-flight")
        .RequiredArgument("COUNT")
        .DefaultValue(ToString(MaxInFlight))
        .StoreMappedResultT<const char*>(&MaxInFlight, &ParseWithKmgSuffix);
    opts.AddLongOption(prefix + "max-in-flight-by-size")
        .RequiredArgument("BYTES")
        .DefaultValue(
            ToString(MaxInFlightBySize))
        .StoreMappedResultT<const char*>(&MaxInFlightBySize, &ParseWithKmgSuffixS);
    opts.AddLongOption(prefix + "per-con-max-in-flight")
        .RequiredArgument("COUNT")
        .DefaultValue(ToString(PerConnectionMaxInFlight))
        .StoreMappedResultT<const char*>(&PerConnectionMaxInFlight,
                                         &ParseWithKmgSuffix);
    opts.AddLongOption(prefix + "per-con-max-in-flight-by-size")
        .RequiredArgument("BYTES")
        .DefaultValue(
            ToString(PerConnectionMaxInFlightBySize))
        .StoreMappedResultT<const char*>(&PerConnectionMaxInFlightBySize,
                                         &ParseWithKmgSuffix);

    opts.AddLongOption(prefix + "default-buffer-size")
        .RequiredArgument("BYTES")
        .DefaultValue(ToString(DefaultBufferSize))
        .StoreMappedResultT<const char*>(&DefaultBufferSize,
                                         &ParseWithKmgSuffix);
    opts.AddLongOption(prefix + "max-buffer-size")
        .RequiredArgument("BYTES")
        .DefaultValue(ToString(MaxBufferSize))
        .StoreMappedResultT<const char*>(&MaxBufferSize, &ParseWithKmgSuffix);
    opts.AddLongOption(prefix + "max-message-size")
        .RequiredArgument("BYTES")
        .DefaultValue(ToString(MaxMessageSize))
        .StoreMappedResultT<const char*>(&MaxMessageSize, &ParseWithKmgSuffix);
    opts.AddLongOption(prefix + "socket-recv-buffer-size")
        .RequiredArgument("BYTES")
        .DefaultValue(ToString(SocketRecvBufferSize))
        .StoreMappedResultT<const char*>(&SocketRecvBufferSize,
                                         &ParseWithKmgSuffix);
    opts.AddLongOption(prefix + "socket-send-buffer-size")
        .RequiredArgument("BYTES")
        .DefaultValue(ToString(SocketSendBufferSize))
        .StoreMappedResultT<const char*>(&SocketSendBufferSize,
                                         &ParseWithKmgSuffix);

    opts.AddLongOption(prefix + "socket-tos")
        .RequiredArgument("[0x00, 0xFF]")
        .StoreMappedResultT<const char*>(&SocketToS, &ParseToSForMessageBus);
    ;
    opts.AddLongOption(prefix + "tcp-cork")
        .RequiredArgument("BOOL")
        .DefaultValue(ToString(TcpCork))
        .StoreResult(&TcpCork);
    opts.AddLongOption(prefix + "cork")
        .RequiredArgument("SECONDS")
        .DefaultValue(
            ToString(Cork.Seconds()))
        .StoreMappedResultT<const char*>(&Cork, &TDuration::Parse);

    opts.AddLongOption(prefix + "on-message-in-pool")
        .RequiredArgument("BOOL")
        .DefaultValue(ToString(ExecuteOnMessageInWorkerPool))
        .StoreResult(&ExecuteOnMessageInWorkerPool);
    opts.AddLongOption(prefix + "on-reply-in-pool")
        .RequiredArgument("BOOL")
        .DefaultValue(ToString(ExecuteOnReplyInWorkerPool))
        .StoreResult(&ExecuteOnReplyInWorkerPool);
}