cutoff 0.3.0 → 0.4.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: 8010fa3d0714c498fdb4ec4d34ea2dbf9fe4b05f534521d029adf370a51300bc
4
- data.tar.gz: 1ba8ed1d6b3c544ab72bc549be87b70c0e9b23c0594bcf74319e828abe0834a0
3
+ metadata.gz: 630a704243a3bd8c2196e0be8bba735eb8f2afb185c3db7ba68f3c8bed242041
4
+ data.tar.gz: 1922fbb0c382c62fd894cd8d2f0bfa2d21ef5bbf2cdb5f7b128d9de582c14344
5
5
  SHA512:
6
- metadata.gz: ee6ada9ba95a71a0717e79f1e0ab113910b4693ad285452dd0852a36d7f6613f57878d9272ae9d088334e2571c3b06a68d01dc0a2d8d92b1d835aec33858def8
7
- data.tar.gz: 14021607c75f24f0a8bbbeb711adb42f2d93c526ea30ba76386b972b108818fcb05a23bc4b4035659a76791fdeff4cf821478e95d0e4107af65ffe03a38f1f5d
6
+ metadata.gz: 7dcb206df1a3dc2ce265e0b78f0d93a8c3ddfe1f92b52945ff06cc87fa20ecfcaf2f4f9636555966b7c5cd9b624a498be788bd8fdf3d6c67cd9c5ffbd35aa393
7
+ data.tar.gz: 8178c8dd01267bc67b96eef6b2ff103f90a9e47954c55d10b33f1c21a39f36701bfb2bfd0e6896f6975a210e66c09611cd8cf419d9ea62b2cf489603dcff8808
data/CHANGELOG.md CHANGED
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.4.0] - 2021-10-01
11
+
12
+ - Add benchmarks and slight performance improvements
13
+ - Add Rails controller integration
14
+
10
15
  ## [0.3.0] - 2021-08-20
11
16
 
12
17
  - Allow timers to be disabled globally with `Cutoff.disable!`
@@ -24,7 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
24
29
  - Cutoff class
25
30
  - Mysql2 patch
26
31
 
27
- [Unreleased]: https://github.com/justinhoward/cutoff/compare/v0.3.0...HEAD
32
+ [Unreleased]: https://github.com/justinhoward/cutoff/compare/v0.4.0...HEAD
33
+ [0.4.0]: https://github.com/justinhoward/cutoff/compare/v0.3.0...v0.4.0
28
34
  [0.3.0]: https://github.com/justinhoward/cutoff/compare/v0.2.0...v0.3.0
29
35
  [0.2.0]: https://github.com/justinhoward/cutoff/compare/v0.1.0...v0.2.0
30
36
  [0.1.0]: https://github.com/justinhoward/cutoff/releases/tag/v0.1.0
data/README.md CHANGED
@@ -220,25 +220,52 @@ end
220
220
  Timing a Rails Controller
221
221
  ---------------------------
222
222
 
223
- One use of a cutoff is to add a deadline to a Rails controller action.
223
+ One use of a cutoff is to add a deadline to a Rails controller action. This is
224
+ typically preferable to approaches like `Rack::Timeout` that use the dangerous
225
+ `Timeout` class.
226
+
227
+ Cutoff includes a built-in integration for this purpose. If Rails is installed,
228
+ the `#cutoff` class method is available in your controllers.
224
229
 
225
230
  ```ruby
226
- around_action { |_controller, action| Cutoff.wrap(2.5) { action.call } }
231
+ class ApplicationController < ActionController::Base
232
+ # You may want to set a long global cutoff, but it's not required
233
+ cutoff 30
234
+ end
235
+
236
+ class UsersController < ApplicationController
237
+ cutoff 5.0
238
+
239
+ def index
240
+ # Now in your action, you can call `checkpoint!`, or if you're using the
241
+ # patches, checkpoints will be added automatically
242
+ Cutoff.checkpoint!
243
+ end
244
+ end
227
245
  ```
228
246
 
229
- Now in your action, you can call `checkpoint!`, or if you're using the Mysql2
230
- patch, checkpoints will be added automatically.
247
+ Just like with controller filters, you can use filters with the cutoff method.
231
248
 
232
249
  ```ruby
233
- def index
234
- # Do thing one
235
- Cutoff.checkpoint!
250
+ class UsersController < ApplicationController
251
+ # For example, use an :only filter
252
+ cutoff 5.0, only: :index
253
+
254
+ # Multiple calls work just fine. Last match wins
255
+ cutoff 2.5, only: :show
236
256
 
237
- # Do something else
257
+ def index
258
+ # ...
259
+ end
260
+
261
+ def show
262
+ # ...
263
+ end
238
264
  end
239
265
  ```
240
266
 
241
- Consider adding a global error handler for the `Cutoff::CutoffExceededError`
267
+ Consider adding a global error handler for the `Cutoff::CutoffExceededError` in
268
+ case you want to display a nice error page for timeouts.
242
269
 
243
270
  ```ruby
244
271
  class ApplicationController < ActionController::Base
@@ -45,8 +45,8 @@ class Cutoff
45
45
 
46
46
  # Loop through tokens like "WORD " or "/* "
47
47
  while @scanner.scan(/(\S+)\s+/)
48
- # Get the word part. None of our tokens care about case
49
- handle_token(@scanner[1].downcase)
48
+ # Get the word part
49
+ handle_token(@scanner[1])
50
50
  end
51
51
 
52
52
  return @scanner.string unless @found_select
@@ -68,7 +68,7 @@ class Cutoff
68
68
  hint_comment
69
69
  elsif token.start_with?('/*')
70
70
  block_comment
71
- elsif token.start_with?('select')
71
+ elsif token.match?(/^select/i)
72
72
  select
73
73
  else
74
74
  other
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_controller'
4
+
5
+ class Cutoff
6
+ module Rails
7
+ # Rails controller integration
8
+ module Controller
9
+ # Set a cutoff for the controller
10
+ #
11
+ # Can be called multiple times with different options to configure
12
+ # cutoffs for various conditions. If multiple conditions match a given
13
+ # controller, the last applied cutoff "wins".
14
+ #
15
+ # @example
16
+ # class ApplicationController
17
+ # # Apply a global maximum
18
+ # cutoff 30
19
+ # end
20
+ #
21
+ # class UsersController < ApplicationController
22
+ # # Override the base time limit
23
+ # cutoff 5.0
24
+ # cutoff 3.0, only: :show
25
+ # cutoff 7, if: :signed_in
26
+ # end
27
+ #
28
+ # @param seconds [Float, Integer] The allowed seconds for a controller
29
+ # action
30
+ # @param options [Hash] Options to pass to `around_action`. For example,
31
+ # pass `:only`, `:except`, `:if`, to limit the scope of the cutoff.
32
+ def cutoff(seconds, options = {})
33
+ prepend_around_action(options) do |_controller, action|
34
+ next action.call if @cutoff_wrapped
35
+
36
+ begin
37
+ @cutoff_wrapped = true
38
+ Cutoff.wrap(seconds, &action)
39
+ ensure
40
+ @cutoff_wrapped = false
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ ActionController::Base.extend(Cutoff::Rails::Controller)
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Namespace for Rails integration
4
+ module Rails
5
+ end
6
+
7
+ require 'cutoff/rails/controller' if Gem.loaded_specs['actionpack']
@@ -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.3.0')
6
+ Gem::Version.new('0.4.0')
7
7
  end
8
8
  end
data/lib/cutoff.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal:true
2
2
 
3
- require_relative 'cutoff/version'
4
- require_relative 'cutoff/error'
5
- require_relative 'cutoff/patch'
6
- require_relative 'cutoff/timer'
3
+ require 'cutoff/version'
4
+ require 'cutoff/error'
5
+ require 'cutoff/patch'
6
+ require 'cutoff/timer'
7
+ require 'cutoff/rails'
7
8
 
8
9
  class Cutoff
9
10
  CURRENT_STACK_KEY = 'cutoff_deadline_stack'
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.3.0
4
+ version: 0.4.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-08-20 00:00:00.000000000 Z
11
+ date: 2021-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rubocop
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -88,6 +102,8 @@ files:
88
102
  - lib/cutoff/patch.rb
89
103
  - lib/cutoff/patch/mysql2.rb
90
104
  - lib/cutoff/patch/net_http.rb
105
+ - lib/cutoff/rails.rb
106
+ - lib/cutoff/rails/controller.rb
91
107
  - lib/cutoff/timer.rb
92
108
  - lib/cutoff/version.rb
93
109
  homepage: https://github.com/justinhoward/cutoff
@@ -95,7 +111,7 @@ licenses:
95
111
  - MIT
96
112
  metadata:
97
113
  changelog_uri: https://github.com/justinhoward/cutoff/blob/master/CHANGELOG.md
98
- documentation_uri: https://www.rubydoc.info/gems/cutoff/0.3.0
114
+ documentation_uri: https://www.rubydoc.info/gems/cutoff/0.4.0
99
115
  post_install_message:
100
116
  rdoc_options: []
101
117
  require_paths: