Skip to content

Commit 855f3ce

Browse files
committedDec 21, 2017
Lazy load compressors
1 parent 093f06a commit 855f3ce

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed
 

‎lib/kafka/compression.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ def self.codecs
2121
end
2222

2323
def self.find_codec(name)
24-
CODECS.fetch(name) do
24+
codec = CODECS.fetch(name) do
2525
raise "Unknown compression codec #{name}"
2626
end
27+
28+
codec.load
29+
30+
codec
2731
end
2832

2933
def self.find_codec_by_id(codec_id)

‎lib/kafka/gzip_codec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module Kafka
22
class GzipCodec
3-
def initialize
4-
require "zlib"
5-
end
6-
73
def codec_id
84
1
95
end
106

7+
def load
8+
require "zlib"
9+
end
10+
1111
def compress(data)
1212
buffer = StringIO.new
1313
buffer.set_encoding(Encoding::BINARY)

‎lib/kafka/lz4_codec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module Kafka
22
class LZ4Codec
3-
def initialize
3+
def codec_id
4+
3
5+
end
6+
7+
def load
48
require "extlz4"
59
rescue LoadError
610
raise LoadError, "using lz4 compression requires adding a dependency on the `extlz4` gem to your Gemfile."
711
end
812

9-
def codec_id
10-
3
11-
end
12-
1313
def compress(data)
1414
LZ4.encode(data)
1515
end

‎lib/kafka/snappy_codec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module Kafka
22
class SnappyCodec
3-
def initialize
3+
def codec_id
4+
2
5+
end
6+
7+
def load
48
require "snappy"
59
rescue LoadError
610
raise LoadError,
711
"Using snappy compression requires adding a dependency on the `snappy` gem to your Gemfile."
812
end
913

10-
def codec_id
11-
2
12-
end
13-
1414
def compress(data)
1515
Snappy.deflate(data)
1616
end

1 commit comments

Comments
 (1)

chadmetcalf-cb commented on Apr 20, 2021

@chadmetcalf-cb

Was just trying to figure out why we had to worry about building snappy when we don't use it anywhere. This is why, and what fixed it.

Thank You!

Please sign in to comment.