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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c879ad2675e462df020c9ea423891e439c9a180c
4
- data.tar.gz: 17736d6479420bcf9621f328fc8d841386a76074
3
+ metadata.gz: a041e23f8531766027d023622e51edea283f439e
4
+ data.tar.gz: 85b0453a007b36211da3d3eb9f324f8a6ac2c623
5
5
  SHA512:
6
- metadata.gz: 9780a0c3cc0dd70d150fc75b2c9929fad4ff2ad0d9055c9dce5dc38bad6bd2c36083b12253b8019b924ec6374aafa0d7d59c450d0a4fceed36713909259f49d4
7
- data.tar.gz: 6ef034f1724bbe081dbcb5cb512d103f550136a7d93b8f84ccaffd436e7940e327d8ce1a5de41ec99d4213f755246acbd6f2ea56d99a206d1ec6665bad6a70ae
6
+ metadata.gz: fc269f3951c22823b73d8313666de3aebca80f6d54da22319c7891e0cedbe95798f32b1c6132af7ba2365fd46a6526da1544e2a52b99641809eac5c7acd3f734
7
+ data.tar.gz: 2df20cdc5d45e98a56d588932b14e86527966f85e9536147c09609303d6bc019106292faa044a97608bf2d30057ba7fd2922a24cd60b894733326111f43da6e0
data/lib/cc/kafka.rb CHANGED
@@ -30,6 +30,10 @@ module CC
30
30
  def statsd
31
31
  @statsd ||= DummyStatsd.new
32
32
  end
33
+
34
+ def ssl_certificates
35
+ @ssl_certificates ||= []
36
+ end
33
37
  end
34
38
  end
35
39
  end
@@ -1,16 +1,11 @@
1
1
  require "bson"
2
- require "poseidon"
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
- SCHEMES = [
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
- serialized = BSON.serialize(data).to_s
26
-
27
- if http?
28
- Kafka.logger.debug("sending message over HTTP")
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
- unless http?
38
- producer.close
39
- end
27
+ producer.close
40
28
  end
41
29
 
42
30
  private
43
31
 
44
- def http?
45
- uri.scheme == HTTP
32
+ def producer
33
+ @producer ||= choose_producer
46
34
  end
47
35
 
48
- def send_http(serialized, key)
49
- http = Net::HTTP.new(uri.host, uri.port)
50
- http.open_timeout = HTTP_TIMEOUT
51
- http.read_timeout = HTTP_TIMEOUT
52
- request = Net::HTTP::Post.new("/")
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 send_poseidon(serialized, key)
67
- message = Poseidon::MessageToSend.new(topic, serialized, key)
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 producer
75
- @producer ||= Poseidon::Producer.new(
76
- broker,
77
- @client_id,
78
- compression_codec: :gzip
79
- )
48
+ def host
49
+ uri.host
80
50
  end
81
51
 
82
- def broker
83
- ["#{uri.host}:#{uri.port}"]
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
@@ -1,5 +1,5 @@
1
1
  module CC
2
2
  module Kafka
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
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.2.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-08 00:00:00.000000000 Z
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: