From 25990707990cea594fad40c1fbb19f2dabf3b170 Mon Sep 17 00:00:00 2001 From: Tim Downey Date: Tue, 24 Nov 2020 15:47:38 -0800 Subject: [PATCH] use TCPServer for rake-task readiness checks - the Socket we were opening would pass the readiness probe checks initially but begin failing later - this replaces it with a TCPServer that runs in a background thread that can accept multiple readiness probe connections - addresses https://github.com/cloudfoundry/capi-k8s-release/issues/91 [#175388005](https://www.pivotaltracker.com/story/show/175388005) Authored-by: Tim Downey --- .../background_job_environment.rb | 27 ++++++++++++------- .../lib/background_job_environment_spec.rb | 3 +-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/cloud_controller/background_job_environment.rb b/lib/cloud_controller/background_job_environment.rb index 8f0017fdba9..497802c7159 100644 --- a/lib/cloud_controller/background_job_environment.rb +++ b/lib/cloud_controller/background_job_environment.rb @@ -1,4 +1,8 @@ +require 'socket' + class BackgroundJobEnvironment + attr_reader :readiness_server + def initialize(config) @config = config @log_counter = Steno::Sink::Counter.new @@ -16,7 +20,7 @@ def setup_environment(readiness_port=nil) @config.configure_components if readiness_port && readiness_port > 0 - open_readiness_port(readiness_port) + listen_on_readiness_port(readiness_port) end yield if block_given? @@ -24,13 +28,18 @@ def setup_environment(readiness_port=nil) private - def open_readiness_port(port) - # rubocop:disable Style/GlobalVars - $socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM) - sockaddr = Socket.pack_sockaddr_in(port, '127.0.0.1') - $socket.bind(sockaddr) - - $socket.listen(READINESS_SOCKET_QUEUE_DEPTH) - # rubocop:enable Style/GlobalVars + def listen_on_readiness_port(port) + @readiness_server = TCPServer.open('0.0.0.0', port) + + Thread.new do + loop do + Thread.start(@readiness_server.accept) do |c| + c.puts 'ok' + c.close + end + end + rescue Errno::EBADF + Thread.exit + end end end diff --git a/spec/unit/lib/background_job_environment_spec.rb b/spec/unit/lib/background_job_environment_spec.rb index 6405a20edcf..e588078a49d 100644 --- a/spec/unit/lib/background_job_environment_spec.rb +++ b/spec/unit/lib/background_job_environment_spec.rb @@ -16,7 +16,6 @@ describe '#setup_environment' do before do allow(VCAP::CloudController::DB).to receive(:load_models) - allow(Thread).to receive(:new).and_yield allow(EM).to receive(:run).and_yield allow(VCAP::CloudController::ResourcePool).to receive(:new) end @@ -55,7 +54,7 @@ def open_port_count expect { background_job_environment.setup_environment(9999) }.to change { open_port_count }.by(1) - expect { TCPSocket.new('127.0.0.1', 9999).close }.not_to raise_error + expect { background_job_environment.readiness_server.close }.not_to raise_error end end end