google-cloud-env 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22fba900d98b872b7f9de91312f5b169695eebcd395df5080f348b41358f45d3
4
- data.tar.gz: 5b14af4d3681740366160c09e6a93d707fe3a6c4c4f866bd1c93ea1c35a76037
3
+ metadata.gz: cf38fd3f5bb159512f455fde243bd4c6f120535533c5762b92ba28f107fb69ca
4
+ data.tar.gz: e989ea213443a933d3fa824fb23dc1a0c2e02e613936fe4e135aebc568aa1109
5
5
  SHA512:
6
- metadata.gz: 1fe82152bd2fdfafbe25a1d00ce3986757917c535a71630a5d32af0bdc7feb120567dd7fbf14a123f44957ca56519de8e2d176ce0084f6902f6182decb60242e
7
- data.tar.gz: a146cfd531bd36e4e517a385b3e7b8f252623a68727dff53bb453e6518e1b98236032d90d96b58bd79315b109355e74b4cc6f87f95bac607758674dcf2dbad62
6
+ metadata.gz: f0ce4b36dae87c5229eae4210e63487da960033430b47e11280cb4752da742d39d7dd3d24ed3cc7ea710124f75da0dbb7b8fa4e36a1ae29cce9f052e77c34100
7
+ data.tar.gz: d24d266602fa82aba6a6a050f049470c9c85cf937b6e0bd58d804135881b889451219e90f2058b88a74cb67ca1b5741d9df31eeeff8ee8f56a601c82bdd4b4a6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release History
2
2
 
3
+ ### 1.1.0 / 2019-05-29
4
+
5
+ * Support disabling of the metadata cache
6
+ * Support configurable retries when querying the metadata server
7
+ * Support configuration of the metadata request timeout
8
+
3
9
  ### 1.0.5 / 2018-09-20
4
10
 
5
11
  * Update documentation.
@@ -65,6 +65,15 @@ module Google
65
65
  # @private URL path for metadata server root.
66
66
  METADATA_ROOT_PATH = "/".freeze
67
67
 
68
+ # @private
69
+ METADATA_FAILURE_EXCEPTIONS = [
70
+ Faraday::TimeoutError,
71
+ Faraday::ConnectionFailed,
72
+ Errno::EHOSTDOWN,
73
+ Errno::ETIMEDOUT,
74
+ Timeout::Error
75
+ ].freeze
76
+
68
77
  ##
69
78
  # Create a new instance of the environment information.
70
79
  # Most client should not need to call this directly. Obtain a singleton
@@ -72,15 +81,36 @@ module Google
72
81
  # is provided for internal testing and allows mocking of the data.
73
82
  #
74
83
  # @param [Hash] env Mock environment variables.
84
+ # @param [Hash,false] metadata_cache The metadata cache. You may pass
85
+ # a prepopuated cache, an empty cache (the default) or `false` to
86
+ # disable the cache completely.
87
+ # @param [Numeric] request_timeout Timeout for each http request.
88
+ # Defaults to 0.1.
89
+ # @param [Integer] retry_count Number of times to retry http requests.
90
+ # Defaults to 1. Note that retry remains in effect even if a custom
91
+ # `connection` is provided.
92
+ # @param [Numeric] retry_interval Time between retries in seconds.
93
+ # Defaults to 0.1.
94
+ # @param [Numeric] retry_backoff_factor Multiplier applied to the retry
95
+ # interval on each retry. Defaults to 1.5.
96
+ # @param [Numeric] retry_max_interval Maximum time between retries in
97
+ # seconds. Defaults to 0.5.
75
98
  # @param [Faraday::Connection] connection Faraday connection to use.
76
- # @param [Hash] metadata_cache Mock cache.
99
+ # If specified, overrides the `request_timeout` setting.
77
100
  #
78
- def initialize env: nil, connection: nil, metadata_cache: nil
101
+ def initialize env: nil, connection: nil, metadata_cache: nil,
102
+ request_timeout: 0.1, retry_count: 2, retry_interval: 0.1,
103
+ retry_backoff_factor: 1.5, retry_max_interval: 0.5
104
+ @disable_metadata_cache = metadata_cache == false
79
105
  @metadata_cache = metadata_cache || {}
80
106
  @env = env || ::ENV
107
+ @retry_count = retry_count
108
+ @retry_interval = retry_interval
109
+ @retry_backoff_factor = retry_backoff_factor
110
+ @retry_max_interval = retry_max_interval
81
111
  @connection = connection ||
82
112
  ::Faraday.new(url: METADATA_HOST,
83
- request: { timeout: 0.1 })
113
+ request: { timeout: request_timeout })
84
114
  end
85
115
 
86
116
  ##
@@ -318,17 +348,14 @@ module Google
318
348
  # @return [Boolean]
319
349
  #
320
350
  def metadata?
321
- unless metadata_cache.include?(METADATA_ROOT_PATH)
322
- begin
323
- resp = connection.get METADATA_ROOT_PATH
324
- metadata_cache[METADATA_ROOT_PATH] = \
325
- resp.status == 200 && resp.headers["Metadata-Flavor"] == "Google"
326
- rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed,
327
- Errno::EHOSTDOWN
328
- metadata_cache[METADATA_ROOT_PATH] = false
351
+ path = METADATA_ROOT_PATH
352
+ if @disable_metadata_cache || !metadata_cache.include?(path)
353
+ metadata_cache[path] = retry_or_fail_with false do
354
+ resp = connection.get path
355
+ resp.status == 200 && resp.headers["Metadata-Flavor"] == "Google"
329
356
  end
330
357
  end
331
- metadata_cache[METADATA_ROOT_PATH]
358
+ metadata_cache[path]
332
359
  end
333
360
 
334
361
  ##
@@ -342,16 +369,15 @@ module Google
342
369
  # @return [String,nil]
343
370
  #
344
371
  def lookup_metadata type, entry
372
+ return nil unless metadata?
373
+
345
374
  path = "#{METADATA_PATH_BASE}/#{type}/#{entry}"
346
- if !metadata_cache.include?(path) && metadata?
347
- begin
375
+ if @disable_metadata_cache || !metadata_cache.include?(path)
376
+ metadata_cache[path] = retry_or_fail_with nil do
348
377
  resp = connection.get path do |req|
349
378
  req.headers = { "Metadata-Flavor" => "Google" }
350
379
  end
351
- metadata_cache[path] = resp.status == 200 ? resp.body.strip : nil
352
- rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed,
353
- Errno::EHOSTDOWN
354
- metadata_cache[path] = nil
380
+ resp.status == 200 ? resp.body.strip : nil
355
381
  end
356
382
  end
357
383
  metadata_cache[path]
@@ -371,6 +397,25 @@ module Google
371
397
  attr_reader :connection
372
398
  attr_reader :env
373
399
  attr_reader :metadata_cache
400
+
401
+ def retry_or_fail_with error_result
402
+ retries_remaining = @retry_count
403
+ retry_interval = @retry_interval
404
+ begin
405
+ yield
406
+ rescue *METADATA_FAILURE_EXCEPTIONS
407
+ retries_remaining -= 1
408
+ if retries_remaining >= 0
409
+ sleep retry_interval
410
+ retry_interval *= @retry_backoff_factor
411
+ if retry_interval > @retry_max_interval
412
+ retry_interval = @retry_max_interval
413
+ end
414
+ retry
415
+ end
416
+ error_result
417
+ end
418
+ end
374
419
  end
375
420
 
376
421
  @env = Env.new
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  class Env
19
- VERSION = "1.0.5".freeze
19
+ VERSION = "1.1.0".freeze
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-env
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-21 00:00:00.000000000 Z
11
+ date: 2019-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 0.50.0
117
+ version: 0.64.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 0.50.0
124
+ version: 0.64.0
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: simplecov
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -202,8 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  - !ruby/object:Gem::Version
203
203
  version: '0'
204
204
  requirements: []
205
- rubyforge_project:
206
- rubygems_version: 2.7.7
205
+ rubygems_version: 3.0.3
207
206
  signing_key:
208
207
  specification_version: 4
209
208
  summary: Google Cloud Platform hosting environment information.