bps-kafka 0.0.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c89e50cf3f915d9894d258b09ebcaa65a7ad4baf300c63449b9f6e1ac5d8222
4
- data.tar.gz: 5ebf8065eb7ca3439442e3d40c22279bcf2d593e57699950e21f3899fc0dad17
3
+ metadata.gz: 332d68ff69826c69b3a32c7d343e7760f6eab8441a2264667138351e189b5a08
4
+ data.tar.gz: b5f753df0a51c938e2fdf0930d9f233b13fb268c2e1960809f2ffda3fda94fc7
5
5
  SHA512:
6
- metadata.gz: fe48f42b65ab43a8e946fd021d2b31ada7e36412114beaa5b756c37992c8b261ff0b8e9eea8d3b97900b94090dc4e56ffaa69d20c4aee398ebb0a306d2682aff
7
- data.tar.gz: c49f9cbf9e4b1e1999acabc9d499133ed298610cc05550408c739e4da0c44dc6f51d3dca6ab94ad7b2cdd24b30a538b3ea0c5831da87065c132cbc425a083dcf
6
+ metadata.gz: 8a524f30fc1066a99be4e33ac4ada7e4c3ca39c8a705d695589ebfea89d4162b9f0b0e5792150d087b6b28a7fe7de546f13402d62c599acdc27a130295e8f2b9
7
+ data.tar.gz: f6aded50c6d824e192ce981c36568793714c07b9b19bbb5f9347e424ba22abd1151e7ef927eb6713dc945cfd0d512efdef86b04f204772e56b48f863e52cca4d
@@ -6,13 +6,11 @@ require 'bps/publisher/kafka_async'
6
6
  module BPS
7
7
  module Publisher
8
8
  register('kafka+sync') do |url, **opts|
9
- addrs = CGI.unescape(url.host).split(',')
10
- Kafka.new(addrs, **Kafka.coercer.coerce(opts))
9
+ Kafka.new(url, **Kafka.coercer.coerce(opts))
11
10
  end
12
11
 
13
12
  register('kafka') do |url, **opts|
14
- addrs = CGI.unescape(url.host).split(',')
15
- KafkaAsync.new(addrs, **Kafka.coercer.coerce(opts))
13
+ KafkaAsync.new(url, **Kafka.coercer.coerce(opts))
16
14
  end
17
15
  end
18
16
  end
@@ -3,8 +3,10 @@ require 'bps/kafka'
3
3
  module BPS
4
4
  module Publisher
5
5
  class Kafka < Abstract
6
- class Topic
6
+ class Topic < Abstract::Topic
7
7
  def initialize(producer, topic)
8
+ super()
9
+
8
10
  @producer = producer
9
11
  @topic = topic
10
12
  end
@@ -14,7 +16,7 @@ module BPS
14
16
  after_publish
15
17
  end
16
18
 
17
- def flush
19
+ def flush(**)
18
20
  @producer.deliver_messages
19
21
  end
20
22
 
@@ -74,12 +76,15 @@ module BPS
74
76
  @coercer ||= BPS::Coercer.new(CLIENT_OPTS.merge(PRODUCER_OPTS)).freeze
75
77
  end
76
78
 
77
- # @param [Array<String>] brokers the seed broker addresses.
79
+ # @param [Array<String>,URI] brokers the seed broker addresses.
78
80
  # @param [Hash] opts the options.
79
81
  # @see https://www.rubydoc.info/gems/ruby-kafka/Kafka/Client#initialize-instance_method
80
82
  def initialize(broker_addrs, **opts)
81
- @topics = {}
82
- @client = ::Kafka.new(broker_addrs, **opts.slice(*CLIENT_OPTS.keys))
83
+ super()
84
+
85
+ broker_addrs = parse_url(broker_addrs) if broker_addrs.is_a?(URI)
86
+ @topics = {}
87
+ @client = ::Kafka.new(broker_addrs, **opts.slice(*CLIENT_OPTS.keys))
83
88
  @producer = init_producer(**opts.slice(*PRODUCER_OPTS.keys))
84
89
  end
85
90
 
@@ -94,6 +99,14 @@ module BPS
94
99
 
95
100
  private
96
101
 
102
+ def parse_url(url)
103
+ port = url.port&.to_s || '9092'
104
+ CGI.unescape(url.host).split(',').map do |addr|
105
+ addr << ':' << port unless /:\d+$/.match?(addr)
106
+ addr
107
+ end
108
+ end
109
+
97
110
  def init_producer(**opts)
98
111
  @producer = @client.producer(**opts)
99
112
  end
@@ -14,18 +14,18 @@ module BPS
14
14
  # @param [Hash] opts the options.
15
15
  # @option opts [Integer] :max_queue_size (defaults to: 1000)
16
16
  # the maximum number of messages allowed in the queue.
17
- # @option opts [Integer] :delivery_threshold (defaults to: 0)
17
+ # @option opts [Integer] :delivery_threshold (defaults to: 1000)
18
18
  # if greater than zero, the number of buffered messages that will automatically
19
19
  # trigger a delivery.
20
- # @option opts [Integer] :delivery_interval (defaults to: 0) if greater than zero, the number of
20
+ # @option opts [Integer] :delivery_interval (defaults to: 30) if greater than zero, the number of
21
21
  # seconds between automatic message deliveries.
22
- def initialize(broker_addrs, **opts)
22
+ def initialize(broker_addrs, **opts) # rubocop:disable Lint/UselessMethodDefinition
23
23
  super
24
24
  end
25
25
 
26
26
  private
27
27
 
28
- def init_producer(max_queue_size: 1000, delivery_threshold: 0, delivery_interval: 0)
28
+ def init_producer(max_queue_size: 1000, delivery_threshold: 1000, delivery_interval: 30)
29
29
  @client.async_producer(
30
30
  max_queue_size: max_queue_size,
31
31
  delivery_threshold: delivery_threshold,
@@ -1,38 +1,64 @@
1
1
  require 'spec_helper'
2
2
  require 'bps/kafka'
3
3
 
4
- def kafka_addrs
5
- ENV.fetch('KAFKA_ADDRS', '127.0.0.1:9092').split(',').freeze
6
- end
4
+ RSpec.describe 'Kafka', kafka: true do
5
+ context 'with addr resolving' do
6
+ let(:client) { instance_double(::Kafka::Client, producer: nil) }
7
7
 
8
- run_spec = \
9
- begin
10
- ::Kafka.new(kafka_addrs).brokers
11
- true
12
- rescue StandardError => e
13
- warn "WARNING: unable to run #{File.basename __FILE__}: #{e.message}"
14
- false
15
- end
8
+ before { allow(::Kafka).to receive(:new).and_return(client) }
16
9
 
17
- helper = proc do
18
- def read_messages(topic_name, num_messages)
19
- client = ::Kafka.new(kafka_addrs)
20
- Enumerator.new do |y|
21
- client.each_message(topic: topic_name, start_from_beginning: true) do |msg|
22
- y << msg.value
10
+ it 'resolves simple URLs' do
11
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7')
12
+ allow(::Kafka).to receive(:new).with(['test.host:9092'], {}).and_return(client)
13
+ else
14
+ allow(::Kafka).to receive(:new).with(['test.host:9092']).and_return(client)
23
15
  end
24
- end.take(num_messages)
25
- ensure
26
- client&.close
27
- end
28
- end
16
+ BPS::Publisher.resolve(URI.parse('kafka+sync://test.host:9092'))
17
+ end
18
+
19
+ it 'resolves URLs with multiple hosts' do
20
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7')
21
+ allow(::Kafka).to receive(:new).with(['foo.host:9092', 'bar.host:9092'], {}).and_return(client)
22
+ else
23
+ allow(::Kafka).to receive(:new).with(['foo.host:9092', 'bar.host:9092']).and_return(client)
24
+ end
25
+ BPS::Publisher.resolve(URI.parse('kafka+sync://foo.host,bar.host:9092'))
26
+ end
29
27
 
30
- RSpec.describe 'Kafka', if: run_spec do
31
- context BPS::Publisher::Kafka do
32
- it_behaves_like 'publisher', url: "kafka+sync://#{kafka_addrs.join(',')}/", &helper
28
+ it 'resolves URLs with multiple hosts/ports' do
29
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7')
30
+ allow(::Kafka).to receive(:new).with(['foo.host:9093', 'bar.host:9092'], {}).and_return(client)
31
+ else
32
+ allow(::Kafka).to receive(:new).with(['foo.host:9093', 'bar.host:9092']).and_return(client)
33
+ end
34
+ BPS::Publisher.resolve(URI.parse('kafka+sync://foo.host%3A9093,bar.host'))
35
+ end
33
36
  end
34
37
 
35
- context BPS::Publisher::KafkaAsync do
36
- it_behaves_like 'publisher', url: "kafka://#{kafka_addrs.join(',')}/", &helper
38
+ describe 'publishers' do
39
+ let(:kafka_addrs) { ENV.fetch('KAFKA_ADDRS', '127.0.0.1:9092').split(',') }
40
+
41
+ def read_messages(topic_name, num_messages)
42
+ client = ::Kafka.new(kafka_addrs)
43
+ Enumerator.new do |y|
44
+ client.each_message(topic: topic_name, start_from_beginning: true) do |msg|
45
+ y << msg.value
46
+ end
47
+ end.take(num_messages)
48
+ ensure
49
+ client&.close
50
+ end
51
+
52
+ context BPS::Publisher::Kafka do
53
+ let(:publisher_url) { "kafka+sync://#{CGI.escape(kafka_addrs.join(','))}/" }
54
+
55
+ it_behaves_like 'publisher'
56
+ end
57
+
58
+ context BPS::Publisher::KafkaAsync do
59
+ let(:publisher_url) { "kafka://#{CGI.escape(kafka_addrs.join(','))}/" }
60
+
61
+ it_behaves_like 'publisher'
62
+ end
37
63
  end
38
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bps-kafka
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Black Square Media
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-01 00:00:00.000000000 Z
11
+ date: 2021-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bps
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.2
19
+ version: 0.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.2
26
+ version: 0.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ruby-kafka
29
29
  requirement: !ruby/object:Gem::Requirement