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 9 commits into
base: development
Choose a base branch
from
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
Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
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)
Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
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