double_restraint 1.0.0 → 1.0.1

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
  SHA256:
3
- metadata.gz: 730e777e36c39bfc23ff335c32c5ed3afcc92f939496546a27e26c16cb755a7b
4
- data.tar.gz: 68a039424903a839c46defb56535a33a793ab8e044ed62e986ff0b2fe5f4ffef
3
+ metadata.gz: 8f0ee8109419ddc8e93ea3685a0d2f32c3d8b96383afe4c4330bbc866d9ee566
4
+ data.tar.gz: 8c92f8eabaae7b7b4e0754d5e968d3fc1ecb045f5b4aa04c2d323f167551f467
5
5
  SHA512:
6
- metadata.gz: fd3bd50d0e03ba15d9a043e54f877e3c1828d546d54b1f2751adfc5aac0f4c8da88eda18e7af4c7597bf6203272f7748b383e2c24ea548e56e72b3b9a83ac0d8
7
- data.tar.gz: 597684fa2b5eefb6f6ae9c5de1fbdeb353846368a209ba606828a2b670a520398400ad3703ae5f0935445d21f3bb58bb62cb6454c045c347dc12b3a897cf5b5c
6
+ metadata.gz: a7e55a6862bfda0b56a445b37af3fe3d0b486cd2ffde2e4ab2246700496cbe3017f3c737e9c507d1ab3ede613d34fc2137c8679356345a74757bc257010f32a0
7
+ data.tar.gz: b80b29ec078b8fe3b48bbb4e5d549684a7e3f56157d29ab58d6fd09e2a20679513ebe250416238b871421f608146271ca07b66c461841a151f095479ce8231f3
data/CHANGELOG.md CHANGED
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.0.1]
8
+ ### Added
9
+ - Methods to expose the pool limits, timeouts and current in use sizes.
10
+
7
11
  ## [1.0.0]
8
12
  ### Added
9
13
  - Everything!
data/README.md CHANGED
@@ -77,6 +77,8 @@ However, you can also specify the Redis instance directly on the `DoubleRestrain
77
77
  restraint = DoubleRestraint.new("MyWebService", redis: Redis.new(url: redis_url)), timeout: 0.5, long_running_timeout: 5.0, long_running_limit: 5)
78
78
  ```
79
79
 
80
+ You can peek at the current pool sizes as well if
81
+
80
82
  ## Installation
81
83
 
82
84
  Add this line to your application's Gemfile:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -3,6 +3,8 @@
3
3
  require "restrainer"
4
4
 
5
5
  class DoubleRestraint
6
+ attr_reader :timeout, :long_running_timeout
7
+
6
8
  # @param name [String, Symbol] The name of the restraint.
7
9
  # @param timeout [Numeric] The first timeout that will be yielded to the block.
8
10
  # @param long_running_timeout [Numeric] The timeout that will be yielded to the block if
@@ -20,22 +22,20 @@ class DoubleRestraint
20
22
  @timeout = timeout
21
23
  @long_running_timeout = long_running_timeout
22
24
  @timeout_errors = Array(timeout_errors)
23
- @restrainer = Restrainer.new("DoubleRestrainer(#{name})", limit: limit, redis: redis) if limit
25
+ limit = -1 if limit.to_i <= 0
26
+ @restrainer = Restrainer.new("DoubleRestrainer(#{name})", limit: limit, redis: redis)
24
27
  @long_running_restrainer = Restrainer.new("DoubleRestrainer(#{name}).long_running", limit: long_running_limit, redis: redis)
25
28
  end
26
29
 
27
30
  # Execute a block of code. The block will be yielded with the timeout value. If the block raises
28
31
  # a timeout error, then it will be called again with the long running timeout. The code in the block
29
32
  # must be idempotent since it can be run twice.
33
+ #
30
34
  # @yieldparam [Numeric] the timeout value to use in the block.
31
35
  # @raise [Restrainer::ThrottleError] if too many concurrent processes are trying to use the restraint.
32
- def execute
36
+ def execute(&block)
33
37
  begin
34
- if @restrainer
35
- @restrainer.throttle do
36
- yield @timeout
37
- end
38
- else
38
+ @restrainer.throttle do
39
39
  yield @timeout
40
40
  end
41
41
  rescue => e
@@ -48,4 +48,44 @@ class DoubleRestraint
48
48
  end
49
49
  end
50
50
  end
51
+
52
+ # Get the current size of the default pool. This can be useful in
53
+ # collecting realtime stats about how the pool is being utilized.
54
+ #
55
+ # @return [Integer]
56
+ def default_pool_size
57
+ @restrainer.current
58
+ end
59
+
60
+ # Get the current size of the long running pool. This can be useful in
61
+ # collecting realtime stats about how the pool is being utilized.
62
+ #
63
+ # @return [Integer]
64
+ def long_running_pool_size
65
+ @long_running_restrainer.current
66
+ end
67
+
68
+ # Get the limit for the default pool. This will return -1 if there
69
+ # is no limit set on that pool.
70
+ #
71
+ # @return [Integer]
72
+ def default_pool_limit
73
+ @restrainer.limit
74
+ end
75
+
76
+ # Get the limit for the long running pool.
77
+ #
78
+ # @return [Integer]
79
+ def long_running_pool_limit
80
+ @long_running_restrainer.limit
81
+ end
82
+
83
+ # Helper method to determine if a timeout represents the long running timeout.
84
+ # Note that the timeout and long running timeouts need to be different values
85
+ # in order for this to work.
86
+ #
87
+ # @return [Boolean]
88
+ def long_running?(timeout)
89
+ timeout.to_f.round(6) == @long_running_timeout.to_f.round(6)
90
+ end
51
91
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: double_restraint
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-28 00:00:00.000000000 Z
11
+ date: 2023-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: restrainer