Commit f611e90a authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch 'sh-optimize-expire-in-parsing' into 'master'

Cache CI expire_in parsing

See merge request gitlab-org/gitlab!78953
parents 2390a213 5fc166b9
......@@ -16,9 +16,7 @@ module Gitlab
def validate_duration
return true if never?
parse
rescue ChronicDuration::DurationParseError
false
cached_parse
end
def seconds_from_now
......@@ -29,12 +27,28 @@ module Gitlab
attr_reader :value
def cached_parse
return validation_cache[value] if validation_cache.key?(value)
validation_cache[value] = safe_parse
end
def safe_parse
parse
rescue ChronicDuration::DurationParseError
false
end
def parse
return if never?
ChronicDuration.parse(value)
end
def validation_cache
Gitlab::SafeRequestStore[:ci_expire_in_parser_cache] ||= {}
end
def never?
value.to_s.casecmp('never') == 0
end
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe Gitlab::Ci::Build::Artifacts::ExpireInParser do
describe '.validate_duration' do
describe '.validate_duration', :request_store do
subject { described_class.validate_duration(value) }
context 'with never' do
......@@ -20,14 +20,33 @@ RSpec.describe Gitlab::Ci::Build::Artifacts::ExpireInParser do
context 'with a duration' do
let(:value) { '1 Day' }
let(:other_value) { '30 seconds' }
it { is_expected.to be_truthy }
it 'caches data' do
expect(ChronicDuration).to receive(:parse).with(value).once.and_call_original
expect(ChronicDuration).to receive(:parse).with(other_value).once.and_call_original
2.times do
expect(described_class.validate_duration(value)).to eq(86400)
expect(described_class.validate_duration(other_value)).to eq(30)
end
end
end
context 'without a duration' do
let(:value) { 'something' }
it { is_expected.to be_falsy }
it 'caches data' do
expect(ChronicDuration).to receive(:parse).with(value).once.and_call_original
2.times do
expect(described_class.validate_duration(value)).to be_falsey
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment