Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: federation requests timeout #24

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ontologies_api_client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def self.get(path, params = {}, options = {})
response = connection.get do |req|
req.url path
req.params = params.dup
req.options[:timeout] = 60
req.options[:timeout] = 20
req.headers.merge(headers)
req.headers[:invalidate_cache] = invalidate_cache
end
Expand Down
20 changes: 20 additions & 0 deletions lib/ontologies_api_client/request_federation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ def federated_get(params = {}, &link)
main_thread_locals = Thread.current.keys.map { |key| [key, Thread.current[key]] }.to_h

connections = Parallel.map(portals, in_threads: portals.size) do |conn|
portal_name = portal_name_from_id(conn.url_prefix.to_s)
portal_status = true

unless Rails.cache.read("federation_portal_up_#{portal_name}").nil?
portal_status = Rails.cache.read("federation_portal_up_#{portal_name}")
end

unless portal_status

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do it inline here like this

next [OpenStruct.new(errors: "Problem retrieving #{portal_name}")] unless portal_status

next [OpenStruct.new(errors: "Problem retrieving #{portal_name}")]
end

main_thread_locals.each { |key, value| Thread.current[key] = value }
begin
HTTP.get(link.call(conn.url_prefix.to_s.chomp('/')), params, connection: conn)
rescue Exception => e
Rails.cache.write("federation_portal_up_#{portal_name}", false, expires_in: 10.minutes)
[OpenStruct.new(errors: "Problem retrieving #{link.call(conn.url_prefix.to_s.chomp('/')) || conn.url_prefix}")]
end
end
Expand All @@ -42,6 +54,14 @@ def request_portals(params = {})

portals
end


def portal_name_from_id(id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be refactured to this

def portal_name_from_id(id)
  LinkedData::Client::HTTP.federated_conn.find { |_, value| value.url_prefix.to_s.eql?(id) }&.first || ''
end 

LinkedData::Client::HTTP.federated_conn.each do |portal_name, value|
return portal_name if value.url_prefix.to_s.eql?(id)
end
return ''
end
end

end
Expand Down
Loading