Skip to content

Commit

Permalink
Merge pull request #3813 from sap-contributions/improve-routing-info-…
Browse files Browse the repository at this point in the history
…collection

Reduce queries to DB when collecting routing info
  • Loading branch information
svkrieger authored May 31, 2024
2 parents 804cbb9 + 0dcba6f commit 8e3a8f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/cloud_controller/diego/protocol/routing_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ def initialize(process)
end

def routing_info
http_info_obj = http_info
tcp_info_obj = tcp_info
process_eager = ProcessModel.eager(route_mappings: { route: %i[domain route_binding] }).where(id: process.id).all

return {} if process_eager.empty?

http_info_obj = http_info(process_eager)
tcp_info_obj = tcp_info(process_eager)

route_info = {}
route_info['http_routes'] = http_info_obj if http_info_obj.present?
route_info['tcp_routes'] = tcp_info_obj if tcp_info_obj.present?
route_info['internal_routes'] = internal_routes
route_info['internal_routes'] = internal_routes(process_eager)
route_info
rescue RoutingApi::RoutingApiDisabled
raise CloudController::Errors::ApiError.new_from_details('RoutingApiDisabled')
Expand All @@ -27,8 +31,8 @@ def routing_info

private

def http_info
route_mappings = process.route_mappings.reject do |route_mapping|
def http_info(process_eager)
route_mappings = process_eager[0].route_mappings.reject do |route_mapping|
route_mapping.route.internal? || route_mapping.route.tcp?
end

Expand All @@ -43,8 +47,8 @@ def http_info
end
end

def tcp_info
route_mappings = process.route_mappings.select do |route_mapping|
def tcp_info(process_eager)
route_mappings = process_eager[0].route_mappings.select do |route_mapping|
r = route_mapping.route
r.tcp? && !r.internal?
end
Expand All @@ -58,8 +62,13 @@ def tcp_info
end
end

def internal_routes
process.routes.select(&:internal?).map do |r|
def internal_routes(process_eager)
route_mappings = process_eager[0].route_mappings.select do |route_mapping|
route_mapping.route.internal?
end

route_mappings.map do |route_mapping|
r = route_mapping.route
{ 'hostname' => "#{r.host}.#{r.domain.name}" }
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/lib/cloud_controller/diego/protocol/routing_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ class Protocol
end
end
end

context 'when process disappeared' do
before do
process.destroy
end

it 'return an empty object' do
expect(ri).to be_empty
end
end
end
end
end
Expand Down

0 comments on commit 8e3a8f2

Please sign in to comment.