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 +4 -4
- data/lib/bps/kafka.rb +2 -4
- data/lib/bps/publisher/kafka.rb +18 -5
- data/lib/bps/publisher/kafka_async.rb +4 -4
- data/spec/bps/kafka_spec.rb +53 -27
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 332d68ff69826c69b3a32c7d343e7760f6eab8441a2264667138351e189b5a08
|
4
|
+
data.tar.gz: b5f753df0a51c938e2fdf0930d9f233b13fb268c2e1960809f2ffda3fda94fc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a524f30fc1066a99be4e33ac4ada7e4c3ca39c8a705d695589ebfea89d4162b9f0b0e5792150d087b6b28a7fe7de546f13402d62c599acdc27a130295e8f2b9
|
7
|
+
data.tar.gz: f6aded50c6d824e192ce981c36568793714c07b9b19bbb5f9347e424ba22abd1151e7ef927eb6713dc945cfd0d512efdef86b04f204772e56b48f863e52cca4d
|
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
|
-
|
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
|
-
|
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
|
data/lib/bps/publisher/kafka.rb
CHANGED
@@ -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
|
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
|
-
|
82
|
-
|
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:
|
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:
|
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:
|
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,
|
data/spec/bps/kafka_spec.rb
CHANGED
@@ -1,38 +1,64 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'bps/kafka'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
36
|
-
|
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
|
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:
|
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
|
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
|
26
|
+
version: 0.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ruby-kafka
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|