Commit 4d66cb9a authored by Stan Hu's avatar Stan Hu

Merge branch 'sh-limit-web-hook-backoffs' into 'master'

Limit updates to Web Hook backoff interval

See merge request gitlab-org/gitlab!69955
parents ffa1d139 95ab2922
...@@ -80,6 +80,8 @@ class WebHook < ApplicationRecord ...@@ -80,6 +80,8 @@ class WebHook < ApplicationRecord
end end
def backoff! def backoff!
return if backoff_count >= MAX_FAILURES && disabled_until && disabled_until > Time.current
assign_attributes(disabled_until: next_backoff.from_now, backoff_count: backoff_count.succ.clamp(0, MAX_FAILURES)) assign_attributes(disabled_until: next_backoff.from_now, backoff_count: backoff_count.succ.clamp(0, MAX_FAILURES))
save(validate: false) save(validate: false)
end end
......
...@@ -10,7 +10,11 @@ RSpec.describe WebHook do ...@@ -10,7 +10,11 @@ RSpec.describe WebHook do
let(:hook) { build(:project_hook, project: project) } let(:hook) { build(:project_hook, project: project) }
around do |example| around do |example|
freeze_time { example.run } if example.metadata[:skip_freeze_time]
example.run
else
freeze_time { example.run }
end
end end
describe 'associations' do describe 'associations' do
...@@ -326,10 +330,28 @@ RSpec.describe WebHook do ...@@ -326,10 +330,28 @@ RSpec.describe WebHook do
expect { hook.backoff! }.to change(hook, :backoff_count).by(1) expect { hook.backoff! }.to change(hook, :backoff_count).by(1)
end end
it 'does not let the backoff count exceed the maximum failure count' do context 'when we have backed off MAX_FAILURES times' do
hook.backoff_count = described_class::MAX_FAILURES before do
stub_const("#{described_class}::MAX_FAILURES", 5)
5.times { hook.backoff! }
end
it 'does not let the backoff count exceed the maximum failure count' do
expect { hook.backoff! }.not_to change(hook, :backoff_count)
end
it 'does not change disabled_until', :skip_freeze_time do
travel_to(hook.disabled_until - 1.minute) do
expect { hook.backoff! }.not_to change(hook, :disabled_until)
end
end
expect { hook.backoff! }.not_to change(hook, :backoff_count) it 'changes disabled_until when it has elapsed', :skip_freeze_time do
travel_to(hook.disabled_until + 1.minute) do
expect { hook.backoff! }.to change { hook.disabled_until }
expect(hook.backoff_count).to eq(described_class::MAX_FAILURES)
end
end
end end
include_examples 'is tolerant of invalid records' do include_examples 'is tolerant of invalid records' do
......
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