config.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package sarama
  2. import (
  3. "crypto/tls"
  4. "regexp"
  5. "time"
  6. )
  7. var validID *regexp.Regexp = regexp.MustCompile(`\A[A-Za-z0-9._-]*\z`)
  8. // Config is used to pass multiple configuration options to Sarama's constructors.
  9. type Config struct {
  10. // Net is the namespace for network-level properties used by the Broker, and
  11. // shared by the Client/Producer/Consumer.
  12. Net struct {
  13. // How many outstanding requests a connection is allowed to have before
  14. // sending on it blocks (default 5).
  15. MaxOpenRequests int
  16. // All three of the below configurations are similar to the
  17. // `socket.timeout.ms` setting in JVM kafka. All of them default
  18. // to 30 seconds.
  19. DialTimeout time.Duration // How long to wait for the initial connection.
  20. ReadTimeout time.Duration // How long to wait for a response.
  21. WriteTimeout time.Duration // How long to wait for a transmit.
  22. TLS struct {
  23. // Whether or not to use TLS when connecting to the broker
  24. // (defaults to false).
  25. Enable bool
  26. // The TLS configuration to use for secure connections if
  27. // enabled (defaults to nil).
  28. Config *tls.Config
  29. }
  30. // KeepAlive specifies the keep-alive period for an active network connection.
  31. // If zero, keep-alives are disabled. (default is 0: disabled).
  32. KeepAlive time.Duration
  33. }
  34. // Metadata is the namespace for metadata management properties used by the
  35. // Client, and shared by the Producer/Consumer.
  36. Metadata struct {
  37. Retry struct {
  38. // The total number of times to retry a metadata request when the
  39. // cluster is in the middle of a leader election (default 3).
  40. Max int
  41. // How long to wait for leader election to occur before retrying
  42. // (default 250ms). Similar to the JVM's `retry.backoff.ms`.
  43. Backoff time.Duration
  44. }
  45. // How frequently to refresh the cluster metadata in the background.
  46. // Defaults to 10 minutes. Set to 0 to disable. Similar to
  47. // `topic.metadata.refresh.interval.ms` in the JVM version.
  48. RefreshFrequency time.Duration
  49. }
  50. // Producer is the namespace for configuration related to producing messages,
  51. // used by the Producer.
  52. Producer struct {
  53. // The maximum permitted size of a message (defaults to 1000000). Should be
  54. // set equal to or smaller than the broker's `message.max.bytes`.
  55. MaxMessageBytes int
  56. // The level of acknowledgement reliability needed from the broker (defaults
  57. // to WaitForLocal). Equivalent to the `request.required.acks` setting of the
  58. // JVM producer.
  59. RequiredAcks RequiredAcks
  60. // The maximum duration the broker will wait the receipt of the number of
  61. // RequiredAcks (defaults to 10 seconds). This is only relevant when
  62. // RequiredAcks is set to WaitForAll or a number > 1. Only supports
  63. // millisecond resolution, nanoseconds will be truncated. Equivalent to
  64. // the JVM producer's `request.timeout.ms` setting.
  65. Timeout time.Duration
  66. // The type of compression to use on messages (defaults to no compression).
  67. // Similar to `compression.codec` setting of the JVM producer.
  68. Compression CompressionCodec
  69. // Generates partitioners for choosing the partition to send messages to
  70. // (defaults to hashing the message key). Similar to the `partitioner.class`
  71. // setting for the JVM producer.
  72. Partitioner PartitionerConstructor
  73. // Return specifies what channels will be populated. If they are set to true,
  74. // you must read from the respective channels to prevent deadlock.
  75. Return struct {
  76. // If enabled, successfully delivered messages will be returned on the
  77. // Successes channel (default disabled).
  78. Successes bool
  79. // If enabled, messages that failed to deliver will be returned on the
  80. // Errors channel, including error (default enabled).
  81. Errors bool
  82. }
  83. // The following config options control how often messages are batched up and
  84. // sent to the broker. By default, messages are sent as fast as possible, and
  85. // all messages received while the current batch is in-flight are placed
  86. // into the subsequent batch.
  87. Flush struct {
  88. // The best-effort number of bytes needed to trigger a flush. Use the
  89. // global sarama.MaxRequestSize to set a hard upper limit.
  90. Bytes int
  91. // The best-effort number of messages needed to trigger a flush. Use
  92. // `MaxMessages` to set a hard upper limit.
  93. Messages int
  94. // The best-effort frequency of flushes. Equivalent to
  95. // `queue.buffering.max.ms` setting of JVM producer.
  96. Frequency time.Duration
  97. // The maximum number of messages the producer will send in a single
  98. // broker request. Defaults to 0 for unlimited. Similar to
  99. // `queue.buffering.max.messages` in the JVM producer.
  100. MaxMessages int
  101. }
  102. Retry struct {
  103. // The total number of times to retry sending a message (default 3).
  104. // Similar to the `message.send.max.retries` setting of the JVM producer.
  105. Max int
  106. // How long to wait for the cluster to settle between retries
  107. // (default 100ms). Similar to the `retry.backoff.ms` setting of the
  108. // JVM producer.
  109. Backoff time.Duration
  110. }
  111. }
  112. // Consumer is the namespace for configuration related to consuming messages,
  113. // used by the Consumer.
  114. //
  115. // Note that Sarama's Consumer type does not currently support automatic
  116. // consumer-group rebalancing and offset tracking. For Zookeeper-based
  117. // tracking (Kafka 0.8.2 and earlier), the https://github.com/wvanbergen/kafka
  118. // library builds on Sarama to add this support. For Kafka-based tracking
  119. // (Kafka 0.9 and later), the https://github.com/bsm/sarama-cluster library
  120. // builds on Sarama to add this support.
  121. Consumer struct {
  122. Retry struct {
  123. // How long to wait after a failing to read from a partition before
  124. // trying again (default 2s).
  125. Backoff time.Duration
  126. }
  127. // Fetch is the namespace for controlling how many bytes are retrieved by any
  128. // given request.
  129. Fetch struct {
  130. // The minimum number of message bytes to fetch in a request - the broker
  131. // will wait until at least this many are available. The default is 1,
  132. // as 0 causes the consumer to spin when no messages are available.
  133. // Equivalent to the JVM's `fetch.min.bytes`.
  134. Min int32
  135. // The default number of message bytes to fetch from the broker in each
  136. // request (default 32768). This should be larger than the majority of
  137. // your messages, or else the consumer will spend a lot of time
  138. // negotiating sizes and not actually consuming. Similar to the JVM's
  139. // `fetch.message.max.bytes`.
  140. Default int32
  141. // The maximum number of message bytes to fetch from the broker in a
  142. // single request. Messages larger than this will return
  143. // ErrMessageTooLarge and will not be consumable, so you must be sure
  144. // this is at least as large as your largest message. Defaults to 0
  145. // (no limit). Similar to the JVM's `fetch.message.max.bytes`. The
  146. // global `sarama.MaxResponseSize` still applies.
  147. Max int32
  148. }
  149. // The maximum amount of time the broker will wait for Consumer.Fetch.Min
  150. // bytes to become available before it returns fewer than that anyways. The
  151. // default is 250ms, since 0 causes the consumer to spin when no events are
  152. // available. 100-500ms is a reasonable range for most cases. Kafka only
  153. // supports precision up to milliseconds; nanoseconds will be truncated.
  154. // Equivalent to the JVM's `fetch.wait.max.ms`.
  155. MaxWaitTime time.Duration
  156. // The maximum amount of time the consumer expects a message takes to process
  157. // for the user. If writing to the Messages channel takes longer than this,
  158. // that partition will stop fetching more messages until it can proceed again.
  159. // Note that, since the Messages channel is buffered, the actual grace time is
  160. // (MaxProcessingTime * ChanneBufferSize). Defaults to 100ms.
  161. MaxProcessingTime time.Duration
  162. // Return specifies what channels will be populated. If they are set to true,
  163. // you must read from them to prevent deadlock.
  164. Return struct {
  165. // If enabled, any errors that occured while consuming are returned on
  166. // the Errors channel (default disabled).
  167. Errors bool
  168. }
  169. // Offsets specifies configuration for how and when to commit consumed
  170. // offsets. This currently requires the manual use of an OffsetManager
  171. // but will eventually be automated.
  172. Offsets struct {
  173. // How frequently to commit updated offsets. Defaults to 1s.
  174. CommitInterval time.Duration
  175. // The initial offset to use if no offset was previously committed.
  176. // Should be OffsetNewest or OffsetOldest. Defaults to OffsetNewest.
  177. Initial int64
  178. // The retention duration for committed offsets. If zero, disabled
  179. // (in which case the `offsets.retention.minutes` option on the
  180. // broker will be used). Kafka only supports precision up to
  181. // milliseconds; nanoseconds will be truncated.
  182. // (default is 0: disabled).
  183. Retention time.Duration
  184. }
  185. }
  186. // A user-provided string sent with every request to the brokers for logging,
  187. // debugging, and auditing purposes. Defaults to "sarama", but you should
  188. // probably set it to something specific to your application.
  189. ClientID string
  190. // The number of events to buffer in internal and external channels. This
  191. // permits the producer and consumer to continue processing some messages
  192. // in the background while user code is working, greatly improving throughput.
  193. // Defaults to 256.
  194. ChannelBufferSize int
  195. }
  196. // NewConfig returns a new configuration instance with sane defaults.
  197. func NewConfig() *Config {
  198. c := &Config{}
  199. c.Net.MaxOpenRequests = 5
  200. c.Net.DialTimeout = 30 * time.Second
  201. c.Net.ReadTimeout = 30 * time.Second
  202. c.Net.WriteTimeout = 30 * time.Second
  203. c.Metadata.Retry.Max = 3
  204. c.Metadata.Retry.Backoff = 250 * time.Millisecond
  205. c.Metadata.RefreshFrequency = 10 * time.Minute
  206. c.Producer.MaxMessageBytes = 1000000
  207. c.Producer.RequiredAcks = WaitForLocal
  208. c.Producer.Timeout = 10 * time.Second
  209. c.Producer.Partitioner = NewHashPartitioner
  210. c.Producer.Retry.Max = 3
  211. c.Producer.Retry.Backoff = 100 * time.Millisecond
  212. c.Producer.Return.Errors = true
  213. c.Consumer.Fetch.Min = 1
  214. c.Consumer.Fetch.Default = 32768
  215. c.Consumer.Retry.Backoff = 2 * time.Second
  216. c.Consumer.MaxWaitTime = 250 * time.Millisecond
  217. c.Consumer.MaxProcessingTime = 100 * time.Millisecond
  218. c.Consumer.Return.Errors = false
  219. c.Consumer.Offsets.CommitInterval = 1 * time.Second
  220. c.Consumer.Offsets.Initial = OffsetNewest
  221. c.ChannelBufferSize = 256
  222. return c
  223. }
  224. // Validate checks a Config instance. It will return a
  225. // ConfigurationError if the specified values don't make sense.
  226. func (c *Config) Validate() error {
  227. // some configuration values should be warned on but not fail completely, do those first
  228. if c.Net.TLS.Enable == false && c.Net.TLS.Config != nil {
  229. Logger.Println("Net.TLS is disabled but a non-nil configuration was provided.")
  230. }
  231. if c.Producer.RequiredAcks > 1 {
  232. Logger.Println("Producer.RequiredAcks > 1 is deprecated and will raise an exception with kafka >= 0.8.2.0.")
  233. }
  234. if c.Producer.MaxMessageBytes >= int(MaxRequestSize) {
  235. Logger.Println("Producer.MaxMessageBytes is larger than MaxRequestSize; it will be ignored.")
  236. }
  237. if c.Producer.Flush.Bytes >= int(MaxRequestSize) {
  238. Logger.Println("Producer.Flush.Bytes is larger than MaxRequestSize; it will be ignored.")
  239. }
  240. if c.Producer.Timeout%time.Millisecond != 0 {
  241. Logger.Println("Producer.Timeout only supports millisecond resolution; nanoseconds will be truncated.")
  242. }
  243. if c.Consumer.MaxWaitTime < 100*time.Millisecond {
  244. Logger.Println("Consumer.MaxWaitTime is very low, which can cause high CPU and network usage. See documentation for details.")
  245. }
  246. if c.Consumer.MaxWaitTime%time.Millisecond != 0 {
  247. Logger.Println("Consumer.MaxWaitTime only supports millisecond precision; nanoseconds will be truncated.")
  248. }
  249. if c.Consumer.Offsets.Retention%time.Millisecond != 0 {
  250. Logger.Println("Consumer.Offsets.Retention only supports millisecond precision; nanoseconds will be truncated.")
  251. }
  252. if c.ClientID == "sarama" {
  253. Logger.Println("ClientID is the default of 'sarama', you should consider setting it to something application-specific.")
  254. }
  255. // validate Net values
  256. switch {
  257. case c.Net.MaxOpenRequests <= 0:
  258. return ConfigurationError("Net.MaxOpenRequests must be > 0")
  259. case c.Net.DialTimeout <= 0:
  260. return ConfigurationError("Net.DialTimeout must be > 0")
  261. case c.Net.ReadTimeout <= 0:
  262. return ConfigurationError("Net.ReadTimeout must be > 0")
  263. case c.Net.WriteTimeout <= 0:
  264. return ConfigurationError("Net.WriteTimeout must be > 0")
  265. case c.Net.KeepAlive < 0:
  266. return ConfigurationError("Net.KeepAlive must be >= 0")
  267. }
  268. // validate the Metadata values
  269. switch {
  270. case c.Metadata.Retry.Max < 0:
  271. return ConfigurationError("Metadata.Retry.Max must be >= 0")
  272. case c.Metadata.Retry.Backoff < 0:
  273. return ConfigurationError("Metadata.Retry.Backoff must be >= 0")
  274. case c.Metadata.RefreshFrequency < 0:
  275. return ConfigurationError("Metadata.RefreshFrequency must be >= 0")
  276. }
  277. // validate the Producer values
  278. switch {
  279. case c.Producer.MaxMessageBytes <= 0:
  280. return ConfigurationError("Producer.MaxMessageBytes must be > 0")
  281. case c.Producer.RequiredAcks < -1:
  282. return ConfigurationError("Producer.RequiredAcks must be >= -1")
  283. case c.Producer.Timeout <= 0:
  284. return ConfigurationError("Producer.Timeout must be > 0")
  285. case c.Producer.Partitioner == nil:
  286. return ConfigurationError("Producer.Partitioner must not be nil")
  287. case c.Producer.Flush.Bytes < 0:
  288. return ConfigurationError("Producer.Flush.Bytes must be >= 0")
  289. case c.Producer.Flush.Messages < 0:
  290. return ConfigurationError("Producer.Flush.Messages must be >= 0")
  291. case c.Producer.Flush.Frequency < 0:
  292. return ConfigurationError("Producer.Flush.Frequency must be >= 0")
  293. case c.Producer.Flush.MaxMessages < 0:
  294. return ConfigurationError("Producer.Flush.MaxMessages must be >= 0")
  295. case c.Producer.Flush.MaxMessages > 0 && c.Producer.Flush.MaxMessages < c.Producer.Flush.Messages:
  296. return ConfigurationError("Producer.Flush.MaxMessages must be >= Producer.Flush.Messages when set")
  297. case c.Producer.Retry.Max < 0:
  298. return ConfigurationError("Producer.Retry.Max must be >= 0")
  299. case c.Producer.Retry.Backoff < 0:
  300. return ConfigurationError("Producer.Retry.Backoff must be >= 0")
  301. }
  302. // validate the Consumer values
  303. switch {
  304. case c.Consumer.Fetch.Min <= 0:
  305. return ConfigurationError("Consumer.Fetch.Min must be > 0")
  306. case c.Consumer.Fetch.Default <= 0:
  307. return ConfigurationError("Consumer.Fetch.Default must be > 0")
  308. case c.Consumer.Fetch.Max < 0:
  309. return ConfigurationError("Consumer.Fetch.Max must be >= 0")
  310. case c.Consumer.MaxWaitTime < 1*time.Millisecond:
  311. return ConfigurationError("Consumer.MaxWaitTime must be >= 1ms")
  312. case c.Consumer.MaxProcessingTime <= 0:
  313. return ConfigurationError("Consumer.MaxProcessingTime must be > 0")
  314. case c.Consumer.Retry.Backoff < 0:
  315. return ConfigurationError("Consumer.Retry.Backoff must be >= 0")
  316. case c.Consumer.Offsets.CommitInterval <= 0:
  317. return ConfigurationError("Consumer.Offsets.CommitInterval must be > 0")
  318. case c.Consumer.Offsets.Initial != OffsetOldest && c.Consumer.Offsets.Initial != OffsetNewest:
  319. return ConfigurationError("Consumer.Offsets.Initial must be OffsetOldest or OffsetNewest")
  320. }
  321. // validate misc shared values
  322. switch {
  323. case c.ChannelBufferSize < 0:
  324. return ConfigurationError("ChannelBufferSize must be >= 0")
  325. case !validID.MatchString(c.ClientID):
  326. return ConfigurationError("ClientID is invalid")
  327. }
  328. return nil
  329. }