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 +4 -4
- data/lib/bps/kafka.rb +2 -4
- data/lib/bps/publisher/kafka.rb +12 -3
- data/lib/bps/publisher/kafka_async.rb +3 -3
- data/spec/bps/kafka_spec.rb +53 -27
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98dcf92fca51a26782f1ac6824503e60172860b0eddaa0ef076b5cc47e3cad0d
|
4
|
+
data.tar.gz: 447de9c24af68374ed0b5b8a50e24ea1713ba485ed76c2ab021290c71e806d73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
@@ -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
|
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
|
-
|
86
|
-
@
|
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:
|
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
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:
|
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.
|
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:
|
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.
|
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.
|
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.
|
72
|
+
rubygems_version: 3.1.4
|
73
73
|
signing_key:
|
74
74
|
specification_version: 4
|
75
75
|
summary: Kafka adapter for bps
|