Skip to content

Use persistent internet connection. #6

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

Merged
merged 1 commit into from
Apr 11, 2021
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
1 change: 1 addition & 0 deletions rack-async-http-falcon-graphql-lazy-resolve/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ source "https://rubygems.org"

gem "rack"
gem "falcon"
gem "thread-local"
gem "async-http"
gem "graphql"
28 changes: 11 additions & 17 deletions rack-async-http-falcon-graphql-lazy-resolve/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
14 changes: 9 additions & 5 deletions rack-async-http-falcon-graphql-lazy-resolve/app.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 16 additions & 9 deletions rack-async-http-falcon-graphql-lazy-resolve/query.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "async/http/internet"
require "thread/local"

class Query < GraphQL::Schema::Object
field :one, String, null: false
Expand All @@ -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
Expand All @@ -32,21 +41,19 @@ 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

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
Expand Down