waterdrop 1.4.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.diffend.yml +3 -0
  5. data/.github/workflows/ci.yml +75 -0
  6. data/.gitignore +2 -0
  7. data/.ruby-version +1 -1
  8. data/CHANGELOG.md +13 -0
  9. data/Gemfile +9 -0
  10. data/Gemfile.lock +67 -54
  11. data/LICENSE +165 -0
  12. data/README.md +194 -56
  13. data/config/errors.yml +3 -16
  14. data/docker-compose.yml +17 -0
  15. data/lib/water_drop.rb +4 -24
  16. data/lib/water_drop/config.rb +41 -142
  17. data/lib/water_drop/contracts.rb +0 -2
  18. data/lib/water_drop/contracts/config.rb +8 -121
  19. data/lib/water_drop/contracts/message.rb +41 -0
  20. data/lib/water_drop/errors.rb +31 -5
  21. data/lib/water_drop/instrumentation.rb +7 -0
  22. data/lib/water_drop/instrumentation/monitor.rb +16 -23
  23. data/lib/water_drop/instrumentation/stdout_listener.rb +113 -32
  24. data/lib/water_drop/producer.rb +143 -0
  25. data/lib/water_drop/producer/async.rb +51 -0
  26. data/lib/water_drop/producer/buffer.rb +113 -0
  27. data/lib/water_drop/producer/builder.rb +63 -0
  28. data/lib/water_drop/producer/dummy_client.rb +32 -0
  29. data/lib/water_drop/producer/statistics_decorator.rb +71 -0
  30. data/lib/water_drop/producer/status.rb +52 -0
  31. data/lib/water_drop/producer/sync.rb +65 -0
  32. data/lib/water_drop/version.rb +1 -1
  33. data/waterdrop.gemspec +5 -5
  34. metadata +27 -26
  35. metadata.gz.sig +0 -0
  36. data/.travis.yml +0 -35
  37. data/MIT-LICENCE +0 -18
  38. data/lib/water_drop/async_producer.rb +0 -26
  39. data/lib/water_drop/base_producer.rb +0 -57
  40. data/lib/water_drop/config_applier.rb +0 -52
  41. data/lib/water_drop/contracts/message_options.rb +0 -19
  42. data/lib/water_drop/sync_producer.rb +0 -24
data/README.md CHANGED
@@ -1,17 +1,26 @@
1
1
  # WaterDrop
2
2
 
3
- [![Build Status](https://travis-ci.org/karafka/waterdrop.svg)](https://travis-ci.org/karafka/waterdrop)
4
- [![Join the chat at https://gitter.im/karafka/karafka](https://badges.gitter.im/karafka/karafka.svg)](https://gitter.im/karafka/karafka?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
3
+ **Note**: Documentation presented here refers to WaterDrop `2.0.0`.
5
4
 
6
- Gem used to send messages to Kafka in an easy way with an extra validation layer. It is a part of the [Karafka](https://github.com/karafka/karafka) ecosystem.
5
+ WaterDrop `2.0` does **not** work with Karafka `1.*` and aims to either work as a standalone producer outside of Karafka `1.*` ecosystem or as a part of not yet released Karafka `2.0.*`.
6
+
7
+ Please refer to [this](https://github.com/karafka/waterdrop/tree/1.4) branch and it's documentation for details about WaterDrop `1.*` usage.
7
8
 
8
- WaterDrop is based on Zendesks [delivery_boy](https://github.com/zendesk/delivery_boy) gem.
9
+ [![Build Status](https://github.com/karafka/waterdrop/workflows/ci/badge.svg)](https://github.com/karafka/waterdrop/actions?query=workflow%3Aci)
10
+ [![Gem Version](https://badge.fury.io/rb/waterdrop.svg)](http://badge.fury.io/rb/waterdrop)
11
+ [![Join the chat at https://gitter.im/karafka/karafka](https://badges.gitter.im/karafka/karafka.svg)](https://gitter.im/karafka/karafka)
9
12
 
10
- It is:
13
+ Gem used to send messages to Kafka in an easy way with an extra validation layer. It is a part of the [Karafka](https://github.com/karafka/karafka) ecosystem.
11
14
 
12
- - Thread safe
13
- - Supports sync and async producers
14
- - Working with 0.11+ Kafka
15
+ It:
16
+
17
+ - Is thread safe
18
+ - Supports sync producing
19
+ - Supports async producing
20
+ - Supports buffering
21
+ - Supports producing messages to multiple clusters
22
+ - Supports multiple delivery policies
23
+ - Works with Kafka 1.0+ and Ruby 2.5+
15
24
 
16
25
  ## Installation
17
26
 
@@ -36,82 +45,213 @@ bundle install
36
45
  WaterDrop is a complex tool, that contains multiple configuration options. To keep everything organized, all the configuration options were divided into two groups:
37
46
 
38
47
  - WaterDrop options - options directly related to Karafka framework and it's components
39
- - Ruby-Kafka driver options - options related to Ruby-Kafka/Delivery boy
48
+ - Kafka driver options - options related to `Kafka`
40
49
 
41
- To apply all those configuration options, you need to use the ```#setup``` method:
50
+ To apply all those configuration options, you need to create a producer instance and use the ```#setup``` method:
42
51
 
43
52
  ```ruby
44
- WaterDrop.setup do |config|
53
+ producer = WaterDrop::Producer.new
54
+
55
+ producer.setup do |config|
45
56
  config.deliver = true
46
- config.kafka.seed_brokers = %w[kafka://localhost:9092]
57
+ config.kafka = {
58
+ 'bootstrap.servers': 'localhost:9092',
59
+ 'request.required.acks': 1
60
+ }
61
+ end
62
+ ```
63
+
64
+ or you can do the same while initializing the producer:
65
+
66
+ ```ruby
67
+ producer = WaterDrop::Producer.new do |config|
68
+ config.deliver = true
69
+ config.kafka = {
70
+ 'bootstrap.servers': 'localhost:9092',
71
+ 'request.required.acks': 1
72
+ }
47
73
  end
48
74
  ```
49
75
 
50
76
  ### WaterDrop configuration options
51
77
 
52
- | Option | Description |
53
- |-----------------------------|------------------------------------------------------------------|
54
- | client_id | This is how the client will identify itself to the Kafka brokers |
55
- | logger | Logger that we want to use |
56
- | deliver | Should we send messages to Kafka |
78
+ | Option | Description |
79
+ |--------------------|-----------------------------------------------------------------|
80
+ | `id` | id of the producer for instrumentation and logging |
81
+ | `logger` | Logger that we want to use |
82
+ | `deliver` | Should we send messages to Kafka or just fake the delivery |
83
+ | `max_wait_timeout` | Waits that long for the delivery report or raises an error |
84
+ | `wait_timeout` | Waits that long before re-check of delivery report availability |
57
85
 
58
- ### Ruby-Kafka driver and Delivery boy configuration options
86
+ ### Kafka configuration options
59
87
 
60
- **Note:** We've listed here only **the most important** configuration options. If you're interested in all the options, please go to the [config.rb](https://github.com/karafka/waterdrop/blob/master/lib/water_drop/config.rb) file for more details.
88
+ You can create producers with different `kafka` settings. Documentation of the available configuration options is available on https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md.
89
+
90
+ ## Usage
61
91
 
62
- **Note:** All the options are subject to validations. In order to check what is and what is not acceptable, please go to the [config.rb validation schema](https://github.com/karafka/waterdrop/blob/master/lib/water_drop/schemas/config.rb) file.
92
+ Please refer to the [documentation](https://www.rubydoc.info/gems/waterdrop) in case you're interested in the more advanced API.
63
93
 
64
- | Option | Description |
65
- |--------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
66
- | raise_on_buffer_overflow | Should we raise an exception, when messages can't be sent in an async way due to the message buffer overflow or should we just drop them |
67
- | delivery_interval | The number of seconds between background message deliveries. Disable timer-based background deliveries by setting this to 0. |
68
- | delivery_threshold | The number of buffered messages that will trigger a background message delivery. Disable buffer size based background deliveries by setting this to 0.|
69
- | required_acks | The number of Kafka replicas that must acknowledge messages before they're considered as successfully written. |
70
- | ack_timeout | A timeout executed by a broker when the client is sending messages to it. |
71
- | max_retries | The number of retries when attempting to deliver messages. |
72
- | retry_backoff | The number of seconds to wait after a failed attempt to send messages to a Kafka broker before retrying. |
73
- | max_buffer_bytesize | The maximum number of bytes allowed in the buffer before new messages are rejected. |
74
- | max_buffer_size | The maximum number of messages allowed in the buffer before new messages are rejected. |
75
- | max_queue_size | The maximum number of messages allowed in the queue before new messages are rejected. |
76
- | sasl_plain_username | The username used to authenticate. |
77
- | sasl_plain_password | The password used to authenticate. |
94
+ ### Basic usage
78
95
 
79
- This configuration can be also placed in *config/initializers* and can vary based on the environment:
96
+ To send Kafka messages, just create a producer and use it:
80
97
 
81
98
  ```ruby
82
- WaterDrop.setup do |config|
83
- config.deliver = Rails.env.production?
84
- config.kafka.seed_brokers = [Rails.env.production? ? 'kafka://prod-host:9091' : 'kafka://localhost:9092']
99
+ producer = WaterDrop::Producer.new
100
+
101
+ producer.setup do |config|
102
+ config.kafka = { 'bootstrap.servers': 'localhost:9092' }
85
103
  end
104
+
105
+ producer.produce_sync(topic: 'my-topic', payload: 'my message')
106
+
107
+ # or for async
108
+ producer.produce_async(topic: 'my-topic', payload: 'my message')
109
+
110
+ # or in batches
111
+ producer.produce_many_sync(
112
+ [
113
+ { topic: 'my-topic', payload: 'my message'},
114
+ { topic: 'my-topic', payload: 'my message'}
115
+ ]
116
+ )
117
+
118
+ # both sync and async
119
+ producer.produce_many_async(
120
+ [
121
+ { topic: 'my-topic', payload: 'my message'},
122
+ { topic: 'my-topic', payload: 'my message'}
123
+ ]
124
+ )
125
+
126
+ # Don't forget to close the producer once you're done to flush the internal buffers, etc
127
+ producer.close
86
128
  ```
87
129
 
88
- ## Usage
130
+ Each message that you want to publish, will have its value checked.
131
+
132
+ Here are all the things you can provide in the message hash:
133
+
134
+ | Option | Required | Value type | Description |
135
+ |-------------|----------|---------------|-------------------------------------------------------|
136
+ | `topic` | true | String | The Kafka topic that should be written to |
137
+ | `payload` | true | String | Data you want to send to Kafka |
138
+ | `key` | false | String | The key that should be set in the Kafka message |
139
+ | `partition` | false | Integer | A specific partition number that should be written to |
140
+ | `timestamp` | false | Time, Integer | The timestamp that should be set on the message |
141
+ | `headers` | false | Hash | Headers for the message |
142
+
143
+ Keep in mind, that message you want to send should be either binary or stringified (to_s, to_json, etc).
89
144
 
90
- To send Kafka messages, just use one of the producers:
145
+ ### Buffering
146
+
147
+ WaterDrop producers support buffering of messages, which means that you can easily implement periodic flushing for long running processes as well as buffer several messages to be flushed the same moment:
91
148
 
92
149
  ```ruby
93
- WaterDrop::SyncProducer.call('message', topic: 'my-topic')
94
- # or for async
95
- WaterDrop::AsyncProducer.call('message', topic: 'my-topic')
150
+ producer = WaterDrop::Producer.new
151
+
152
+ producer.setup do |config|
153
+ config.kafka = { 'bootstrap.servers': 'localhost:9092' }
154
+ end
155
+
156
+ time = Time.now - 10
157
+
158
+ while time < Time.now
159
+ time += 1
160
+ producer.buffer(topic: 'times', payload: Time.now.to_s)
161
+ end
162
+
163
+ puts "The messages buffer size #{producer.messages.size}"
164
+ producer.flush_sync
165
+ puts "The messages buffer size #{producer.message.size}"
166
+
167
+ producer.close
96
168
  ```
97
169
 
98
- Both ```SyncProducer``` and ```AsyncProducer``` accept following options:
170
+ ## Instrumentation
99
171
 
100
- | Option | Required | Value type | Description |
101
- |-------------------- |----------|------------|---------------------------------------------------------------------|
102
- | ```topic``` | true | String | The Kafka topic that should be written to |
103
- | ```key``` | false | String | The key that should be set in the Kafka message |
104
- | ```partition``` | false | Integer | A specific partition number that should be written to |
105
- | ```partition_key``` | false | String | A string that can be used to deterministically select the partition |
106
- | ```create_time``` | false | Time | The timestamp that should be set on the message |
107
- | ```headers``` | false | Hash | Headers for the message |
172
+ Each of the producers after the `#setup` is done, has a custom monitor to which you can subscribe.
108
173
 
109
- Keep in mind, that message you want to send should be either binary or stringified (to_s, to_json, etc).
174
+ ```ruby
175
+ producer = WaterDrop::Producer.new
176
+
177
+ producer.setup do |config|
178
+ config.kafka = { 'bootstrap.servers': 'localhost:9092' }
179
+ end
180
+
181
+ producer.monitor.subscribe('message.produced_async') do |event|
182
+ puts "A message was produced to '#{event[:message][:topic]}' topic!"
183
+ end
184
+
185
+ producer.produce_async(topic: 'events', payload: 'data')
186
+
187
+ producer.close
188
+ ```
189
+
190
+ See the `WaterDrop::Instrumentation::Monitor::EVENTS` for the list of all the supported events.
191
+
192
+ ### Usage statistics
193
+
194
+ WaterDrop may be configured to emit internal metrics at a fixed interval by setting the `kafka` `statistics.interval.ms` configuration property to a value > `0`. Once that is done, emitted statistics are available after subscribing to the `statistics.emitted` publisher event.
195
+
196
+ The statistics include all of the metrics from `librdkafka` (full list [here](https://github.com/edenhill/librdkafka/blob/master/STATISTICS.md)) as well as the diff of those against the previously emitted values.
197
+
198
+ For several attributes like `txmsgs`, `librdkafka` publishes only the totals. In order to make it easier to track the progress (for example number of messages sent between statistics emitted events), WaterDrop diffs all the numeric values against previously available numbers. All of those metrics are available under the same key as the metric but with additional `_d` postfix:
199
+
200
+
201
+ ```ruby
202
+ producer = WaterDrop::Producer.new do |config|
203
+ config.kafka = {
204
+ 'bootstrap.servers': 'localhost:9092',
205
+ 'statistics.interval.ms': 2_000 # emit statistics every 2 seconds
206
+ }
207
+ end
208
+
209
+ producer.monitor.subscribe('statistics.emitted') do |event|
210
+ sum = event[:statistics]['txmsgs']
211
+ diff = event[:statistics]['txmsgs_d']
212
+
213
+ p "Sent messages: #{sum}"
214
+ p "Messages sent from last statistics report: #{diff}"
215
+ end
216
+
217
+ sleep(2)
218
+
219
+ # Sent messages: 0
220
+ # Messages sent from last statistics report: 0
221
+
222
+ 20.times { producer.produce_async(topic: 'events', payload: 'data') }
223
+
224
+ # Sent messages: 20
225
+ # Messages sent from last statistics report: 20
226
+
227
+ sleep(2)
228
+
229
+ 20.times { producer.produce_async(topic: 'events', payload: 'data') }
230
+
231
+ # Sent messages: 40
232
+ # Messages sent from last statistics report: 20
233
+
234
+ sleep(2)
235
+
236
+ # Sent messages: 40
237
+ # Messages sent from last statistics report: 0
238
+
239
+ producer.close
240
+ ```
241
+
242
+ Note: The metrics returned may not be completely consistent between brokers, toppars and totals, due to the internal asynchronous nature of librdkafka. E.g., the top level tx total may be less than the sum of the broker tx values which it represents.
243
+
244
+ ### Forking and potential memory problems
245
+
246
+ If you work with forked processes, make sure you **don't** use the producer before the fork. You can easily configure the producer and then fork and use it.
247
+
248
+ To tackle this [obstacle](https://github.com/appsignal/rdkafka-ruby/issues/15) related to rdkafka, WaterDrop adds finalizer to each of the producers to close the rdkafka client before the Ruby process is shutdown. Due to the [nature of the finalizers](https://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/), this implementation prevents producers from being GCed (except upon VM shutdown) and can cause memory leaks if you don't use persistent/long-lived producers in a long-running process or if you don't use the `#close` method of a producer when it is no longer needed. Creating a producer instance for each message is anyhow a rather bad idea, so we recommend not to.
110
249
 
111
250
  ## References
112
251
 
252
+ * [WaterDrop code documentation](https://www.rubydoc.info/github/karafka/waterdrop)
113
253
  * [Karafka framework](https://github.com/karafka/karafka)
114
- * [WaterDrop Travis CI](https://travis-ci.org/karafka/waterdrop)
254
+ * [WaterDrop Actions CI](https://github.com/karafka/waterdrop/actions?query=workflow%3Ac)
115
255
  * [WaterDrop Coditsu](https://app.coditsu.io/karafka/repositories/waterdrop)
116
256
 
117
257
  ## Note on contributions
@@ -123,5 +263,3 @@ Each pull request must pass all the RSpec specs and meet our quality requirement
123
263
  To check if everything is as it should be, we use [Coditsu](https://coditsu.io) that combines multiple linters and code analyzers for both code and documentation. Once you're done with your changes, submit a pull request.
124
264
 
125
265
  Coditsu will automatically check your work against our quality standards. You can find your commit check results on the [builds page](https://app.coditsu.io/karafka/repositories/waterdrop/builds/commit_builds) of WaterDrop repository.
126
-
127
- [![coditsu](https://coditsu.io/assets/quality_bar.svg)](https://app.coditsu.io/karafka/repositories/waterdrop/builds/commit_builds)
data/config/errors.yml CHANGED
@@ -1,19 +1,6 @@
1
1
  en:
2
2
  dry_validation:
3
3
  errors:
4
- broker_schema: >
5
- has an invalid format.
6
- Expected schema, host and port number.
7
- Example: kafka://127.0.0.1:9092 or kafka+ssl://127.0.0.1:9092
8
- ssl_client_cert_with_ssl_client_cert_key: >
9
- Both ssl_client_cert and ssl_client_cert_key need to be provided.
10
- ssl_client_cert_key_with_ssl_client_cert: >
11
- Both ssl_client_cert_key and ssl_client_cert need to be provided.
12
- ssl_client_cert_chain_with_ssl_client_cert: >
13
- Both ssl_client_cert_chain and ssl_client_cert need to be provided.
14
- ssl_client_cert_chain_with_ssl_client_cert_key: >
15
- Both ssl_client_cert_chain and ssl_client_cert_key need to be provided.
16
- ssl_client_cert_key_password_with_ssl_client_cert_key: >
17
- Both ssl_client_cert_key_password and ssl_client_cert_key need to be provided.
18
- sasl_oauth_token_provider_respond_to_token: >
19
- sasl_oauth_token_provider needs to respond to a #token method.
4
+ invalid_key_type: all keys need to be of type String
5
+ invalid_value_type: all values need to be of type String
6
+ max_payload_size: is more than `max_payload_size` config value
@@ -0,0 +1,17 @@
1
+ version: '2'
2
+ services:
3
+ zookeeper:
4
+ image: wurstmeister/zookeeper
5
+ ports:
6
+ - "2181:2181"
7
+ kafka:
8
+ image: wurstmeister/kafka:1.0.1
9
+ ports:
10
+ - "9092:9092"
11
+ environment:
12
+ KAFKA_ADVERTISED_HOST_NAME: localhost
13
+ KAFKA_ADVERTISED_PORT: 9092
14
+ KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
15
+ KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
16
+ volumes:
17
+ - /var/run/docker.sock:/var/run/docker.sock
data/lib/water_drop.rb CHANGED
@@ -3,39 +3,19 @@
3
3
  # External components
4
4
  # delegate should be removed because we don't need it, we just add it because of ruby-kafka
5
5
  %w[
6
- delegate
7
- json
8
- delivery_boy
9
- singleton
6
+ concurrent/array
10
7
  dry-configurable
11
8
  dry/monitor/notifications
12
9
  dry-validation
10
+ rdkafka
11
+ json
13
12
  zeitwerk
13
+ securerandom
14
14
  ].each { |lib| require lib }
15
15
 
16
16
  # WaterDrop library
17
17
  module WaterDrop
18
18
  class << self
19
- attr_accessor :logger
20
-
21
- # Sets up the whole configuration
22
- # @param [Block] block configuration block
23
- def setup(&block)
24
- Config.setup(&block)
25
- DeliveryBoy.logger = self.logger = config.logger
26
- ConfigApplier.call(DeliveryBoy.config, Config.config.to_h)
27
- end
28
-
29
- # @return [WaterDrop::Config] config instance
30
- def config
31
- Config.config
32
- end
33
-
34
- # @return [::WaterDrop::Monitor] monitor that we want to use
35
- def monitor
36
- config.monitor
37
- end
38
-
39
19
  # @return [String] root path of this gem
40
20
  def gem_root
41
21
  Pathname.new(File.expand_path('..', __dir__))
@@ -5,158 +5,57 @@
5
5
  module WaterDrop
6
6
  # Configuration object for setting up all options required by WaterDrop
7
7
  class Config
8
- extend Dry::Configurable
9
-
10
- # Config schema definition
11
- # @note We use a single instance not to create new one upon each usage
12
- SCHEMA = Contracts::Config.new.freeze
13
-
14
- private_constant :SCHEMA
8
+ include Dry::Configurable
15
9
 
16
10
  # WaterDrop options
17
- # option client_id [String] identifier of this producer
18
- setting :client_id, 'waterdrop'
19
- # option [Instance, nil] logger that we want to use or nil to fallback to ruby-kafka logger
20
- setting :logger, Logger.new($stdout, level: Logger::WARN)
11
+ #
12
+ # option [String] id of the producer. This can be helpful when building producer specific
13
+ # instrumentation or loggers. It is not the kafka producer id
14
+ setting(:id, false) { |id| id || SecureRandom.uuid }
15
+ # option [Instance] logger that we want to use
16
+ # @note Due to how rdkafka works, this setting is global for all the producers
17
+ setting(:logger, false) { |logger| logger || Logger.new($stdout, level: Logger::WARN) }
21
18
  # option [Instance] monitor that we want to use. See instrumentation part of the README for
22
19
  # more details
23
- setting :monitor, WaterDrop::Instrumentation::Monitor.new
20
+ setting(:monitor, false) { |monitor| monitor || WaterDrop::Instrumentation::Monitor.new }
21
+ # option [Integer] max payload size allowed for delivery to Kafka
22
+ setting :max_payload_size, 1_000_012
23
+ # option [Integer] Wait that long for the delivery report or raise an error if this takes
24
+ # longer than the timeout.
25
+ setting :max_wait_timeout, 5
26
+ # option [Numeric] how long should we wait between re-checks on the availability of the
27
+ # delivery report. In a really robust systems, this describes the min-delivery time
28
+ # for a single sync message when produced in isolation
29
+ setting :wait_timeout, 0.005 # 5 milliseconds
24
30
  # option [Boolean] should we send messages. Setting this to false can be really useful when
25
- # testing and or developing because when set to false, won't actually ping Kafka
31
+ # testing and or developing because when set to false, won't actually ping Kafka but will
32
+ # run all the validations, etc
26
33
  setting :deliver, true
27
- # option [Boolean] if you're producing messages faster than the framework or the network can
28
- # send them off, ruby-kafka might reject them. If that happens, WaterDrop will either raise
29
- # or ignore - this setting manages that behavior. This only applies to async producer as
30
- # sync producer will always raise upon problems
31
- setting :raise_on_buffer_overflow, true
32
-
33
- # Settings directly related to the Kafka driver
34
- setting :kafka do
35
- # option [Array<String>] Array that contains Kafka seed broker hosts with ports
36
- setting :seed_brokers
37
-
38
- # Network timeouts
39
- # option connect_timeout [Integer] Sets the number of seconds to wait while connecting to
40
- # a broker for the first time. When ruby-kafka initializes, it needs to connect to at
41
- # least one host.
42
- setting :connect_timeout, 10
43
- # option socket_timeout [Integer] Sets the number of seconds to wait when reading from or
44
- # writing to a socket connection to a broker. After this timeout expires the connection
45
- # will be killed. Note that some Kafka operations are by definition long-running, such as
46
- # waiting for new messages to arrive in a partition, so don't set this value too low
47
- setting :socket_timeout, 30
48
-
49
- # Buffering for async producer
50
- # @option [Integer] The maximum number of bytes allowed in the buffer before new messages
51
- # are rejected.
52
- setting :max_buffer_bytesize, 10_000_000
53
- # @option [Integer] The maximum number of messages allowed in the buffer before new messages
54
- # are rejected.
55
- setting :max_buffer_size, 1000
56
- # @option [Integer] The maximum number of messages allowed in the queue before new messages
57
- # are rejected. The queue is used to ferry messages from the foreground threads of your
58
- # application to the background thread that buffers and delivers messages.
59
- setting :max_queue_size, 1000
60
-
61
- # option [Integer] A timeout executed by a broker when the client is sending messages to it.
62
- # It defines the number of seconds the broker should wait for replicas to acknowledge the
63
- # write before responding to the client with an error. As such, it relates to the
64
- # required_acks setting. It should be set lower than socket_timeout.
65
- setting :ack_timeout, 5
66
- # option [Integer] The number of seconds between background message
67
- # deliveries. Default is 10 seconds. Disable timer-based background deliveries by
68
- # setting this to 0.
69
- setting :delivery_interval, 10
70
- # option [Integer] The number of buffered messages that will trigger a background message
71
- # delivery. Default is 100 messages. Disable buffer size based background deliveries by
72
- # setting this to 0.
73
- setting :delivery_threshold, 100
74
- # option [Boolean]
75
- setting :idempotent, false
76
- # option [Boolean]
77
- setting :transactional, false
78
- # option [Integer]
79
- setting :transactional_timeout, 60
80
-
81
- # option [Integer] The number of retries when attempting to deliver messages.
82
- setting :max_retries, 2
83
- # option [Integer]
84
- setting :required_acks, -1
85
- # option [Integer]
86
- setting :retry_backoff, 1
87
-
88
- # option [Integer] The minimum number of messages that must be buffered before compression is
89
- # attempted. By default only one message is required. Only relevant if compression_codec
90
- # is set.
91
- setting :compression_threshold, 1
92
- # option [Symbol] The codec used to compress messages. Must be either snappy or gzip.
93
- setting :compression_codec, nil
94
-
95
- # SSL authentication related settings
96
- # option ca_cert [String, nil] SSL CA certificate
97
- setting :ssl_ca_cert, nil
98
- # option ssl_ca_cert_file_path [String, nil] SSL CA certificate file path
99
- setting :ssl_ca_cert_file_path, nil
100
- # option ssl_ca_certs_from_system [Boolean] Use the CA certs from your system's default
101
- # certificate store
102
- setting :ssl_ca_certs_from_system, false
103
- # option ssl_verify_hostname [Boolean] Verify the hostname for client certs
104
- setting :ssl_verify_hostname, true
105
- # option ssl_client_cert [String, nil] SSL client certificate
106
- setting :ssl_client_cert, nil
107
- # option ssl_client_cert_key [String, nil] SSL client certificate password
108
- setting :ssl_client_cert_key, nil
109
- # option sasl_gssapi_principal [String, nil] sasl principal
110
- setting :sasl_gssapi_principal, nil
111
- # option sasl_gssapi_keytab [String, nil] sasl keytab
112
- setting :sasl_gssapi_keytab, nil
113
- # option sasl_plain_authzid [String] The authorization identity to use
114
- setting :sasl_plain_authzid, ''
115
- # option sasl_plain_username [String, nil] The username used to authenticate
116
- setting :sasl_plain_username, nil
117
- # option sasl_plain_password [String, nil] The password used to authenticate
118
- setting :sasl_plain_password, nil
119
- # option sasl_scram_username [String, nil] The username used to authenticate
120
- setting :sasl_scram_username, nil
121
- # option sasl_scram_password [String, nil] The password used to authenticate
122
- setting :sasl_scram_password, nil
123
- # option sasl_scram_mechanism [String, nil] Scram mechanism, either 'sha256' or 'sha512'
124
- setting :sasl_scram_mechanism, nil
125
- # option sasl_over_ssl [Boolean] whether to enforce SSL with SASL
126
- setting :sasl_over_ssl, true
127
- # option ssl_client_cert_chain [String, nil] client cert chain or nil if not used
128
- setting :ssl_client_cert_chain, nil
129
- # option ssl_client_cert_key_password [String, nil] the password required to read
130
- # the ssl_client_cert_key
131
- setting :ssl_client_cert_key_password, nil
132
- # @param sasl_oauth_token_provider [Object, nil] OAuthBearer Token Provider instance that
133
- # implements method token.
134
- setting :sasl_oauth_token_provider, nil
135
- end
136
-
137
- class << self
138
- # Configuration method
139
- # @yield Runs a block of code providing a config singleton instance to it
140
- # @yieldparam [WaterDrop::Config] WaterDrop config instance
141
- def setup
142
- configure do |config|
143
- yield(config)
144
- validate!(config.to_h)
145
- end
34
+ # rdkafka options
35
+ # @see https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md
36
+ setting :kafka, {}
37
+
38
+ # Configuration method
39
+ # @yield Runs a block of code providing a config singleton instance to it
40
+ # @yieldparam [WaterDrop::Config] WaterDrop config instance
41
+ def setup
42
+ configure do |config|
43
+ yield(config)
44
+ validate!(config.to_h)
146
45
  end
46
+ end
147
47
 
148
- private
48
+ private
149
49
 
150
- # Validates the configuration and if anything is wrong, will raise an exception
151
- # @param config_hash [Hash] config hash with setup details
152
- # @raise [WaterDrop::Errors::InvalidConfiguration] raised when something is wrong with
153
- # the configuration
154
- def validate!(config_hash)
155
- validation_result = SCHEMA.call(config_hash)
156
- return true if validation_result.success?
50
+ # Validates the configuration and if anything is wrong, will raise an exception
51
+ # @param config_hash [Hash] config hash with setup details
52
+ # @raise [WaterDrop::Errors::ConfigurationInvalidError] raised when something is wrong with
53
+ # the configuration
54
+ def validate!(config_hash)
55
+ result = Contracts::Config.new.call(config_hash)
56
+ return true if result.success?
157
57
 
158
- raise Errors::InvalidConfiguration, validation_result.errors.to_h
159
- end
58
+ raise Errors::ConfigurationInvalidError, result.errors.to_h
160
59
  end
161
60
  end
162
61
  end