cutoff 0.1.0 → 0.2.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: e27bb02cc23dc725a4bcd3b9585159723e3fa4d3c88f98ec207ff2b91cf438d4
4
- data.tar.gz: 8ab5a3a462bbbb2a32837e69de70d55ac59b3e347ead1db889d374f2be5b9224
3
+ metadata.gz: 64171df8b5550b99b603ba4bae4874174cc5e4bcce2632318be777ecc932dce3
4
+ data.tar.gz: 39ee62f1f743fdce6d9dd3e873bd3acf4d7efd09907d0f9e265e98c786cb7dc9
5
5
  SHA512:
6
- metadata.gz: c957634f0bc03f9fa8fb058080338978322adb54fce2c2c952c7b684ea3a979165f076abdc29878e416a084acd7b904ad6e8d637f0c1b90091ffa179fdb6076d
7
- data.tar.gz: b9ac92d37812b341fba24c6a3feb5c0f1f4c9265f1995aa5266b85e3d46e27c48e80fd4801ecc4f46d9a22b5195e0b1dfc35bc9c74449e2ac35c11ace28a5f15
6
+ metadata.gz: bb41605b534178706b45f3e6921aad80ff0ff50606038d95d9d50267910a590df59969cb77b1c7879d0b2cd429c60205a418e1ff590b1dd7b4e7f4394b0f4f3c
7
+ data.tar.gz: 713d75df722a2f82414373d482d4a981e9a579069de55262afe12004287abdd5b6f2e7e09052130a3b0ce875658e8177ce519bdadb549dbb92dc8970f9cd0813
data/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.0] - 2021-07-22
11
+
12
+ ### Added
13
+
14
+ - Net::HTTP patch
15
+
10
16
  ## [0.1.0] - 2021-07-19
11
17
 
12
18
  ### Added
@@ -14,4 +20,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
14
20
  - Cutoff class
15
21
  - Mysql2 patch
16
22
 
23
+ [Unreleased]: https://github.com/justinhoward/cutoff/compare/v0.2.0...HEAD
24
+ [0.2.0]: https://github.com/justinhoward/cutoff/compare/v0.1.0...v0.2.0
17
25
  [0.1.0]: https://github.com/justinhoward/cutoff/releases/tag/v0.1.0
data/README.md CHANGED
@@ -20,8 +20,8 @@ Cutoff.wrap(5) do
20
20
  end
21
21
  ```
22
22
 
23
- It has a built-in patch for Mysql2 to auto-insert checkpoints and timeout query
24
- hints.
23
+ It has built-in patches for Mysql2 and Net::HTTP to auto-insert checkpoints and
24
+ timeouts.
25
25
 
26
26
  ```ruby
27
27
  require 'cutoff/patch/mysql2'
@@ -50,7 +50,7 @@ single query will take longer than 3 seconds. However, imagine a bad controller
50
50
  action or background job executes 100 slow queries. In that case, the queries
51
51
  add up to 300 seconds, much too long.
52
52
 
53
- Deadlines keep track of the total elapsed time in a request of job and interrupt
53
+ Deadlines keep track of the total elapsed time in a request or job and interrupt
54
54
  it if it takes too long.
55
55
 
56
56
  Installation
@@ -138,8 +138,11 @@ Patches
138
138
  -------------
139
139
 
140
140
  Cutoff is in early stages, but it aims to provide patches for common networked
141
- dependencies. The first of these is the `mysql2` patch. It is not loaded by
142
- default, so you need to require it manually.
141
+ dependencies. Patches automatically insert useful checkpoints and timeouts. The
142
+ patches so far are for `mysql2` and `Net::HTTP`. They are not loaded by default,
143
+ so you need to require them manually.
144
+
145
+ For example, to load the Mysql2 patch:
143
146
 
144
147
  ```ruby
145
148
  # In your Gemfile
@@ -152,10 +155,14 @@ require 'cutoff'
152
155
  require 'cutoff/patch/mysql2'
153
156
  ```
154
157
 
155
- Once it is enabled, any `Mysql2::Client` object will respect the current cutoff
156
- if one is set.
158
+ ### Mysql2
159
+
160
+ Once it is enabled, any `Mysql2::Client` object will respect the current
161
+ class-level cutoff if one is set.
157
162
 
158
163
  ```ruby
164
+ require 'cutoff/patch/mysql2'
165
+
159
166
  client = Mysql2::Client.new
160
167
  Cutoff.wrap(3) do
161
168
  sleep(4)
@@ -183,6 +190,33 @@ Cutoff.wrap(3) do
183
190
  end
184
191
  ```
185
192
 
193
+ ### Net::HTTP
194
+
195
+ Once it is enabled, any `Net::HTTP` requests will respect the current
196
+ class-level cutoff if one is set.
197
+
198
+ ```ruby
199
+ require 'cutoff/patch/net_http'
200
+
201
+ Cutoff.wrap(3) do
202
+ sleep(5)
203
+
204
+ # The cutoff is expired, so this hits a checkpoint and will not be executed
205
+ Net::HTTP.get(URI.parse('http://example.com'))
206
+ end
207
+
208
+ Cutoff.wrap(3) do
209
+ sleep(1.5)
210
+
211
+ # The cutoff has 1.5 seconds left, so this request will be executed
212
+ # open_timeout, read_timeout, and write_timeout (Ruby >= 2.6) will each
213
+ # be set to 1.5
214
+ # This means the overall time can be > 1.5 since the combined phases can take
215
+ # up to 4.5 seconds
216
+ Net::HTTP.get(URI.parse('http://example.com'))
217
+ end
218
+ ```
219
+
186
220
  Timing a Rails Controller
187
221
  ---------------------------
188
222
 
@@ -360,10 +394,9 @@ never interrupt a running program unless:
360
394
  - `checkpoint!` is called
361
395
  - a network timeout is exceeded
362
396
 
363
- Patches such as the current Mysql2 patch are designed to ease the burden on
364
- developers to manually call `checkpoint!` or configure network timeouts. The
365
- ruby `Timeout` class is not used. See Julia Evans' post on [Why Ruby's Timeout
366
- is dangerous][julia_evans].
397
+ Patches are designed to ease the burden on developers to manually call
398
+ `checkpoint!` or configure network timeouts. The ruby `Timeout` class is not
399
+ used. See Julia Evans' post on [Why Ruby's Timeout is dangerous][julia_evans].
367
400
 
368
401
  Patches are only applied by explicit opt-in, and Cutoff can always be used as a
369
402
  standalone library.
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal:true
2
+
3
+ require 'net/http'
4
+
5
+ class Cutoff
6
+ module Patch
7
+ # Adds a checkpoint for starting HTTP requests and sets network timeouts
8
+ # to the remaining time
9
+ module NetHttp
10
+ # Construct a {Net::HTTP}, but with the timeouts set to the remaining
11
+ # cutoff time if one is active
12
+ def initialize(address, port = nil)
13
+ super
14
+ return unless (cutoff = Cutoff.current)
15
+
16
+ @open_timeout = cutoff.seconds_remaining
17
+ @read_timeout = cutoff.seconds_remaining
18
+ @write_timeout = cutoff.seconds_remaining
19
+ end
20
+
21
+ # Same as the original start, but with a cutoff checkpoint
22
+ #
23
+ # @see {Net::HTTP#start}
24
+ def start
25
+ Cutoff.checkpoint!
26
+ super
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ Net::HTTP.prepend(Cutoff::Patch::NetHttp)
@@ -3,6 +3,6 @@
3
3
  class Cutoff
4
4
  # @return [Gem::Version] The current version of the cutoff gem
5
5
  def self.version
6
- Gem::Version.new('0.1.0')
6
+ Gem::Version.new('0.2.0')
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cutoff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Howard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-20 00:00:00.000000000 Z
11
+ date: 2021-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -87,13 +87,14 @@ files:
87
87
  - lib/cutoff/error.rb
88
88
  - lib/cutoff/patch.rb
89
89
  - lib/cutoff/patch/mysql2.rb
90
+ - lib/cutoff/patch/net_http.rb
90
91
  - lib/cutoff/version.rb
91
92
  homepage: https://github.com/justinhoward/cutoff
92
93
  licenses:
93
94
  - MIT
94
95
  metadata:
95
96
  changelog_uri: https://github.com/justinhoward/cutoff/blob/master/CHANGELOG.md
96
- documentation_uri: https://www.rubydoc.info/gems/cutoff/0.1.0
97
+ documentation_uri: https://www.rubydoc.info/gems/cutoff/0.2.0
97
98
  post_install_message:
98
99
  rdoc_options: []
99
100
  require_paths: