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: in parallel calls save main thread states across threads #19

Merged
merged 1 commit into from
Sep 9, 2024
Merged
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
19 changes: 11 additions & 8 deletions lib/ontologies_api_client/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def config(&block)
def config_connection(options = {})
return if @settings_run_connection
store = options[:cache_store] || ActiveSupport::Cache::MemoryStore.new
@settings.conn = faraday_connection(@settings.rest_url, @settings.apikey, store)
@settings.conn = faraday_connection(@settings.rest_url, @settings.apikey, store, current_portal: true)
@settings.federated_conn = @settings.federated_portals.map do |portal_name, portal_info|
[portal_name, faraday_connection(portal_info[:api], portal_info[:apikey], store)]
end.to_h
Expand All @@ -51,21 +51,24 @@ def connection_configured?
end

private
def faraday_connection(url, apikey, store)
def faraday_connection(url, apikey, store, current_portal: false)
Faraday.new(url.to_s.chomp('/')) do |faraday|

if @settings.enable_long_request_log
require_relative 'middleware/faraday-long-requests'
faraday.use :long_requests
end

require_relative 'middleware/faraday-user-apikey'
faraday.use :user_apikey
if current_portal
require_relative 'middleware/faraday-user-apikey'
faraday.use :user_apikey

require_relative 'middleware/faraday-slices'
faraday.use :ncbo_slices
require_relative 'middleware/faraday-slices'
faraday.use :ncbo_slices

require_relative 'middleware/faraday-last-updated'
faraday.use :last_updated
require_relative 'middleware/faraday-last-updated'
faraday.use :last_updated
end

if @settings.cache
begin
Expand Down
4 changes: 3 additions & 1 deletion lib/ontologies_api_client/request_federation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ def self.included(base)
module ClassMethods
def federated_get(params = {}, &link)
portals = request_portals(params)
main_thread_locals = Thread.current.keys.map { |key| [key, Thread.current[key]] }.to_h

connections = Parallel.map(portals, in_threads: portals.size) do |conn|
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 StandardError => e
rescue Exception => e
[OpenStruct.new(errors: "Problem retrieving #{link.call(conn.url_prefix.to_s.chomp('/')) || conn.url_prefix}")]
end
end
Expand Down
Loading