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: a7302bc4cca8faceca8577dce4b9b10362963e1d51e24657389a8c663376299e
4
- data.tar.gz: 2fd2e9b55fc3c9ba9f4038a44e6bf71e18a49d1d4186a44031ffbe2e43be1fe7
3
+ metadata.gz: e49f5a72c701e7a5a68d1b537d9910a95d385966dcab6788a080c0d7cfefc503
4
+ data.tar.gz: c94a21a6fc772735aedd857c5f592b5ab0eb7344419814d593f7bec71b96b5e5
5
5
  SHA512:
6
- metadata.gz: 0f8be58ee44eddefe127293b9423358e625982a33a00d83076f23acfcb5845bb27633e0ee6820fe1b7c7915bfdad29a9b40db3a971cf74490082e5cc0573189d
7
- data.tar.gz: 6bff5193dcd2072b584d1a6ff52d4bf7173c66ff6d2568665fcd1c1a7a73e536718fd90c4f393574c579f8a368afeb547ff83bb2475493c25de5917a4d753280
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 timeout [Float]
15
+ # @param blk [Proc]
16
16
  #
17
17
  # @return [ConnectionPool]
18
18
  #
19
- private def get_pool(url)
19
+ private def with_pool(url, &blk)
20
20
  origin = TerminalShop::Util.uri_origin(url)
21
- @mutex.synchronize do
22
- @pools[origin] ||= ConnectionPool.new(size: Etc.nprocessors) do
23
- port =
24
- case [url.port, url.scheme]
25
- in [Integer, _]
26
- url.port
27
- in [nil, "http" | "ws"]
28
- Net::HTTP.http_default_port
29
- in [nil, "https" | "wss"]
30
- Net::HTTP.https_default_port
31
- end
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
- session = Net::HTTP.new(url.host, port)
34
- session.use_ssl = %w[https wss].include?(url.scheme)
35
- session.max_retries = 0
36
- session
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
- ![:head, :options].include?(method),
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
- # This timeout is for acquiring a connection from the pool
71
- # The default 5 seconds seems too short, lets just have a nearly unbounded queue for now
72
- #
73
- # TODO: revisit this around granular timeout / concurrency control
74
- get_pool(url).with(timeout: 600) do |conn|
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.read_timeout = timeout
77
- conn.write_timeout = timeout
78
- conn.continue_timeout = timeout
115
+ conn.start
116
+ end
79
117
 
80
- conn.start unless conn.started?
118
+ conn.read_timeout = timeout
119
+ conn.write_timeout = timeout
120
+ conn.continue_timeout = timeout
81
121
 
82
- conn.request(request)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TerminalShop
4
- VERSION = "0.1.0-alpha.9"
4
+ VERSION = "0.1.0-alpha.10"
5
5
  end
@@ -1,5 +1,5 @@
1
1
  # typed: strong
2
2
 
3
3
  module TerminalShop
4
- VERSION = "0.1.0-alpha.9"
4
+ VERSION = "0.1.0-alpha.10"
5
5
  end
@@ -1,3 +1,3 @@
1
1
  module TerminalShop
2
- VERSION: "0.1.0-alpha.8"
2
+ VERSION: "0.1.0-alpha.9"
3
3
  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.9
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-17 00:00:00.000000000 Z
11
+ date: 2025-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool