session_config.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "session_config.h"
  2. #include <util/generic/strbuf.h>
  3. #include <util/string/hex.h>
  4. using namespace NBus;
  5. TBusSessionConfig::TSecret::TSecret()
  6. : TimeoutPeriod(TDuration::Seconds(1))
  7. , StatusFlushPeriod(TDuration::MilliSeconds(400))
  8. {
  9. }
  10. TBusSessionConfig::TBusSessionConfig()
  11. : BUS_SESSION_CONFIG_MAP(STRUCT_FIELD_INIT, COMMA)
  12. {
  13. }
  14. TString TBusSessionConfig::PrintToString() const {
  15. TStringStream ss;
  16. BUS_SESSION_CONFIG_MAP(STRUCT_FIELD_PRINT, )
  17. return ss.Str();
  18. }
  19. static int ParseDurationForMessageBus(const char* option) {
  20. return TDuration::Parse(option).MilliSeconds();
  21. }
  22. static int ParseToSForMessageBus(const char* option) {
  23. int tos;
  24. TStringBuf str(option);
  25. if (str.StartsWith("0x")) {
  26. str = str.Tail(2);
  27. Y_ABORT_UNLESS(str.length() == 2, "ToS must be a number between 0x00 and 0xFF");
  28. tos = String2Byte(str.data());
  29. } else {
  30. tos = FromString<int>(option);
  31. }
  32. Y_ABORT_UNLESS(tos >= 0 && tos <= 255, "ToS must be between 0x00 and 0xFF");
  33. return tos;
  34. }
  35. template <class T>
  36. static T ParseWithKmgSuffixT(const char* option) {
  37. TStringBuf str(option);
  38. T multiplier = 1;
  39. if (str.EndsWith('k')) {
  40. multiplier = 1024;
  41. str = str.Head(str.size() - 1);
  42. } else if (str.EndsWith('m')) {
  43. multiplier = 1024 * 1024;
  44. str = str.Head(str.size() - 1);
  45. } else if (str.EndsWith('g')) {
  46. multiplier = 1024 * 1024 * 1024;
  47. str = str.Head(str.size() - 1);
  48. }
  49. return FromString<T>(str) * multiplier;
  50. }
  51. static ui64 ParseWithKmgSuffix(const char* option) {
  52. return ParseWithKmgSuffixT<ui64>(option);
  53. }
  54. static i64 ParseWithKmgSuffixS(const char* option) {
  55. return ParseWithKmgSuffixT<i64>(option);
  56. }
  57. void TBusSessionConfig::ConfigureLastGetopt(NLastGetopt::TOpts& opts,
  58. const TString& prefix) {
  59. opts.AddLongOption(prefix + "total-timeout")
  60. .RequiredArgument("MILLISECONDS")
  61. .DefaultValue(ToString(TotalTimeout))
  62. .StoreMappedResultT<const char*>(&TotalTimeout,
  63. &ParseDurationForMessageBus);
  64. opts.AddLongOption(prefix + "connect-timeout")
  65. .RequiredArgument("MILLISECONDS")
  66. .DefaultValue(ToString(ConnectTimeout))
  67. .StoreMappedResultT<const char*>(&ConnectTimeout,
  68. &ParseDurationForMessageBus);
  69. opts.AddLongOption(prefix + "send-timeout")
  70. .RequiredArgument("MILLISECONDS")
  71. .DefaultValue(ToString(SendTimeout))
  72. .StoreMappedResultT<const char*>(&SendTimeout,
  73. &ParseDurationForMessageBus);
  74. opts.AddLongOption(prefix + "send-threshold")
  75. .RequiredArgument("BYTES")
  76. .DefaultValue(ToString(SendThreshold))
  77. .StoreMappedResultT<const char*>(&SendThreshold, &ParseWithKmgSuffix);
  78. opts.AddLongOption(prefix + "max-in-flight")
  79. .RequiredArgument("COUNT")
  80. .DefaultValue(ToString(MaxInFlight))
  81. .StoreMappedResultT<const char*>(&MaxInFlight, &ParseWithKmgSuffix);
  82. opts.AddLongOption(prefix + "max-in-flight-by-size")
  83. .RequiredArgument("BYTES")
  84. .DefaultValue(
  85. ToString(MaxInFlightBySize))
  86. .StoreMappedResultT<const char*>(&MaxInFlightBySize, &ParseWithKmgSuffixS);
  87. opts.AddLongOption(prefix + "per-con-max-in-flight")
  88. .RequiredArgument("COUNT")
  89. .DefaultValue(ToString(PerConnectionMaxInFlight))
  90. .StoreMappedResultT<const char*>(&PerConnectionMaxInFlight,
  91. &ParseWithKmgSuffix);
  92. opts.AddLongOption(prefix + "per-con-max-in-flight-by-size")
  93. .RequiredArgument("BYTES")
  94. .DefaultValue(
  95. ToString(PerConnectionMaxInFlightBySize))
  96. .StoreMappedResultT<const char*>(&PerConnectionMaxInFlightBySize,
  97. &ParseWithKmgSuffix);
  98. opts.AddLongOption(prefix + "default-buffer-size")
  99. .RequiredArgument("BYTES")
  100. .DefaultValue(ToString(DefaultBufferSize))
  101. .StoreMappedResultT<const char*>(&DefaultBufferSize,
  102. &ParseWithKmgSuffix);
  103. opts.AddLongOption(prefix + "max-buffer-size")
  104. .RequiredArgument("BYTES")
  105. .DefaultValue(ToString(MaxBufferSize))
  106. .StoreMappedResultT<const char*>(&MaxBufferSize, &ParseWithKmgSuffix);
  107. opts.AddLongOption(prefix + "max-message-size")
  108. .RequiredArgument("BYTES")
  109. .DefaultValue(ToString(MaxMessageSize))
  110. .StoreMappedResultT<const char*>(&MaxMessageSize, &ParseWithKmgSuffix);
  111. opts.AddLongOption(prefix + "socket-recv-buffer-size")
  112. .RequiredArgument("BYTES")
  113. .DefaultValue(ToString(SocketRecvBufferSize))
  114. .StoreMappedResultT<const char*>(&SocketRecvBufferSize,
  115. &ParseWithKmgSuffix);
  116. opts.AddLongOption(prefix + "socket-send-buffer-size")
  117. .RequiredArgument("BYTES")
  118. .DefaultValue(ToString(SocketSendBufferSize))
  119. .StoreMappedResultT<const char*>(&SocketSendBufferSize,
  120. &ParseWithKmgSuffix);
  121. opts.AddLongOption(prefix + "socket-tos")
  122. .RequiredArgument("[0x00, 0xFF]")
  123. .StoreMappedResultT<const char*>(&SocketToS, &ParseToSForMessageBus);
  124. ;
  125. opts.AddLongOption(prefix + "tcp-cork")
  126. .RequiredArgument("BOOL")
  127. .DefaultValue(ToString(TcpCork))
  128. .StoreResult(&TcpCork);
  129. opts.AddLongOption(prefix + "cork")
  130. .RequiredArgument("SECONDS")
  131. .DefaultValue(
  132. ToString(Cork.Seconds()))
  133. .StoreMappedResultT<const char*>(&Cork, &TDuration::Parse);
  134. opts.AddLongOption(prefix + "on-message-in-pool")
  135. .RequiredArgument("BOOL")
  136. .DefaultValue(ToString(ExecuteOnMessageInWorkerPool))
  137. .StoreResult(&ExecuteOnMessageInWorkerPool);
  138. opts.AddLongOption(prefix + "on-reply-in-pool")
  139. .RequiredArgument("BOOL")
  140. .DefaultValue(ToString(ExecuteOnReplyInWorkerPool))
  141. .StoreResult(&ExecuteOnReplyInWorkerPool);
  142. }