bps-kafka 0.0.3 → 0.2.1

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: 68826145fba6a103c652c008207089c913a18999a41acdcc35082d526a8979b1
4
- data.tar.gz: f14e9f89c8ff5d968b744483e381abb41f6b1265d6d696688a31439f0fdbf591
3
+ metadata.gz: 98dcf92fca51a26782f1ac6824503e60172860b0eddaa0ef076b5cc47e3cad0d
4
+ data.tar.gz: 447de9c24af68374ed0b5b8a50e24ea1713ba485ed76c2ab021290c71e806d73
5
5
  SHA512:
6
- metadata.gz: b9b4d9ae93a101c874f0cd2f0a5f07b7b577e6526de8a3ccc978cd346344d768e354202aa6b4652589334e0a1c56d5a556c1053929b2e3c1a1152d9556911571
7
- data.tar.gz: 52d62b30b81d2f18a1098a47eff1314406aef6b371293ca0ef9ae10b17c8fa1a53043e5c21d7490b10717255fbb9408fa1df67879eab36e947cf06fe27782500
6
+ metadata.gz: e56d669ed4fabdd2b8068ddc1d22d5efd2c6ccf0d6d05052303ba95eb798cf35549835fec2f6a959a50600cce076bd6965714f32366467475afe8cc44fe7daf5
7
+ data.tar.gz: 9c97c002fb7dbae88abea0c0572dd5e7c92a121a45c0da12098677f92296c193754b584c1a422e5a91a9052638826f3a14071b9af1fa1a6e5d44d85855b283b4
data/lib/bps/kafka.rb CHANGED
@@ -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
@@ -76,14 +76,15 @@ module BPS
76
76
  @coercer ||= BPS::Coercer.new(CLIENT_OPTS.merge(PRODUCER_OPTS)).freeze
77
77
  end
78
78
 
79
- # @param [Array<String>] brokers the seed broker addresses.
79
+ # @param [Array<String>,URI] brokers the seed broker addresses.
80
80
  # @param [Hash] opts the options.
81
81
  # @see https://www.rubydoc.info/gems/ruby-kafka/Kafka/Client#initialize-instance_method
82
82
  def initialize(broker_addrs, **opts)
83
83
  super()
84
84
 
85
- @topics = {}
86
- @client = ::Kafka.new(broker_addrs, **opts.slice(*CLIENT_OPTS.keys))
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))
87
88
  @producer = init_producer(**opts.slice(*PRODUCER_OPTS.keys))
88
89
  end
89
90
 
@@ -98,6 +99,14 @@ module BPS
98
99
 
99
100
  private
100
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
+
101
110
  def init_producer(**opts)
102
111
  @producer = @client.producer(**opts)
103
112
  end
@@ -14,10 +14,10 @@ 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
22
  def initialize(broker_addrs, **opts) # rubocop:disable Lint/UselessMethodDefinition
23
23
  super
@@ -25,7 +25,7 @@ module BPS
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.3
4
+ version: 0.2.1
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-09-08 00:00:00.000000000 Z
11
+ date: 2021-03-05 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.3
19
+ version: 0.2.1
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.3
26
+ version: 0.2.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ruby-kafka
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubygems_version: 3.1.2
72
+ rubygems_version: 3.1.4
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: Kafka adapter for bps