Skip to content
Snippets Groups Projects
Commit f15f110a authored by Dmitry Gruzd's avatar Dmitry Gruzd :two:
Browse files

Concurrency limit: allow pausing workers

Changelog: changed
parent 9f2022b8
No related branches found
No related tags found
1 merge request!165218Concurrency limit: allow pausing workers
......@@ -388,7 +388,8 @@ WARNING:
If there is a sustained workload over the limit, the `LIST` is going to grow until the limit is disabled or
the workload drops under the limit.
You should use a lambda to define the limit. If it returns `nil`, `0`, or a negative value, the limit won't be applied.
You should use a lambda to define the limit. If it returns `nil` or `0`, the limit won't be applied.
Negative numbers pause the execution.
```ruby
class LimitedWorker
......
......@@ -23,8 +23,9 @@ def limit_for(worker:)
def over_the_limit?(worker:)
limit_proc = limit_for(worker: worker)
limit = limit_proc&.call
return false if limit.to_i <= 0
limit = limit_proc&.call.to_i
return false if limit == 0
return true if limit < 0
current = ::Gitlab::SidekiqMiddleware::ConcurrencyLimit::WorkersConcurrency.current_for(worker: worker)
......
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe Gitlab::SidekiqMiddleware::ConcurrencyLimit::WorkersMap, feature_category: :global_search do
using RSpec::Parameterized::TableSyntax
let(:worker_class) do
Class.new do
def self.name
......@@ -46,22 +48,31 @@ def perform(*); end
describe '.over_the_limit?' do
subject(:over_the_limit?) { described_class.over_the_limit?(worker: worker_class) }
it 'returns false if no limit is set' do
expect(described_class).to receive(:limit_for).and_return(nil)
expect(over_the_limit?).to be_falsey
where(:limit, :current, :result) do
nil | 0 | false
nil | 5 | false
-> { nil } | 0 | false
-> { nil } | 5 | false
-> { 0 } | 0 | false
-> { 0 } | 10 | false
-> { 5 } | 10 | true
-> { 10 } | 0 | false
-> { 10 } | 5 | false
-> { -1 } | 0 | true
-> { -1 } | 1 | true
-> { -10 } | 10 | true
end
it 'returns false if under the limit' do
allow(::Gitlab::SidekiqMiddleware::ConcurrencyLimit::WorkersConcurrency).to receive(:current_for).and_return(50)
expect(over_the_limit?).to be_falsey
end
it 'returns true if over the limit' do
allow(::Gitlab::SidekiqMiddleware::ConcurrencyLimit::WorkersConcurrency).to receive(:current_for).and_return(100)
with_them do
before do
allow(described_class).to receive(:limit_for).and_return(limit)
allow(::Gitlab::SidekiqMiddleware::ConcurrencyLimit::WorkersConcurrency).to receive(:current_for)
.and_return(current)
end
expect(over_the_limit?).to be_truthy
it 'returns correct result' do
expect(over_the_limit?).to eq(result)
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment