terminal-shop 0.1.0.pre.alpha.9 → 0.1.0.pre.alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e49f5a72c701e7a5a68d1b537d9910a95d385966dcab6788a080c0d7cfefc503
|
4
|
+
data.tar.gz: c94a21a6fc772735aedd857c5f592b5ab0eb7344419814d593f7bec71b96b5e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b5237266009ebbfb3019971b882c5c92076931ee011fbad8023c8ac806a89bd58eeb1373aa38ee60374d8af66913bacd8f00afd5f809bec5e682cabdb70dced
|
7
|
+
data.tar.gz: 882c016a8b85b9ce829fb952eb63cccf295595f64c500863a8550243946e5164b0308af9699f618cfd768a812831d4d2953af97a1ef12c7e3dc3bc9ab0f5c8b8
|
@@ -12,29 +12,58 @@ module TerminalShop
|
|
12
12
|
# @private
|
13
13
|
#
|
14
14
|
# @param url [URL::Generic]
|
15
|
-
# @param
|
15
|
+
# @param blk [Proc]
|
16
16
|
#
|
17
17
|
# @return [ConnectionPool]
|
18
18
|
#
|
19
|
-
private def
|
19
|
+
private def with_pool(url, &blk)
|
20
20
|
origin = TerminalShop::Util.uri_origin(url)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
21
|
+
key = :"#{self.class.name}-connection_in_use_for_#{origin}"
|
22
|
+
|
23
|
+
return blk.call(make_conn(url)) if Thread.current[key]
|
24
|
+
|
25
|
+
pool =
|
26
|
+
@mutex.synchronize do
|
27
|
+
@pools[origin] ||= ConnectionPool.new(size: Etc.nprocessors) do
|
28
|
+
make_conn(url)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
pool.with do |conn|
|
33
|
+
Thread.current[key] = true
|
34
|
+
|
35
|
+
blk.call(conn)
|
36
|
+
# rubocop:disable Lint/RescueException
|
37
|
+
rescue Exception => e
|
38
|
+
# rubocop:enable Lint/RescueException
|
39
|
+
# should close connection on all errors to ensure no invalid state persists
|
40
|
+
conn.finish if conn.started?
|
41
|
+
raise e
|
42
|
+
ensure
|
43
|
+
Thread.current[key] = nil
|
44
|
+
end
|
45
|
+
end
|
32
46
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
47
|
+
# @private
|
48
|
+
#
|
49
|
+
# @param url [URI::Generic]
|
50
|
+
#
|
51
|
+
# @return [Net::HTTP]
|
52
|
+
#
|
53
|
+
private def make_conn(url)
|
54
|
+
port =
|
55
|
+
case [url.port, url.scheme]
|
56
|
+
in [Integer, _]
|
57
|
+
url.port
|
58
|
+
in [nil, "http" | "ws"]
|
59
|
+
Net::HTTP.http_default_port
|
60
|
+
in [nil, "https" | "wss"]
|
61
|
+
Net::HTTP.https_default_port
|
37
62
|
end
|
63
|
+
|
64
|
+
Net::HTTP.new(url.host, port).tap do
|
65
|
+
_1.use_ssl = %w[https wss].include?(url.scheme)
|
66
|
+
_1.max_retries = 0
|
38
67
|
end
|
39
68
|
end
|
40
69
|
|
@@ -44,7 +73,7 @@ module TerminalShop
|
|
44
73
|
# @option req [Symbol] :method
|
45
74
|
# @option req [URI::Generic] :url
|
46
75
|
# @option req [Hash{String => String}] :headers
|
47
|
-
# @option req [String, Hash] :body
|
76
|
+
# @option req [String, Hash, IO, StringIO] :body
|
48
77
|
# @option req [Float] :timeout
|
49
78
|
#
|
50
79
|
# @return [Net::HTTPResponse]
|
@@ -55,7 +84,7 @@ module TerminalShop
|
|
55
84
|
request = Net::HTTPGenericRequest.new(
|
56
85
|
method.to_s.upcase,
|
57
86
|
!body.nil?,
|
58
|
-
|
87
|
+
method != :head,
|
59
88
|
url.to_s
|
60
89
|
)
|
61
90
|
|
@@ -67,28 +96,30 @@ module TerminalShop
|
|
67
96
|
request.body_stream = body
|
68
97
|
end
|
69
98
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
99
|
+
with_pool(url) do |conn|
|
100
|
+
make_request(conn, request, timeout)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# @private
|
105
|
+
#
|
106
|
+
# @param conn [Net::HTTP]
|
107
|
+
# @param request [Net::HTTPGenericRequest]
|
108
|
+
# @param timeout [Float]
|
109
|
+
#
|
110
|
+
# @return [Net::HTTPResponse]
|
111
|
+
#
|
112
|
+
private def make_request(conn, request, timeout)
|
113
|
+
unless conn.started?
|
75
114
|
conn.open_timeout = timeout
|
76
|
-
conn.
|
77
|
-
|
78
|
-
conn.continue_timeout = timeout
|
115
|
+
conn.start
|
116
|
+
end
|
79
117
|
|
80
|
-
|
118
|
+
conn.read_timeout = timeout
|
119
|
+
conn.write_timeout = timeout
|
120
|
+
conn.continue_timeout = timeout
|
81
121
|
|
82
|
-
|
83
|
-
# rubocop:disable Lint/RescueException
|
84
|
-
rescue Exception => e
|
85
|
-
# rubocop:enable Lint/RescueException
|
86
|
-
# should close connection on all errors to ensure no invalid state persists
|
87
|
-
conn.finish if conn.started?
|
88
|
-
raise e
|
89
|
-
end
|
90
|
-
rescue ConnectionPool::TimeoutError
|
91
|
-
raise TerminalShop::APITimeoutError.new(url: url)
|
122
|
+
conn.request(request)
|
92
123
|
end
|
93
124
|
end
|
94
125
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: terminal-shop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.pre.alpha.
|
4
|
+
version: 0.1.0.pre.alpha.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Terminal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: connection_pool
|