Reduce sleep
time in request pool spec (#25470)
This commit is contained in:
parent
cec4f1d506
commit
e1c9d52e91
2 changed files with 20 additions and 10 deletions
|
@ -28,8 +28,9 @@ class RequestPool
|
||||||
end
|
end
|
||||||
|
|
||||||
MAX_IDLE_TIME = 30
|
MAX_IDLE_TIME = 30
|
||||||
WAIT_TIMEOUT = 5
|
|
||||||
MAX_POOL_SIZE = ENV.fetch('MAX_REQUEST_POOL_SIZE', 512).to_i
|
MAX_POOL_SIZE = ENV.fetch('MAX_REQUEST_POOL_SIZE', 512).to_i
|
||||||
|
REAPER_FREQUENCY = 30
|
||||||
|
WAIT_TIMEOUT = 5
|
||||||
|
|
||||||
class Connection
|
class Connection
|
||||||
attr_reader :site, :last_used_at, :created_at, :in_use, :dead, :fresh
|
attr_reader :site, :last_used_at, :created_at, :in_use, :dead, :fresh
|
||||||
|
@ -98,7 +99,7 @@ class RequestPool
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@pool = ConnectionPool::SharedConnectionPool.new(size: MAX_POOL_SIZE, timeout: WAIT_TIMEOUT) { |site| Connection.new(site) }
|
@pool = ConnectionPool::SharedConnectionPool.new(size: MAX_POOL_SIZE, timeout: WAIT_TIMEOUT) { |site| Connection.new(site) }
|
||||||
@reaper = Reaper.new(self, 30)
|
@reaper = Reaper.new(self, REAPER_FREQUENCY)
|
||||||
@reaper.run
|
@reaper.run
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -48,16 +48,25 @@ describe RequestPool do
|
||||||
expect(subject.size).to be > 1
|
expect(subject.size).to be > 1
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'closes idle connections' do
|
context 'with an idle connection' do
|
||||||
stub_request(:get, 'http://example.com/').to_return(status: 200, body: 'Hello!')
|
before do
|
||||||
|
stub_const('RequestPool::MAX_IDLE_TIME', 1) # Lower idle time limit to 1 seconds
|
||||||
subject.with('http://example.com') do |http_client|
|
stub_const('RequestPool::REAPER_FREQUENCY', 0.1) # Run reaper every 0.1 seconds
|
||||||
http_client.get('/').flush
|
stub_request(:get, 'http://example.com/').to_return(status: 200, body: 'Hello!')
|
||||||
end
|
end
|
||||||
|
|
||||||
expect(subject.size).to eq 1
|
it 'closes the connections' do
|
||||||
sleep RequestPool::MAX_IDLE_TIME + 30 + 1
|
subject.with('http://example.com') do |http_client|
|
||||||
expect(subject.size).to eq 0
|
http_client.get('/').flush
|
||||||
|
end
|
||||||
|
|
||||||
|
expect { reaper_observes_idle_timeout }.to change(subject, :size).from(1).to(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def reaper_observes_idle_timeout
|
||||||
|
# One full idle period and 2 reaper cycles more
|
||||||
|
sleep RequestPool::MAX_IDLE_TIME + (RequestPool::REAPER_FREQUENCY * 2)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue