Skip to content

Commit 50df978

Browse files
committed
Use persistent internet connection.
Use a thread local to store the internet client. This ensures connections persist between requests. In addition, improve the logging so that these improvements are more visible.
1 parent 036161c commit 50df978

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

rack-async-http-falcon-graphql-lazy-resolve/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ source "https://rubygems.org"
22

33
gem "rack"
44
gem "falcon"
5+
gem "thread-local"
56
gem "async-http"
67
gem "graphql"

rack-async-http-falcon-graphql-lazy-resolve/Gemfile.lock

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
async (1.28.7)
4+
async (1.29.0)
55
console (~> 1.10)
66
nio4r (~> 2.3)
77
timers (~> 4.1)
@@ -19,12 +19,12 @@ GEM
1919
async-http (~> 0.53)
2020
async-io (1.30.2)
2121
async (~> 1.14)
22-
async-pool (0.3.4)
22+
async-pool (0.3.5)
2323
async (~> 1.25)
2424
build-environment (1.13.0)
25-
console (1.10.1)
25+
console (1.11.1)
2626
fiber-local
27-
falcon (0.37.2)
27+
falcon (0.37.3)
2828
async (~> 1.13)
2929
async-container (~> 0.16.0)
3030
async-http (~> 0.54.0)
@@ -37,17 +37,13 @@ GEM
3737
rack (>= 1.0)
3838
samovar (~> 2.1)
3939
fiber-local (1.0.0)
40-
graphql (1.12.4)
41-
graphql-batch (0.4.3)
42-
graphql (>= 1.3, < 2)
43-
promise.rb (~> 0.7.2)
44-
localhost (1.1.6)
40+
graphql (1.12.7)
41+
localhost (1.1.7)
4542
mapping (1.1.1)
46-
nio4r (2.5.5)
43+
nio4r (2.5.7)
4744
process-metrics (0.2.1)
4845
console (~> 1.8)
4946
samovar (~> 2.1)
50-
promise.rb (0.7.4)
5147
protocol-hpack (1.4.2)
5248
protocol-http (0.21.0)
5349
protocol-http1 (0.13.2)
@@ -59,7 +55,8 @@ GEM
5955
samovar (2.1.4)
6056
console (~> 1.0)
6157
mapping (~> 1.0)
62-
timers (4.3.2)
58+
thread-local (1.1.0)
59+
timers (4.3.3)
6360

6461
PLATFORMS
6562
ruby
@@ -68,11 +65,8 @@ DEPENDENCIES
6865
async-http
6966
falcon
7067
graphql
71-
graphql-batch
7268
rack
73-
74-
RUBY VERSION
75-
ruby 2.7.2p137
69+
thread-local
7670

7771
BUNDLED WITH
78-
2.1.4
72+
2.2.5
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
require_relative "schema"
22

3+
require 'securerandom'
4+
35
class App
46
def self.call(env)
5-
puts "Request start"
6-
7-
result = Schema.execute("query { one two three }")
7+
logger = Console.logger.with(name: SecureRandom.uuid)
88

9-
puts "Request finish"
9+
Async(logger: logger) do
10+
result = Console.logger.measure(self, "Schema.execute") do
11+
Schema.execute("query { one two three }")
12+
end
1013

11-
[200, {}, [result.to_json]]
14+
[200, {}, [result.to_json]]
15+
end.wait
1216
end
1317
end

rack-async-http-falcon-graphql-lazy-resolve/query.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "async/http/internet"
2+
require "thread/local"
23

34
class Query < GraphQL::Schema::Object
45
field :one, String, null: false
@@ -17,8 +18,16 @@ def three
1718
Async { delay_2_data["url"] }
1819
end
1920

21+
module Internet
22+
extend Thread::Local
23+
24+
def self.local
25+
Async::HTTP::Internet.new
26+
end
27+
end
28+
2029
def internet
21-
@_internet ||= Async::HTTP::Internet.new
30+
Internet.instance
2231
end
2332

2433
def delay_1_semaphore
@@ -32,21 +41,19 @@ def delay_2_semaphore
3241
def delay_1_data
3342
delay_1_semaphore.async do |task|
3443
@_delay_1_data ||= begin
35-
puts "-> delay_1_data"
36-
data = JSON.parse(internet.get("https://httpbin.org/delay/1").read)
37-
puts "<- delay_1_data"
38-
data
44+
Console.logger.measure(self, "delay_1_data") do
45+
JSON.parse(internet.get("https://httpbin.org/delay/1").read)
46+
end
3947
end
4048
end.result
4149
end
4250

4351
def delay_2_data
4452
delay_2_semaphore.async do |task|
4553
@_delay_2_data ||= begin
46-
puts "-> delay_2_data"
47-
data = JSON.parse(internet.get("https://httpbin.org/delay/2").read)
48-
puts "<- delay_2_data"
49-
data
54+
Console.logger.measure(self, "delay_2_data") do
55+
JSON.parse(internet.get("https://httpbin.org/delay/2").read)
56+
end
5057
end
5158
end.result
5259
end

0 commit comments

Comments
 (0)