semian 0.7.9 → 0.8.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
  SHA1:
3
- metadata.gz: 84a146123d366cf81b13b36d9038f7338722647a
4
- data.tar.gz: 05cf06fd875a0b989d348c17bae7206b06bf07a4
3
+ metadata.gz: dd9c2d960a88c72716c280d2c26f8d81501bb090
4
+ data.tar.gz: f797fadf0c2848492fd22b291231b07bc63aa370
5
5
  SHA512:
6
- metadata.gz: 286c3a3a02aa7ecc68dc673baf15ece2c0693b186378f939d2ea5d77f30fbca6eb4f64a3019ea44cae6a35e21c02d56afec4340b1db41e6b3a9b39d638692204
7
- data.tar.gz: d2e1d56be8743f82bca4da45c632f5f0bfa23b36d137c1d11497455f60cd4d8faefc90416ee6941a788dfef1a604fae22e4ccb19379a2658431b01f314983d47
6
+ metadata.gz: cb62bd5e7269858b6f0a2108e3cab1e75a73e687daddad3f9c993b4be66768f59cdbbf38074bb4636def924521e537d1a9ce031b304efc12c61ed0f0b8694d35
7
+ data.tar.gz: 652545ff897ce72a76473075e2e71e8269655887bd360fce2ecf22c63c46ae38ca533357918df2fbf6a8b36712a9e9764f343691c9029871725934c204c63d8a
data/lib/semian.rb CHANGED
@@ -244,6 +244,7 @@ module Semian
244
244
  error_threshold: options[:error_threshold],
245
245
  error_timeout: options[:error_timeout],
246
246
  exceptions: Array(exceptions) + [::Semian::BaseError],
247
+ half_open_resource_timeout: options[:half_open_resource_timeout],
247
248
  implementation: implementation,
248
249
  )
249
250
  end
@@ -31,7 +31,7 @@ module Semian
31
31
 
32
32
  def acquire_semian_resource(scope:, adapter:, &block)
33
33
  return yield if resource_already_acquired?
34
- semian_resource.acquire(scope: scope, adapter: adapter) do
34
+ semian_resource.acquire(scope: scope, adapter: adapter, resource: self) do
35
35
  mark_resource_as_acquired(&block)
36
36
  end
37
37
  rescue ::Semian::OpenCircuitError => error
@@ -4,21 +4,22 @@ module Semian
4
4
 
5
5
  def_delegators :@state, :closed?, :open?, :half_open?
6
6
 
7
- attr_reader :name
7
+ attr_reader :name, :half_open_resource_timeout
8
8
 
9
- def initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:)
9
+ def initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:, half_open_resource_timeout: nil)
10
10
  @name = name.to_sym
11
11
  @success_count_threshold = success_threshold
12
12
  @error_count_threshold = error_threshold
13
13
  @error_timeout = error_timeout
14
14
  @exceptions = exceptions
15
+ @half_open_resource_timeout = half_open_resource_timeout
15
16
 
16
17
  @errors = implementation::SlidingWindow.new(max_size: @error_count_threshold)
17
18
  @successes = implementation::Integer.new
18
19
  @state = implementation::State.new
19
20
  end
20
21
 
21
- def acquire
22
+ def acquire(resource = nil, &block)
22
23
  return yield if disabled?
23
24
 
24
25
  half_open if open? && error_timeout_expired?
@@ -27,7 +28,7 @@ module Semian
27
28
 
28
29
  result = nil
29
30
  begin
30
- result = yield
31
+ result = maybe_with_half_open_resource_timeout(resource, &block)
31
32
  rescue *@exceptions => error
32
33
  mark_failed(error)
33
34
  raise error
@@ -122,5 +123,19 @@ module Semian
122
123
  def disabled?
123
124
  ENV['SEMIAN_CIRCUIT_BREAKER_DISABLED'] || ENV['SEMIAN_DISABLED']
124
125
  end
126
+
127
+ def maybe_with_half_open_resource_timeout(resource, &block)
128
+ result = nil
129
+
130
+ if half_open? && @half_open_resource_timeout && resource.respond_to?(:with_resource_timeout)
131
+ resource.with_resource_timeout(@half_open_resource_timeout) do
132
+ result = block.call
133
+ end
134
+ else
135
+ result = block.call
136
+ end
137
+
138
+ result
139
+ end
125
140
  end
126
141
  end
data/lib/semian/mysql2.rb CHANGED
@@ -83,6 +83,22 @@ module Semian
83
83
  end
84
84
  end
85
85
 
86
+ # TODO: write_timeout and connect_timeout can't be configured currently
87
+ # dynamically, await https://github.com/brianmario/mysql2/pull/955
88
+ def with_resource_timeout(temp_timeout)
89
+ prev_read_timeout = @read_timeout
90
+
91
+ begin
92
+ # C-ext reads this directly, writer method will configure
93
+ # properly on the client but based on my read--this is good enough
94
+ # until we get https://github.com/brianmario/mysql2/pull/955 in
95
+ @read_timeout = temp_timeout
96
+ yield
97
+ ensure
98
+ @read_timeout = prev_read_timeout
99
+ end
100
+ end
101
+
86
102
  private
87
103
 
88
104
  def query_whitelisted?(sql, *)
@@ -19,8 +19,8 @@ module Semian
19
19
  @circuit_breaker.destroy unless @circuit_breaker.nil?
20
20
  end
21
21
 
22
- def acquire(timeout: nil, scope: nil, adapter: nil)
23
- acquire_circuit_breaker(scope, adapter) do
22
+ def acquire(timeout: nil, scope: nil, adapter: nil, resource: nil)
23
+ acquire_circuit_breaker(scope, adapter, resource) do
24
24
  acquire_bulkhead(timeout, scope, adapter) do
25
25
  yield self
26
26
  end
@@ -29,11 +29,11 @@ module Semian
29
29
 
30
30
  private
31
31
 
32
- def acquire_circuit_breaker(scope, adapter)
32
+ def acquire_circuit_breaker(scope, adapter, resource)
33
33
  if @circuit_breaker.nil?
34
34
  yield self
35
35
  else
36
- @circuit_breaker.acquire do
36
+ @circuit_breaker.acquire(resource) do
37
37
  yield self
38
38
  end
39
39
  end
@@ -1,3 +1,3 @@
1
1
  module Semian
2
- VERSION = '0.7.9'
2
+ VERSION = '0.8.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.9
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Francis
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-04-09 00:00:00.000000000 Z
13
+ date: 2018-07-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake-compiler
@@ -68,6 +68,20 @@ dependencies:
68
68
  - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: byebug
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
71
85
  - !ruby/object:Gem::Dependency
72
86
  name: mysql2
73
87
  requirement: !ruby/object:Gem::Requirement