diff --git a/rack-async-http-falcon-graphql-lazy-resolve/Gemfile b/rack-async-http-falcon-graphql-lazy-resolve/Gemfile index 32525cb..573d5a5 100644 --- a/rack-async-http-falcon-graphql-lazy-resolve/Gemfile +++ b/rack-async-http-falcon-graphql-lazy-resolve/Gemfile @@ -2,5 +2,6 @@ source "https://rubygems.org" gem "rack" gem "falcon" +gem "thread-local" gem "async-http" gem "graphql" diff --git a/rack-async-http-falcon-graphql-lazy-resolve/Gemfile.lock b/rack-async-http-falcon-graphql-lazy-resolve/Gemfile.lock index eb211e5..d970cc3 100644 --- a/rack-async-http-falcon-graphql-lazy-resolve/Gemfile.lock +++ b/rack-async-http-falcon-graphql-lazy-resolve/Gemfile.lock @@ -1,7 +1,7 @@ GEM remote: https://rubygems.org/ specs: - async (1.28.7) + async (1.29.0) console (~> 1.10) nio4r (~> 2.3) timers (~> 4.1) @@ -19,12 +19,12 @@ GEM async-http (~> 0.53) async-io (1.30.2) async (~> 1.14) - async-pool (0.3.4) + async-pool (0.3.5) async (~> 1.25) build-environment (1.13.0) - console (1.10.1) + console (1.11.1) fiber-local - falcon (0.37.2) + falcon (0.37.3) async (~> 1.13) async-container (~> 0.16.0) async-http (~> 0.54.0) @@ -37,17 +37,13 @@ GEM rack (>= 1.0) samovar (~> 2.1) fiber-local (1.0.0) - graphql (1.12.4) - graphql-batch (0.4.3) - graphql (>= 1.3, < 2) - promise.rb (~> 0.7.2) - localhost (1.1.6) + graphql (1.12.7) + localhost (1.1.7) mapping (1.1.1) - nio4r (2.5.5) + nio4r (2.5.7) process-metrics (0.2.1) console (~> 1.8) samovar (~> 2.1) - promise.rb (0.7.4) protocol-hpack (1.4.2) protocol-http (0.21.0) protocol-http1 (0.13.2) @@ -59,7 +55,8 @@ GEM samovar (2.1.4) console (~> 1.0) mapping (~> 1.0) - timers (4.3.2) + thread-local (1.1.0) + timers (4.3.3) PLATFORMS ruby @@ -68,11 +65,8 @@ DEPENDENCIES async-http falcon graphql - graphql-batch rack - -RUBY VERSION - ruby 2.7.2p137 + thread-local BUNDLED WITH - 2.1.4 + 2.2.5 diff --git a/rack-async-http-falcon-graphql-lazy-resolve/app.rb b/rack-async-http-falcon-graphql-lazy-resolve/app.rb index 34a9104..a0b5393 100644 --- a/rack-async-http-falcon-graphql-lazy-resolve/app.rb +++ b/rack-async-http-falcon-graphql-lazy-resolve/app.rb @@ -1,13 +1,17 @@ require_relative "schema" +require 'securerandom' + class App def self.call(env) - puts "Request start" - - result = Schema.execute("query { one two three }") + logger = Console.logger.with(name: SecureRandom.uuid) - puts "Request finish" + Async(logger: logger) do + result = Console.logger.measure(self, "Schema.execute") do + Schema.execute("query { one two three }") + end - [200, {}, [result.to_json]] + [200, {}, [result.to_json]] + end.wait end end diff --git a/rack-async-http-falcon-graphql-lazy-resolve/query.rb b/rack-async-http-falcon-graphql-lazy-resolve/query.rb index 4751a35..1e6e57b 100644 --- a/rack-async-http-falcon-graphql-lazy-resolve/query.rb +++ b/rack-async-http-falcon-graphql-lazy-resolve/query.rb @@ -1,4 +1,5 @@ require "async/http/internet" +require "thread/local" class Query < GraphQL::Schema::Object field :one, String, null: false @@ -17,8 +18,16 @@ def three Async { delay_2_data["url"] } end + module Internet + extend Thread::Local + + def self.local + Async::HTTP::Internet.new + end + end + def internet - @_internet ||= Async::HTTP::Internet.new + Internet.instance end def delay_1_semaphore @@ -32,10 +41,9 @@ def delay_2_semaphore def delay_1_data delay_1_semaphore.async do |task| @_delay_1_data ||= begin - puts "-> delay_1_data" - data = JSON.parse(internet.get("https://httpbin.org/delay/1").read) - puts "<- delay_1_data" - data + Console.logger.measure(self, "delay_1_data") do + JSON.parse(internet.get("https://httpbin.org/delay/1").read) + end end end.result end @@ -43,10 +51,9 @@ def delay_1_data def delay_2_data delay_2_semaphore.async do |task| @_delay_2_data ||= begin - puts "-> delay_2_data" - data = JSON.parse(internet.get("https://httpbin.org/delay/2").read) - puts "<- delay_2_data" - data + Console.logger.measure(self, "delay_2_data") do + JSON.parse(internet.get("https://httpbin.org/delay/2").read) + end end end.result end