azure-blob 0.9.0 → 0.10.0

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: 1e0ce3229ce4281799f9d78b8f5b95343ac3fced270f902f07215630568bec93
4
- data.tar.gz: ce76f13f260cdc59684ccfe0f655730c6fa03859bfa98264678d111b49999cfb
3
+ metadata.gz: 803bd81be45a0ba597d53d7967864d4bc79606e239fc29e85b2c4f31b21e3b34
4
+ data.tar.gz: 99f58e80bac3ce0c670ef8ccc86b7673a40c22711ebb77bbf34196ce84c55fa5
5
5
  SHA512:
6
- metadata.gz: d663e5fb344c55fc2a8efb9247f6207866c9f5676e9e64e028a1bde2c6762fa5bc5dd5ecf9864b41a44176eb7d7782524424a4dc29df1bf3bf716abb47e56d2b
7
- data.tar.gz: 58663d45654f161db667d2bc5def5a36c7d5202038cf107322805b7f15b12cc03a7b455b058ee11385233a3c1dff70037ffe43118a848e2427e14e177d84920f
6
+ metadata.gz: a4aa69a0ee49c7a5c0d11122ec7519c9d947e04739d34a27f741ca573c9c3867acd9bb05a34c31cec8e560a703e86ff1ab5427eeefa48c05d9a4d1e6decadd56
7
+ data.tar.gz: 65159fa66b8649f0dba35b8a3d5d7ad3d76f32b65f03128fe100c6df680d03288333b28ae50712e76bcdb19a872a85fe19fdaff23923d8dbc909f603980ee6ed
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.10.0] 2026-11-22
4
+
5
+ - Add persistent connections: HTTP connections are kept open between requests when the `net-http-persistent` gem is loaded (#53)
6
+ - Add optional `connection_pool` to `AzureBlob::Client.new` and the ActiveStorage config to send requests through a custom connection pool
7
+
3
8
  ## [0.9.0] 2026-10-11
4
9
 
5
10
  - Drop support for Ruby 3.1 and 3.2. Ruby 3.3 or newer is now required
data/README.md CHANGED
@@ -152,6 +152,73 @@ To enable lazy loading and ignore missing credentials, set the `lazy` option:
152
152
 
153
153
  or add `lazy: true` to your `config/storage.yml` for Active Storage.
154
154
 
155
+ ### Persistent connections
156
+
157
+ By default, every request opens a new connection to Azure and closes it afterward. Persistent connections stay open and are reused, which saves a TCP and TLS handshake on every request.
158
+
159
+ This relies on the [net-http-persistent](https://github.com/drbrain/net-http-persistent) gem. If you want to activate persistent connections, add it to your Gemfile:
160
+
161
+ ```ruby
162
+ gem "net-http-persistent"
163
+ ```
164
+
165
+ Rails requires it automatically. To use it outside of Rails, set the `persistent_connections` option to `true` on the client:
166
+
167
+ ```ruby
168
+ client = AzureBlob::Client.new(
169
+ account_name: "account_name",
170
+ access_key: "SECRET_KEY",
171
+ container: "container_name",
172
+ persistent_connections: true,
173
+ )
174
+ ```
175
+
176
+ If your app uses net-http-persistent but you do not want azure-blob to use it, set `persistent_connections: false` on the client or in your `config/storage.yml`.
177
+
178
+ #### Custom connection pool
179
+
180
+ To tune the connections (pool size, timeouts, etc.), build your own pool and pass it as `connection_pool`:
181
+
182
+ ```ruby
183
+ connection_pool = Net::HTTP::Persistent.new(name: "azure-blob", pool_size: 10)
184
+ connection_pool.idle_timeout = 30
185
+
186
+ client = AzureBlob::Client.new(
187
+ account_name: "account_name",
188
+ access_key: "SECRET_KEY",
189
+ container: "container_name",
190
+ connection_pool: connection_pool,
191
+ )
192
+ ```
193
+
194
+ `connection_pool` also takes a class, or the name of a class, and calls `new` on it. This is how to set a pool from `config/storage.yml`:
195
+
196
+ ```ruby
197
+ class BlobConnectionPool < Net::HTTP::Persistent
198
+ def initialize
199
+ super(name: "azure-blob", pool_size: 10)
200
+ self.idle_timeout = 30
201
+ end
202
+ end
203
+ ```
204
+
205
+ ```yaml
206
+ microsoft:
207
+ service: AzureBlob
208
+ storage_account_name: account_name
209
+ storage_access_key: SECRET_KEY
210
+ container: container_name
211
+ connection_pool: BlobConnectionPool
212
+ ```
213
+
214
+ A pool does not have to be a `Net::HTTP::Persistent`. It can be any object that:
215
+
216
+ - responds to `request(uri, request)`, where `uri` is a `URI` and `request` a `Net::HTTPRequest`
217
+ - returns the `Net::HTTPResponse`
218
+ - is safe to call from several threads at once
219
+
220
+ A `connection_pool` is used whatever `persistent_connections` is set to.
221
+
155
222
 
156
223
  ## Contributing
157
224
 
@@ -25,6 +25,7 @@ module AzureBlob
25
25
  @principal_id = principal_id
26
26
  @use_managed_identities = options[:use_managed_identities]
27
27
  @delegation_key_expiration = options[:delegation_key_expiration]
28
+ @connection_pool = build_connection_pool(options[:connection_pool], persistent_connections: options[:persistent_connections])
28
29
  signer unless options[:lazy]
29
30
  end
30
31
 
@@ -77,7 +78,7 @@ module AzureBlob
77
78
  "x-ms-range": options[:start] && "bytes=#{options[:start]}-#{options[:end]}",
78
79
  }.merge(additional_headers(options))
79
80
 
80
- Http.new(uri, headers, signer:).get
81
+ Http.new(uri, headers, signer:, connection_pool:).get
81
82
  end
82
83
 
83
84
  # Copy a blob between containers or within the same container
@@ -111,7 +112,7 @@ module AzureBlob
111
112
  "x-ms-requires-sync": "true",
112
113
  }.merge(additional_headers(options))
113
114
 
114
- Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put
115
+ Http.new(uri, headers, signer:, connection_pool:, **options.slice(:metadata, :tags)).put
115
116
  rescue Http::Error => error
116
117
  raise unless error.status == 409
117
118
  copy_blob_blocks(key, source_key, source_client, options)
@@ -134,7 +135,7 @@ module AzureBlob
134
135
  "x-ms-delete-snapshots": options[:delete_snapshots] || "include",
135
136
  }.merge(additional_headers(options))
136
137
 
137
- Http.new(uri, headers, signer:).delete
138
+ Http.new(uri, headers, signer:, connection_pool:).delete
138
139
  end
139
140
 
140
141
  # Delete all blobs prefixed by the given prefix.
@@ -174,7 +175,7 @@ module AzureBlob
174
175
  query[:marker] = marker
175
176
  query.reject! { |key, value| value.to_s.empty? }
176
177
  uri.query = URI.encode_www_form(**query)
177
- response = Http.new(uri, additional_headers(options), signer:).get
178
+ response = Http.new(uri, additional_headers(options), signer:, connection_pool:).get
178
179
  end
179
180
 
180
181
  BlobList.new(fetcher)
@@ -190,7 +191,7 @@ module AzureBlob
190
191
  uri = generate_uri("#{container}/#{key}")
191
192
  uri.query = URI.encode_www_form(timeout: options[:timeout]) if options[:timeout]
192
193
 
193
- response = Http.new(uri, additional_headers(options), signer:).head
194
+ response = Http.new(uri, additional_headers(options), signer:, connection_pool:).head
194
195
 
195
196
  Blob.new(response)
196
197
  end
@@ -216,7 +217,7 @@ module AzureBlob
216
217
  query = { comp: "tags" }
217
218
  query[:timeout] = options[:timeout] if options[:timeout]
218
219
  uri.query = URI.encode_www_form(**query)
219
- response = Http.new(uri, additional_headers(options), signer:).get
220
+ response = Http.new(uri, additional_headers(options), signer:, connection_pool:).get
220
221
 
221
222
  Tags.from_response(response).to_h
222
223
  end
@@ -231,7 +232,7 @@ module AzureBlob
231
232
  query = { restype: "container" }
232
233
  query[:timeout] = options[:timeout] if options[:timeout]
233
234
  uri.query = URI.encode_www_form(**query)
234
- response = Http.new(uri, additional_headers(options), signer:, raise_on_error: false).head
235
+ response = Http.new(uri, additional_headers(options), signer:, connection_pool:, raise_on_error: false).head
235
236
 
236
237
  Container.new(response)
237
238
  end
@@ -256,7 +257,7 @@ module AzureBlob
256
257
  query = { restype: "container" }
257
258
  query[:timeout] = options[:timeout] if options[:timeout]
258
259
  uri.query = URI.encode_www_form(**query)
259
- response = Http.new(uri, headers, signer:).put
260
+ response = Http.new(uri, headers, signer:, connection_pool:).put
260
261
  end
261
262
 
262
263
  # Delete the container
@@ -267,7 +268,7 @@ module AzureBlob
267
268
  query = { restype: "container" }
268
269
  query[:timeout] = options[:timeout] if options[:timeout]
269
270
  uri.query = URI.encode_www_form(**query)
270
- response = Http.new(uri, additional_headers(options), signer:).delete
271
+ response = Http.new(uri, additional_headers(options), signer:, connection_pool:).delete
271
272
  end
272
273
 
273
274
  # Return a URI object to a resource in the container. Takes a path.
@@ -318,7 +319,7 @@ module AzureBlob
318
319
  "x-ms-blob-content-disposition": options[:content_disposition],
319
320
  }.merge(additional_headers(options))
320
321
 
321
- Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put(nil)
322
+ Http.new(uri, headers, signer:, connection_pool:, **options.slice(:metadata, :tags)).put(nil)
322
323
  end
323
324
 
324
325
  # Append a block to an Append Blob
@@ -342,7 +343,7 @@ module AzureBlob
342
343
  "Content-MD5": options[:content_md5],
343
344
  }.merge(additional_headers(options))
344
345
 
345
- Http.new(uri, headers, signer:).put(content)
346
+ Http.new(uri, headers, signer:, connection_pool:).put(content)
346
347
  end
347
348
 
348
349
  # Uploads a block to a blob.
@@ -368,7 +369,7 @@ module AzureBlob
368
369
  "Content-MD5": options[:content_md5],
369
370
  }.merge(additional_headers(options))
370
371
 
371
- Http.new(uri, headers, signer:).put(content)
372
+ Http.new(uri, headers, signer:, connection_pool:).put(content)
372
373
 
373
374
  block_id
374
375
  end
@@ -397,7 +398,7 @@ module AzureBlob
397
398
  "x-ms-source-range": source_range && "bytes=#{source_range.min}-#{source_range.max}",
398
399
  }.merge(additional_headers(options))
399
400
 
400
- Http.new(uri, headers, signer:).put
401
+ Http.new(uri, headers, signer:, connection_pool:).put
401
402
 
402
403
  block_id
403
404
  end
@@ -428,7 +429,7 @@ module AzureBlob
428
429
  "x-ms-blob-content-disposition": options[:content_disposition],
429
430
  }.merge(additional_headers(options))
430
431
 
431
- Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put(content)
432
+ Http.new(uri, headers, signer:, connection_pool:, **options.slice(:metadata, :tags)).put(content)
432
433
  end
433
434
 
434
435
  private
@@ -466,7 +467,7 @@ module AzureBlob
466
467
  "x-ms-blob-content-disposition": options[:content_disposition],
467
468
  }.merge(additional_headers(options))
468
469
 
469
- Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put(content.read)
470
+ Http.new(uri, headers, signer:, connection_pool:, **options.slice(:metadata, :tags)).put(content.read)
470
471
  end
471
472
 
472
473
  # One Put Block From URL request per block, then one Put Block List to commit them.
@@ -509,6 +510,26 @@ module AzureBlob
509
510
  @host ||= "https://#{account_name}.blob.#{CLOUD_REGIONS_SUFFIX[cloud_regions]}"
510
511
  end
511
512
 
513
+ # Uses connection_pool when it is set: an object, a class or the name of a class.
514
+ # Otherwise uses persistent connections when net-http-persistent is available.
515
+ # Falls back to one connection per request when it is not, or when persistent_connections is false.
516
+ def build_connection_pool(connection_pool, persistent_connections:)
517
+ connection_pool = Object.const_get(connection_pool) if connection_pool.is_a?(String)
518
+ connection_pool = connection_pool.new if connection_pool.is_a?(Class)
519
+ connection_pool || persistent_connection_pool(persistent_connections)
520
+ end
521
+
522
+ # net-http-persistent is available when the application has loaded it.
523
+ # persistent_connections: true loads it, and raises when the gem is missing.
524
+ def persistent_connection_pool(persistent_connections)
525
+ return if persistent_connections == false
526
+
527
+ require "net/http/persistent" if persistent_connections
528
+ Net::HTTP::Persistent.new(name: "azure-blob", proxy: :ENV) if defined?(Net::HTTP::Persistent)
529
+ rescue LoadError
530
+ raise AzureBlob::Error, "`persistent_connections` requires the net-http-persistent gem. Add `gem \"net-http-persistent\"` to your Gemfile."
531
+ end
532
+
512
533
  def signer
513
534
  @signer ||=
514
535
  begin
@@ -527,6 +548,6 @@ module AzureBlob
527
548
  end
528
549
  end
529
550
 
530
- attr_reader :account_name, :container, :http, :cloud_regions, :access_key, :principal_id, :use_managed_identities, :delegation_key_expiration
551
+ attr_reader :account_name, :container, :http, :connection_pool, :cloud_regions, :access_key, :principal_id, :use_managed_identities, :delegation_key_expiration
531
552
  end
532
553
  end
@@ -27,64 +27,52 @@ module AzureBlob
27
27
 
28
28
  include REXML
29
29
 
30
- def initialize(uri, headers = {}, signer: nil, metadata: {}, tags: {}, debug: false, raise_on_error: true)
30
+ def initialize(uri, headers = {}, signer: nil, connection_pool: nil, metadata: {}, tags: {}, debug: false, raise_on_error: true)
31
31
  @raise_on_error = raise_on_error
32
32
  @date = Time.now.httpdate
33
33
  @uri = uri
34
34
  @signer = signer
35
+ @connection_pool = connection_pool
36
+ @debug = debug
35
37
  @headers = headers.merge(
36
38
  Metadata.new(metadata).headers,
37
39
  Tags.new(tags).headers,
38
40
  )
39
41
 
40
42
  sanitize_headers
41
-
42
- @http = Net::HTTP.new(uri.hostname, uri.port)
43
- @http.use_ssl = uri.port == 443
44
- @http.set_debug_output($stdout) if debug
45
43
  end
46
44
 
47
45
  def get
48
46
  sign_request("GET") if signer
49
- @response = http.start do |http|
50
- http.get(uri, headers)
51
- end
47
+ @response = perform(Net::HTTP::Get.new(uri, headers))
52
48
  raise_error unless success?
53
49
  response.body
54
50
  end
55
51
 
56
52
  def put(content = "")
57
53
  sign_request("PUT") if signer
58
- @response = http.start do |http|
59
- http.put(uri, content, headers)
60
- end
54
+ @response = perform(Net::HTTP::Put.new(uri, headers), content)
61
55
  raise_error unless success?
62
56
  true
63
57
  end
64
58
 
65
59
  def post(content = "")
66
60
  sign_request("POST") if signer
67
- @response = http.start do |http|
68
- http.post(uri, content, headers)
69
- end
61
+ @response = perform(Net::HTTP::Post.new(uri, headers), content)
70
62
  raise_error unless success?
71
63
  response.body
72
64
  end
73
65
 
74
66
  def head
75
67
  sign_request("HEAD") if signer
76
- @response = http.start do |http|
77
- http.head(uri, headers)
78
- end
68
+ @response = perform(Net::HTTP::Head.new(uri, headers))
79
69
  raise_error unless success?
80
70
  response
81
71
  end
82
72
 
83
73
  def delete
84
74
  sign_request("DELETE") if signer
85
- @response = http.start do |http|
86
- http.delete(uri, headers)
87
- end
75
+ @response = perform(Net::HTTP::Delete.new(uri, headers))
88
76
  raise_error unless success?
89
77
  response.body
90
78
  end
@@ -95,6 +83,19 @@ module AzureBlob
95
83
 
96
84
  private
97
85
 
86
+ # Sends the request through the connection pool when the client has one, which keeps the
87
+ # connection open for the next request. Without one, the request gets a connection of its own.
88
+ def perform(request, body = nil)
89
+ request.body = body if body
90
+
91
+ return connection_pool.request(uri, request) if connection_pool
92
+
93
+ http = Net::HTTP.new(uri.hostname, uri.port)
94
+ http.use_ssl = uri.port == 443
95
+ http.set_debug_output($stdout) if debug
96
+ http.start { http.request(request) }
97
+ end
98
+
98
99
  ERROR_MAPPINGS = {
99
100
  Net::HTTPNotFound => FileNotFoundError,
100
101
  Net::HTTPForbidden => ForbiddenError,
@@ -137,6 +138,6 @@ module AzureBlob
137
138
  ERROR_MAPPINGS[status] || ERROR_CODE_MAPPINGS[azure_error_code] || Error
138
139
  end
139
140
 
140
- attr_accessor :host, :http, :signer, :response, :headers, :uri, :date, :raise_on_error
141
+ attr_accessor :host, :connection_pool, :debug, :signer, :response, :headers, :uri, :date, :raise_on_error
141
142
  end
142
143
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AzureBlob
4
- VERSION = "0.9.0"
4
+ VERSION = "0.10.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azure-blob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joé Dupuis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-10-11 00:00:00.000000000 Z
11
+ date: 2026-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cgi