config.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #ifndef BOOST_REDIS_CONFIG_HPP
  7. #define BOOST_REDIS_CONFIG_HPP
  8. #include <boost/redis/request.hpp>
  9. #include <chrono>
  10. #include <limits>
  11. #include <optional>
  12. #include <string>
  13. namespace boost::redis {
  14. /// Address of a Redis server.
  15. struct address {
  16. /// Redis host.
  17. std::string host = "127.0.0.1";
  18. /// Redis port.
  19. std::string port = "6379";
  20. };
  21. /// Configure parameters used by the connection classes.
  22. struct config {
  23. /// Uses SSL instead of a plain connection.
  24. bool use_ssl = false;
  25. /// For TCP connections, hostname and port of the Redis server.
  26. address addr = address{"127.0.0.1", "6379"};
  27. /**
  28. * @brief The UNIX domain socket path where the server is listening.
  29. *
  30. * If non-empty, communication with the server will happen using
  31. * UNIX domain sockets, and @ref addr will be ignored.
  32. * UNIX domain sockets can't be used with SSL: if `unix_socket` is non-empty,
  33. * @ref use_ssl must be `false`.
  34. */
  35. std::string unix_socket;
  36. /** @brief Username used for authentication during connection establishment.
  37. *
  38. * If @ref use_setup is false (the default), during connection establishment,
  39. * authentication is performed by sending a `HELLO` command.
  40. * This field contains the username to employ.
  41. *
  42. * If the username equals the literal `"default"` (the default)
  43. * and no password is specified, the `HELLO` command is sent
  44. * without authentication parameters.
  45. */
  46. std::string username = "default";
  47. /** @brief Password used for authentication during connection establishment.
  48. *
  49. * If @ref use_setup is false (the default), during connection establishment,
  50. * authentication is performed by sending a `HELLO` command.
  51. * This field contains the password to employ.
  52. *
  53. * If the username equals the literal `"default"` (the default)
  54. * and no password is specified, the `HELLO` command is sent
  55. * without authentication parameters.
  56. */
  57. std::string password;
  58. /** @brief Client name parameter to use during connection establishment.
  59. *
  60. * If @ref use_setup is false (the default), during connection establishment,
  61. * a `HELLO` command is sent. If this field is not empty, the `HELLO` command
  62. * will contain a `SETNAME` subcommand containing this value.
  63. */
  64. std::string clientname = "Boost.Redis";
  65. /** @brief Database index to pass to the `SELECT` command during connection establishment.
  66. *
  67. * If @ref use_setup is false (the default), and this field is set to a
  68. * non-empty optional, and its value is different than zero,
  69. * a `SELECT` command will be issued during connection establishment to set the logical
  70. * database index. By default, no `SELECT` command is sent.
  71. */
  72. std::optional<int> database_index = 0;
  73. /// Message used by `PING` commands sent by the health checker.
  74. std::string health_check_id = "Boost.Redis";
  75. /**
  76. * @brief (Deprecated) Sets the logger prefix, a string printed before log messages.
  77. *
  78. * Setting a prefix in this struct is deprecated. If you need to change how log messages
  79. * look like, please construct a logger object passing a formatting function, and use that
  80. * logger in connection's constructor. This member will be removed in subsequent releases.
  81. */
  82. std::string log_prefix = "(Boost.Redis) ";
  83. /// Time span that the resolve operation is allowed to elapse.
  84. std::chrono::steady_clock::duration resolve_timeout = std::chrono::seconds{10};
  85. /// Time span that the connect operation is allowed to elapse.
  86. std::chrono::steady_clock::duration connect_timeout = std::chrono::seconds{10};
  87. /// Time span that the SSL handshake operation is allowed to elapse.
  88. std::chrono::steady_clock::duration ssl_handshake_timeout = std::chrono::seconds{10};
  89. /** @brief Time span between successive health checks.
  90. * Set to zero to disable health-checks.
  91. *
  92. * When this value is set to a non-zero duration, @ref basic_connection::async_run
  93. * will issue `PING` commands whenever no command is sent to the server for more
  94. * than `health_check_interval`. You can configure the message passed to the `PING`
  95. * command using @ref health_check_id.
  96. *
  97. * Enabling health checks also sets timeouts to individual network
  98. * operations. The connection is considered dead if:
  99. *
  100. * @li No byte can be written to the server after `health_check_interval`.
  101. * @li No byte is read from the server after `2 * health_check_interval`.
  102. *
  103. * If the health checker finds that the connection is unresponsive, it will be closed,
  104. * and a reconnection will be triggered, as if a network error had occurred.
  105. *
  106. * The exact timeout values are *not* part of the interface, and might change
  107. * in future versions.
  108. */
  109. std::chrono::steady_clock::duration health_check_interval = std::chrono::seconds{2};
  110. /** @brief Time span to wait between successive connection retries.
  111. * Set to zero to disable reconnection.
  112. */
  113. std::chrono::steady_clock::duration reconnect_wait_interval = std::chrono::seconds{1};
  114. /** @brief Maximum size of the socket read-buffer in bytes.
  115. *
  116. * Sets a limit on how much data is allowed to be read into the
  117. * read buffer. It can be used to prevent DDOS.
  118. */
  119. std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)();
  120. /** @brief Grow size of the read buffer.
  121. *
  122. * The size by which the read buffer grows when more space is
  123. * needed. This can help avoiding some memory allocations. Once the
  124. * maximum size is reached no more memory allocations are made
  125. * since the buffer is reused.
  126. */
  127. std::size_t read_buffer_append_size = 4096;
  128. /** @brief Enables using a custom requests during connection establishment.
  129. *
  130. * If set to true, the @ref setup member will be sent to the server immediately after
  131. * connection establishment. Every time a reconnection happens, the setup
  132. * request will be executed before any other request.
  133. * It can be used to perform authentication,
  134. * subscribe to channels or select a database index.
  135. *
  136. * When set to true, *the custom setup request replaces the built-in HELLO
  137. * request generated by the library*. The @ref username, @ref password,
  138. * @ref clientname and @ref database_index fields *will be ignored*.
  139. *
  140. * By default, @ref setup contains a `"HELLO 3"` command, which upgrades the
  141. * protocol to RESP3. You might modify this request as you like,
  142. * but you should ensure that the resulting connection uses RESP3.
  143. *
  144. * To prevent sending any setup request at all, set this field to true
  145. * and @ref setup to an empty request. This can be used to interface with
  146. * systems that don't support `HELLO`.
  147. *
  148. * By default, this field is false, and @ref setup will not be used.
  149. */
  150. bool use_setup = false;
  151. /** @brief Request to be executed after connection establishment.
  152. *
  153. * This member is only used if @ref use_setup is `true`. Please consult
  154. * @ref use_setup docs for more info.
  155. *
  156. * By default, `setup` contains a `"HELLO 3"` command.
  157. */
  158. request setup = detail::make_hello_request();
  159. };
  160. } // namespace boost::redis
  161. #endif // BOOST_REDIS_CONFIG_HPP