1
1
defmodule ComponentsGuide.Fetch do
2
- alias ComponentsGuide.Fetch . { Request , Response }
2
+ alias ComponentsGuide.Fetch . { Request , Response , Timings }
3
3
4
4
@ timeout 5000
5
5
@@ -10,45 +10,6 @@ defmodule ComponentsGuide.Fetch do
10
10
get_following_redirects! ( url_string )
11
11
end
12
12
13
- defmodule Timings do
14
- defstruct [ :duration , :start ]
15
-
16
- def start ( ) do
17
- % __MODULE__ {
18
- start: System . monotonic_time ( )
19
- }
20
- end
21
-
22
- def finish ( timings = % __MODULE__ { start: start } ) do
23
- duration = System . monotonic_time ( ) - start
24
- put_in ( timings . duration , duration )
25
- end
26
-
27
- def start_with_telemetry ( event_name , metadata \\ % { } ) do
28
- t = start ( )
29
-
30
- :telemetry . execute (
31
- event_name ,
32
- % { start: t . start } ,
33
- metadata
34
- )
35
-
36
- t
37
- end
38
-
39
- def finish_with_telemetry ( t = % __MODULE__ { } , event_name , metadata \\ % { } ) do
40
- t = finish ( t )
41
-
42
- :telemetry . execute (
43
- event_name ,
44
- % { duration: t . duration } ,
45
- metadata
46
- )
47
-
48
- t
49
- end
50
- end
51
-
52
13
def get_following_redirects! ( url_string ) when is_binary ( url_string ) do
53
14
response =
54
15
case Request . new ( url_string ) do
@@ -88,18 +49,14 @@ defmodule ComponentsGuide.Fetch do
88
49
t = Timings . start_with_telemetry ( [ :fetch , :load! , :start ] , % { req: req } )
89
50
90
51
{ :ok , conn } = Mint.HTTP . connect ( :https , host , 443 , mode: :passive , protocols: protocols )
91
- { conn , response } = do_request ( conn , req )
52
+ t = t |> Timings . did_connect ( )
53
+ { conn , response } = do_request ( conn , req , t )
92
54
Mint.HTTP . close ( conn )
93
55
94
- t =
95
- Timings . finish_with_telemetry ( t , [ :fetch , :load! , :done ] , % {
96
- req: req
97
- } )
98
-
99
- response = Response . add_timings ( response , t )
56
+ response = Response . finish_timings ( response , [ :fetch , :load! , :done ] , % { req: req } )
100
57
101
58
IO . puts (
102
- "Loaded #{ req . url_string } in #{ System . convert_time_unit ( t . duration , :native , :millisecond ) } ms. #{ inspect ( response . done? ) } "
59
+ "Loaded #{ req . url_string } in #{ System . convert_time_unit ( response . timings . duration , :native , :millisecond ) } ms. #{ inspect ( response . done? ) } "
103
60
)
104
61
105
62
response
@@ -116,20 +73,16 @@ defmodule ComponentsGuide.Fetch do
116
73
t = Timings . start_with_telemetry ( [ :fetch , :load_many! , :start ] , % { host: host } )
117
74
118
75
{ :ok , conn } = Mint.HTTP . connect ( :https , host , 443 , mode: :passive , protocols: [ :http1 ] )
76
+ t = t |> Timings . did_connect ( )
119
77
120
78
{ conn , results } =
121
79
Enum . reduce ( reqs , { conn , [ ] } , fn
122
80
% Request { uri: % URI { host: ^ host , port: 443 } } = req , { conn , results } ->
123
81
t = Timings . start_with_telemetry ( [ :fetch , :load_many! , :request , :start ] , % { req: req } )
124
82
125
- { conn , response } = do_request ( conn , req )
126
-
127
- t =
128
- Timings . finish_with_telemetry ( t , [ :fetch , :load_many! , :request , :done ] , % {
129
- req: req
130
- } )
83
+ { conn , response } = do_request ( conn , req , t )
131
84
132
- response = Response . add_timings ( response , t )
85
+ response = Response . finish_timings ( response , [ :fetch , :load_many! , :request , :done ] , % { req: req } )
133
86
134
87
{ conn , [ response | results ] }
135
88
end )
@@ -162,9 +115,10 @@ defmodule ComponentsGuide.Fetch do
162
115
headers: headers ,
163
116
body: body ,
164
117
url_string: url_string
165
- }
118
+ } ,
119
+ timings = % Timings { }
166
120
) do
167
- result = Response . new ( url_string )
121
+ result = Response . new ( url_string , timings )
168
122
path_and_query = % URI { path: uri . path || "/" , query: uri . query } |> URI . to_string ( )
169
123
170
124
case Mint.HTTP . request ( conn , method , path_and_query , headers , body ) do
0 commit comments