knifeswitch 1.1.0 → 1.1.1

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: c21ca91eae9b7d22ebef28c43b90429e6e1c5e10501fd39e54eefffec2f2ef45
4
- data.tar.gz: 5faa4bc86e93ad8dbff72095b2f4da97adeb8e54ebc9d0ec8191b4db0c7999ea
3
+ metadata.gz: 0a4ccbfd70b65a64a5dd7ca2b4415776a2edbf69ecff735d264410f52e872c9c
4
+ data.tar.gz: e05145c272a86defde56a34123049f1e05995c456291a8f3b08c9e8b46728890
5
5
  SHA512:
6
- metadata.gz: 294756a35f27839e007c34496e550d88e95979f0252f1532536b06851de52c87f7f0fcf8f51e384976189a728a1c6d91b1f9dcb1fea918955ff022a1d16485c0
7
- data.tar.gz: 94bc6838ab85d95ca0bc42a5ca131e487d63dbeaa67f2c5075b9d482418b08ea8a5e363b38e4b5b169d8ee080f33b827c0017754949c20de2acd0928862f13fc
6
+ metadata.gz: 333b47a38d2c6cfb4a71f9a2fd44a7502e9ca294d93310d31d9279fcce504136ec1b1a5fcbf734a4bb45e9bf4c05479e3687fc2f1b5684bd774f7cb3291ed651
7
+ data.tar.gz: fdfbfe1d54427f06991127547f6f1a700dd0caba0dffe7de0514e9970eed1a769cc594ac5d53b684f263f88a7b104db3a1460a9925b959c52ca59336c6434f29
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # Knifeswitch
2
+
3
+ ![knifeswitch](https://user-images.githubusercontent.com/2793160/67077729-c908dd80-f1ca-11e9-96c7-5b1c8b792254.jpg)
4
+
2
5
  Yet another circuit breaker gem. This one strives to be as small and simple as possible. In the effort to remain simple, it currently only supports Rails with MySQL.
3
6
 
4
7
  ## Usage
@@ -49,23 +49,38 @@ module Knifeswitch
49
49
  #
50
50
  # Raises Knifeswitch::CircuitOpen when called while the circuit is open.
51
51
  def run
52
- if open?
53
- callback.try(:call, CircuitOpen.new)
54
- raise CircuitOpen
55
- end
52
+ with_connection do
53
+ if open?
54
+ callback.try(:call, CircuitOpen.new)
55
+ raise CircuitOpen
56
+ end
57
+
58
+ begin
59
+ result = yield
60
+ rescue Exception => error
61
+ if exceptions.any? { |watched| error.is_a?(watched) }
62
+ increment_counter!
63
+ callback.try(:call, error)
64
+ else
65
+ reset_counter!
66
+ end
67
+
68
+ raise error
69
+ end
56
70
 
57
- result = yield
58
- reset_counter!
59
- result
60
- rescue Exception => error
61
- if exceptions.any? { |watched| error.is_a?(watched) }
62
- increment_counter!
63
- callback.try(:call, error)
64
- else
65
71
  reset_counter!
72
+ result
66
73
  end
74
+ ensure
75
+ reset_record
76
+ end
77
+
78
+ def closetime
79
+ record&.dig("closetime")
80
+ end
67
81
 
68
- raise error
82
+ def counter
83
+ record&.dig("counter") || 0
69
84
  end
70
85
 
71
86
  # Queries the database to see if the circuit is open.
@@ -74,22 +89,7 @@ module Knifeswitch
74
89
  # When the circuit is open, calls to `run` will raise CircuitOpen
75
90
  # instead of yielding.
76
91
  def open?
77
- result = sql(:select_value, %(
78
- SELECT COUNT(*) c FROM knifeswitch_counters
79
- WHERE name = ? AND closetime > ?
80
- ), namespace, DateTime.now)
81
-
82
- result > 0
83
- end
84
-
85
- # Retrieves the current counter value.
86
- def counter
87
- result = sql(:select_value, %(
88
- SELECT counter FROM knifeswitch_counters
89
- WHERE name = ?
90
- ), namespace)
91
-
92
- result || 0
92
+ return closetime && closetime > DateTime.now
93
93
  end
94
94
 
95
95
  # Increments counter and opens the circuit if it went
@@ -117,6 +117,7 @@ module Knifeswitch
117
117
 
118
118
  # Sets the counter to zero
119
119
  def reset_counter!
120
+ return if counter == 0
120
121
  sql(:execute, %(
121
122
  INSERT INTO knifeswitch_counters (name,counter)
122
123
  VALUES (?, 0)
@@ -126,14 +127,42 @@ module Knifeswitch
126
127
 
127
128
  private
128
129
 
130
+ def load_record
131
+ sql(:select_one, %(
132
+ SELECT counter, closetime FROM knifeswitch_counters
133
+ WHERE name = ?
134
+ ), @namespace)
135
+ end
136
+
137
+ def reset_record
138
+ @record = nil
139
+ end
140
+
141
+ def record
142
+ @record ||= load_record
143
+ end
144
+
129
145
  # Executes a SQL query with the given Connection method
130
146
  # (i.e. :execute, or :select_values)
131
147
  def sql(method, query, *args)
132
148
  query = ActiveRecord::Base.send(:sanitize_sql_array, [query] + args)
133
-
134
- ActiveRecord::Base.connection_pool.with_connection do |conn|
149
+ with_connection do |conn|
135
150
  conn.send(method, query)
136
151
  end
137
152
  end
153
+
154
+ def with_connection
155
+ if @conn
156
+ yield(@conn)
157
+ else
158
+ begin
159
+ @conn = ActiveRecord::Base.connection_pool.checkout
160
+ yield(@conn)
161
+ ensure
162
+ ActiveRecord::Base.connection_pool.checkin(@conn)
163
+ @conn = nil
164
+ end
165
+ end
166
+ end
138
167
  end
139
168
  end
@@ -1,3 +1,3 @@
1
1
  module Knifeswitch
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knifeswitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nigel Baillie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-31 00:00:00.000000000 Z
11
+ date: 2019-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  - !ruby/object:Gem::Version
81
81
  version: '0'
82
82
  requirements: []
83
- rubygems_version: 3.0.4
83
+ rubygems_version: 3.0.6
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: Simple implementation of the circuit breaker pattern.