codeclimate-kafka 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cc/kafka.rb +4 -0
- data/lib/cc/kafka/producer.rb +21 -51
- data/lib/cc/kafka/producer/http.rb +61 -0
- data/lib/cc/kafka/producer/poseidon.rb +36 -0
- data/lib/cc/kafka/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a041e23f8531766027d023622e51edea283f439e
|
4
|
+
data.tar.gz: 85b0453a007b36211da3d3eb9f324f8a6ac2c623
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc269f3951c22823b73d8313666de3aebca80f6d54da22319c7891e0cedbe95798f32b1c6132af7ba2365fd46a6526da1544e2a52b99641809eac5c7acd3f734
|
7
|
+
data.tar.gz: 2df20cdc5d45e98a56d588932b14e86527966f85e9536147c09609303d6bc019106292faa044a97608bf2d30057ba7fd2922a24cd60b894733326111f43da6e0
|
data/lib/cc/kafka.rb
CHANGED
data/lib/cc/kafka/producer.rb
CHANGED
@@ -1,16 +1,11 @@
|
|
1
1
|
require "bson"
|
2
|
-
require "
|
2
|
+
require "cc/kafka/producer/http"
|
3
|
+
require "cc/kafka/producer/poseidon"
|
3
4
|
|
4
5
|
module CC
|
5
6
|
module Kafka
|
6
7
|
class Producer
|
7
|
-
|
8
|
-
HTTP = "http",
|
9
|
-
KAFKA = "kafka",
|
10
|
-
]
|
11
|
-
|
12
|
-
HTTPError = Class.new(StandardError)
|
13
|
-
HTTP_TIMEOUT= 60 # seconds
|
8
|
+
InvalidScheme = Class.new(StandardError)
|
14
9
|
|
15
10
|
def initialize(url, client_id = nil)
|
16
11
|
@url = url
|
@@ -22,65 +17,40 @@ module CC
|
|
22
17
|
def send_message(data, key = nil)
|
23
18
|
Kafka.logger.debug("data: #{data.inspect}, key: #{key.inspect}")
|
24
19
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
send_http(serialized, key)
|
30
|
-
else
|
31
|
-
Kafka.logger.debug("sending message direct via Poseidon")
|
32
|
-
send_poseidon(serialized, key)
|
33
|
-
end
|
20
|
+
producer.send_message(BSON.serialize(data).to_s, key)
|
21
|
+
rescue
|
22
|
+
producer.close
|
23
|
+
raise
|
34
24
|
end
|
35
25
|
|
36
26
|
def close
|
37
|
-
|
38
|
-
producer.close
|
39
|
-
end
|
27
|
+
producer.close
|
40
28
|
end
|
41
29
|
|
42
30
|
private
|
43
31
|
|
44
|
-
def
|
45
|
-
|
32
|
+
def producer
|
33
|
+
@producer ||= choose_producer
|
46
34
|
end
|
47
35
|
|
48
|
-
def
|
49
|
-
|
50
|
-
http.
|
51
|
-
|
52
|
-
|
53
|
-
request["Topic"] = topic
|
54
|
-
request["Key"] = key if key
|
55
|
-
request.body = serialized
|
56
|
-
|
57
|
-
Kafka.logger.debug("POST #{uri.host}:#{uri.port}/")
|
58
|
-
Kafka.logger.debug("data: #{serialized.inspect}")
|
59
|
-
response = http.request(request)
|
60
|
-
|
61
|
-
unless response.is_a?(Net::HTTPSuccess)
|
62
|
-
raise HTTPError, "request not successful: (#{response.code}) #{response.body}"
|
36
|
+
def choose_producer
|
37
|
+
case (scheme = uri.scheme)
|
38
|
+
when "http" then HTTP.new(host, port, topic)
|
39
|
+
when "kafka" then Poseidon.new(host, port, topic, @client_id)
|
40
|
+
else raise InvalidScheme, "invalid scheme #{scheme.inspect}"
|
63
41
|
end
|
64
42
|
end
|
65
43
|
|
66
|
-
def
|
67
|
-
|
68
|
-
producer.send_messages([message])
|
69
|
-
rescue
|
70
|
-
producer.close
|
71
|
-
raise
|
44
|
+
def scheme
|
45
|
+
uri.scheme
|
72
46
|
end
|
73
47
|
|
74
|
-
def
|
75
|
-
|
76
|
-
broker,
|
77
|
-
@client_id,
|
78
|
-
compression_codec: :gzip
|
79
|
-
)
|
48
|
+
def host
|
49
|
+
uri.host
|
80
50
|
end
|
81
51
|
|
82
|
-
def
|
83
|
-
|
52
|
+
def port
|
53
|
+
uri.port
|
84
54
|
end
|
85
55
|
|
86
56
|
def topic
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module CC
|
2
|
+
module Kafka
|
3
|
+
class Producer
|
4
|
+
class HTTP
|
5
|
+
HTTPError = Class.new(StandardError)
|
6
|
+
HTTP_TIMEOUT = 60 # seconds
|
7
|
+
|
8
|
+
def initialize(host, port, topic, ssl = false)
|
9
|
+
@host = host
|
10
|
+
@port = port
|
11
|
+
@topic = topic
|
12
|
+
@ssl = ssl
|
13
|
+
end
|
14
|
+
|
15
|
+
def send_message(message, key)
|
16
|
+
Kafka.logger.debug("sending message over HTTP")
|
17
|
+
http = Net::HTTP.new(@host, @port)
|
18
|
+
http.open_timeout = HTTP_TIMEOUT
|
19
|
+
http.read_timeout = HTTP_TIMEOUT
|
20
|
+
|
21
|
+
if ssl?
|
22
|
+
http.use_ssl = true
|
23
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
24
|
+
http.cert_store = certificate_store
|
25
|
+
end
|
26
|
+
|
27
|
+
request = Net::HTTP::Post.new("/")
|
28
|
+
request["Topic"] = @topic
|
29
|
+
request["Key"] = key if key
|
30
|
+
request.body = message
|
31
|
+
|
32
|
+
response = http.request(request)
|
33
|
+
|
34
|
+
unless response.is_a?(Net::HTTPSuccess)
|
35
|
+
raise HTTPError, "request not successful: (#{response.code}) #{response.body}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def close
|
40
|
+
# no-op
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def ssl?
|
46
|
+
@ssl
|
47
|
+
end
|
48
|
+
|
49
|
+
def certificate_store
|
50
|
+
OpenSSL::X509::Store.new.tap do |store|
|
51
|
+
store.set_default_paths
|
52
|
+
|
53
|
+
Kafka.ssl_certificates.each do |file|
|
54
|
+
store.add_file(file)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "poseidon"
|
2
|
+
|
3
|
+
module CC
|
4
|
+
module Kafka
|
5
|
+
class Producer
|
6
|
+
class Poseidon
|
7
|
+
def initialize(host, port, topic, client_id)
|
8
|
+
@brokers = ["#{host}:#{port}"]
|
9
|
+
@topic = topic
|
10
|
+
@client_id = client_id
|
11
|
+
end
|
12
|
+
|
13
|
+
def send_message(message, key)
|
14
|
+
Kafka.logger.debug("sending message direct via Poseidon")
|
15
|
+
producer.send_messages([
|
16
|
+
::Poseidon::MessageToSend.new(@topic, message, key)
|
17
|
+
])
|
18
|
+
end
|
19
|
+
|
20
|
+
def close
|
21
|
+
producer.close
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def producer
|
27
|
+
@producer ||= ::Poseidon::Producer.new(
|
28
|
+
@brokers,
|
29
|
+
@client_id,
|
30
|
+
compression_codec: :gzip
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/cc/kafka/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codeclimate-kafka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code Climate
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bson
|
@@ -75,6 +75,8 @@ files:
|
|
75
75
|
- lib/cc/kafka.rb
|
76
76
|
- lib/cc/kafka/consumer.rb
|
77
77
|
- lib/cc/kafka/producer.rb
|
78
|
+
- lib/cc/kafka/producer/http.rb
|
79
|
+
- lib/cc/kafka/producer/poseidon.rb
|
78
80
|
- lib/cc/kafka/version.rb
|
79
81
|
homepage: https://codeclimate.com
|
80
82
|
licenses:
|