From 7694ee4f8a1230da315c88db01ff65506f22a9bd Mon Sep 17 00:00:00 2001 From: Matt Fenelon Date: Fri, 10 May 2024 09:45:53 +0100 Subject: [PATCH] Add adapter close on the same thread Rails assigns a connection to a thread so the close call has to happen on that same thread for the thread's connection to be closed. --- lib/graphiti/scope.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/graphiti/scope.rb b/lib/graphiti/scope.rb index 9afe1c71..5224d248 100644 --- a/lib/graphiti/scope.rb +++ b/lib/graphiti/scope.rb @@ -55,6 +55,8 @@ def resolve def resolve_sideloads(results) return if results == [] + concurrent = Graphiti.config.concurrency + promises = @query.sideloads.filter_map do |name, q| sideload = @resource.class.sideload(name) next if sideload.nil? || sideload.shared_remote? @@ -66,11 +68,9 @@ def resolve_sideloads(results) Graphiti.config.before_sideload&.call(graphiti_context) Graphiti.context = graphiti_context results = sideload.resolve(results, q, parent_resource) + @resource.adapter.close if concurrent if results.is_a?(Concurrent::Promises::Future) - results.then_on(self.class.global_thread_pool_executor) do - # TODO: will this always run? - @resource.adapter.close - end + results else Concurrent::Promises.fulfilled_future(results, self.class.global_thread_pool_executor) end