Class: Gitlab::SidekiqMiddleware::SizeLimiter::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/sidekiq_middleware/size_limiter/compressor.rb

Constant Summary collapse

PayloadDecompressionConflictError =
Class.new(StandardError)
PayloadDecompressionError =
Class.new(StandardError)
COMPRESS_LEVEL =

Level 5 is a good trade-off between space and time gitlab.com/gitlab-com/gl-infra/scalability/-/issues/1054#note_568129605

5
ORIGINAL_SIZE_KEY =
'original_job_size_bytes'
COMPRESSED_KEY =
'compressed'

Class Method Summary collapse

Class Method Details

.compress(job, job_args) ⇒ Object


20
21
22
23
24
25
26
27
28
# File 'lib/gitlab/sidekiq_middleware/size_limiter/compressor.rb', line 20

def self.compress(job, job_args)
  compressed_args = Base64.strict_encode64(Zlib::Deflate.deflate(job_args, COMPRESS_LEVEL))

  job[COMPRESSED_KEY] = true
  job[ORIGINAL_SIZE_KEY] = job_args.bytesize
  job['args'] = [compressed_args]

  compressed_args
end

.compressed?(job) ⇒ Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/gitlab/sidekiq_middleware/size_limiter/compressor.rb', line 16

def self.compressed?(job)
  job&.has_key?(COMPRESSED_KEY)
end

.decompress(job) ⇒ Object


30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/sidekiq_middleware/size_limiter/compressor.rb', line 30

def self.decompress(job)
  return unless compressed?(job)

  validate_args!(job)

  job.except!(ORIGINAL_SIZE_KEY, COMPRESSED_KEY)
  job['args'] = Sidekiq.load_json(Zlib::Inflate.inflate(Base64.strict_decode64(job['args'].first)))
rescue Zlib::Error
  raise PayloadDecompressionError, 'Fail to decompress Sidekiq job payload'
end

.validate_args!(job) ⇒ Object


41
42
43
44
45
46
47
48
# File 'lib/gitlab/sidekiq_middleware/size_limiter/compressor.rb', line 41

def self.validate_args!(job)
  if job['args'] && job['args'].length != 1
    exception = PayloadDecompressionConflictError.new('Sidekiq argument list should include 1 argument.\
                                                      This means that there is another a middleware interfering with the job payload.\
                                                      That conflicts with the payload compressor')
    ::Gitlab::ErrorTracking.track_and_raise_exception(exception)
  end
end