slow_down 0.3.1 → 1.0.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: a0c7264bef730a08dfe8c85d3b11190db7887ae2ec49cd15d3779fc7ada04b73
4
- data.tar.gz: b5a99d310faf2cc9e54d739c2af4a52d126ce346e0e7ec3219f19a9e357ba4a3
3
+ metadata.gz: ef031b47cde11badd5fac8ef0ad545783e0a19610cf6dfbad21d1c0f8e7a6041
4
+ data.tar.gz: c00f3956a6750c6ff18920518ee657b3f489dbd4a3b6618bf6b95ee4585b5286
5
5
  SHA512:
6
- metadata.gz: ed30fae233b0c0d48b48d36b60f7fa0e639391a87915712e13ac3a54f4aca7c59abbcdda31698783467be72ddcfd5054b222aa47670bef2d559ecd0a2f304908
7
- data.tar.gz: 234adb889bd5267b86f02fe913e0962f3cdeb2b8ca121f260f18a3f0401fd5f7151721dfd23c2056000a3bbb76aa81259ca5035943c6d4e24d8d4bf0b21b2bdd
6
+ metadata.gz: c026782c62882f375a51143c5bc5e85476a7c352770612e8ac304c02a807b60c781fe8d3eda7459d76ff47fd2359866adf1f061fa4b8ccdbfdd24414cc7a86bf
7
+ data.tar.gz: 8812582579e822c3c6a01d6e859682bacafd710d9f45ee0306a043a948e5e67702b7a46ccc62d1e9a45c37e8cd9bf5644ed7eb39311692880e9fd3052c62e4bd
@@ -0,0 +1,168 @@
1
+ # See https://github.com/bbatsov/rubocop/blob/master/manual/cops.md
2
+ AllCops:
3
+ DisplayCopNames: true
4
+ Exclude:
5
+ - ".git/**/*"
6
+ - "bin/*"
7
+ - "db/schema.rb"
8
+ - "node_modules/**/*"
9
+ - "vendor/**/*"
10
+ Bundler/OrderedGems:
11
+ Enabled: false
12
+ Layout/ArgumentAlignment:
13
+ EnforcedStyle: with_fixed_indentation
14
+ Layout/CaseIndentation:
15
+ EnforcedStyle: end
16
+ Layout/ElseAlignment:
17
+ Enabled: false
18
+ Layout/EndAlignment:
19
+ EnforcedStyleAlignWith: variable
20
+ Layout/FirstHashElementIndentation:
21
+ EnforcedStyle: consistent
22
+ Layout/LineLength:
23
+ Max: 120
24
+ Lint/AmbiguousOperator:
25
+ Enabled: false
26
+ Lint/AmbiguousRegexpLiteral:
27
+ Enabled: false
28
+ Lint/AssignmentInCondition:
29
+ AllowSafeAssignment: true
30
+ Lint/SuppressedException:
31
+ Enabled: false
32
+ Lint/UriEscapeUnescape:
33
+ Enabled: false
34
+ Metrics/AbcSize:
35
+ Enabled: false
36
+ Metrics/BlockLength:
37
+ Enabled: false
38
+ Metrics/ClassLength:
39
+ Enabled: false
40
+ Metrics/CyclomaticComplexity:
41
+ Enabled: false
42
+ Metrics/MethodLength:
43
+ Enabled: false
44
+ Metrics/ModuleLength:
45
+ Enabled: false
46
+ Metrics/ParameterLists:
47
+ Enabled: false
48
+ Metrics/PerceivedComplexity:
49
+ Enabled: false
50
+ Naming/AccessorMethodName:
51
+ Enabled: false
52
+ Naming/VariableNumber:
53
+ EnforcedStyle: snake_case
54
+ Style/Alias:
55
+ Enabled: false
56
+ Style/AsciiComments:
57
+ Enabled: false
58
+ Style/BlockDelimiters:
59
+ EnforcedStyle: line_count_based # Explicitly, even if this is the default setting
60
+ Exclude:
61
+ - "**/spec/**/*.rb"
62
+ - "**/test/**/*.rb"
63
+ Style/ClassAndModuleChildren:
64
+ Exclude:
65
+ - "**/spec/**/*.rb"
66
+ - "**/test/**/*.rb"
67
+ Style/Documentation:
68
+ Enabled: false
69
+ Style/DoubleNegation:
70
+ Enabled: false
71
+ Style/EmptyCaseCondition:
72
+ Enabled: false
73
+ Style/FormatStringToken:
74
+ EnforcedStyle: template
75
+ Style/IfUnlessModifier:
76
+ Enabled: false
77
+ Style/ImplicitRuntimeError:
78
+ Enabled: true
79
+ Style/MethodCallWithArgsParentheses:
80
+ Enabled: true
81
+ Exclude:
82
+ - "**/config/routes.rb"
83
+ - "**/config/routes/*.rb"
84
+ - "**/factories/**/*_factory.rb"
85
+ - "**/**/Gemfile"
86
+ - "**/**/Rakefile"
87
+ - "**/migrate/*"
88
+ - "*.gemspec"
89
+ - config.ru
90
+ IgnoreMacros: true
91
+ IgnoredMethods:
92
+ - alias
93
+ - alias_method
94
+ - and
95
+ - And
96
+ - attr_accessor
97
+ - attr_reader
98
+ - attr_writer
99
+ - attribute
100
+ - attributes
101
+ - context
102
+ - desc
103
+ - describe
104
+ - example
105
+ - fail
106
+ - feature
107
+ - Given
108
+ - halt
109
+ - include
110
+ - include_context
111
+ - include_examples
112
+ - it
113
+ - it_behaves_like
114
+ - it_should_behave_like
115
+ - not_to
116
+ - or
117
+ - puts
118
+ - raise
119
+ - redirect_back
120
+ - redirect_to
121
+ - render
122
+ - require
123
+ - require_relative
124
+ - shared_examples
125
+ - shared_context
126
+ - should
127
+ - should_not
128
+ - specify
129
+ - task
130
+ - Then
131
+ - throw
132
+ - to
133
+ - to_not
134
+ - use
135
+ - visit
136
+ - When
137
+ - xit
138
+ Style/MutableConstant:
139
+ Enabled: false
140
+ Style/NumericLiterals:
141
+ Enabled: false
142
+ Style/NumericPredicate:
143
+ Enabled: false
144
+ Style/PercentLiteralDelimiters:
145
+ PreferredDelimiters:
146
+ default: "()"
147
+ "%i": "()"
148
+ "%I": "()"
149
+ "%r": "{}"
150
+ "%w": "()"
151
+ "%W": "()"
152
+ Style/RedundantException:
153
+ Enabled: false
154
+ Style/RegexpLiteral:
155
+ Enabled: false
156
+ Style/RescueModifier:
157
+ Enabled: false
158
+ Style/Semicolon:
159
+ AllowAsExpressionSeparator: true
160
+ Style/StringLiterals:
161
+ Enabled: true
162
+ EnforcedStyle: double_quotes
163
+ Style/SymbolArray:
164
+ Enabled: false
165
+ Style/TernaryParentheses:
166
+ EnforcedStyle: require_parentheses_when_complex
167
+ Style/WordArray:
168
+ Enabled: false
@@ -1,20 +1,16 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.7
4
+ - 2.6
3
5
  - 2.5
4
- - 2.3
5
- - jruby-head
6
+ - 2.4
6
7
  services:
7
8
  - redis-server
8
9
  branches:
9
10
  only:
10
11
  - master
11
- - develop
12
12
  env:
13
13
  global:
14
14
  - REDIS_URL="redis://127.0.0.1:6379/0"
15
- - JRUBY_OPTS="$JRUBY_OPTS -Xcext.enabled=false -Xcompile.invokedynamic=false"
16
- matrix:
17
- allow_failures:
18
- - rvm: jruby-head
19
15
  notifications:
20
16
  email: false
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
2
4
 
3
- # Specify your gem's dependencies in slow_down.gemspec
4
5
  gemspec
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # SlowDown
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/slow_down.svg)](http://badge.fury.io/rb/slow_down)
4
- [![Build Status](https://travis-ci.org/lipanski/slow-down.svg?branch=master)](https://travis-ci.org/lipanski/slow-down)
4
+ [![Build Status](https://travis-ci.com/lipanski/slow-down.svg?branch=master)](https://travis-ci.com/lipanski/slow-down)
5
5
 
6
6
  ## Why would you want to slow down your requests?!
7
7
 
@@ -13,6 +13,16 @@ It relies on a Redis lock so it should be able to handle a cluster of servers al
13
13
  It's based on the `PX` and `NX` options of the Redis `SET` command, which should make it thread-safe.
14
14
  Note that these options were introduced with Redis version 2.6.12.
15
15
 
16
+ ## Installation
17
+
18
+ Add it to your *Gemfile*:
19
+
20
+ ```ruby
21
+ gem "slow_down"
22
+ ```
23
+
24
+ ...and call `bundle install`.
25
+
16
26
  ## Usage
17
27
 
18
28
  ### Basic
@@ -196,9 +206,11 @@ These polling strategies are just a proof of concept and their behaviour relies
196
206
 
197
207
  ## Development
198
208
 
199
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
209
+ Run tests:
200
210
 
201
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
211
+ ```
212
+ bundle exec rake
213
+ ```
202
214
 
203
215
  ## Contributing
204
216
 
data/Rakefile CHANGED
@@ -1,9 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rake"
2
4
  require "rake/testtask"
3
5
  require "bundler/gem_tasks"
4
6
 
7
+ task(default: ["rubocop", "test"])
8
+
9
+ desc "Run rubocop checks"
10
+ task :rubocop do
11
+ require "rubocop/rake_task"
12
+ RuboCop::RakeTask.new
13
+ end
14
+
5
15
  Rake::TestTask.new do |t|
6
16
  t.test_files = Dir.glob("test/**/test_*.rb")
7
17
  end
8
-
9
- task(default: :test)
@@ -1,4 +1,5 @@
1
- require "slow_down/version"
1
+ # frozen_string_literal: true
2
+
2
3
  require "slow_down/group"
3
4
 
4
5
  module SlowDown
@@ -28,9 +29,8 @@ module SlowDown
28
29
  end
29
30
 
30
31
  def reset(group_name = :default)
31
- if group = Group.find(group_name)
32
- group.reset
33
- end
32
+ group = Group.find(group_name) || return
33
+ group.reset
34
34
  end
35
35
 
36
36
  def find_or_create_group(*args)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "logger"
2
4
  require "redis"
3
5
  require "slow_down/strategy/linear"
@@ -19,7 +21,7 @@ module SlowDown
19
21
  redis_namespace: :slow_down,
20
22
  lock_namespace: :default,
21
23
  concurrency: nil,
22
- log_path: STDOUT,
24
+ log_path: $stdout,
23
25
  log_level: Logger::UNKNOWN
24
26
  }
25
27
 
@@ -88,7 +90,7 @@ module SlowDown
88
90
  end
89
91
 
90
92
  unless klass.is_a?(Class) && klass < Strategy::Base
91
- fail ConfigError, ":retry_strategy should be a class inheriting SlowDown::Strategy::Base"
93
+ raise ConfigError, ":retry_strategy should be a class inheriting SlowDown::Strategy::Base"
92
94
  end
93
95
 
94
96
  klass.new(retries, timeout).normalized_series
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "slow_down/configuration"
2
4
 
3
5
  module SlowDown
4
6
  class Group
5
7
  def self.all
6
- @groups || {}
8
+ @groups ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName
7
9
  end
8
10
 
9
11
  def self.find(name)
@@ -18,15 +20,14 @@ module SlowDown
18
20
  def self.find_or_create(name, options = {})
19
21
  if all[name] && !options.empty?
20
22
  all[name].config.logger.error(name) { "Group #{name} has already been configured elsewhere" }
21
- fail ConfigError, "Group #{name} has already been configured elsewhere - you may not override configurations"
23
+ raise ConfigError, "Group #{name} has already been configured elsewhere - you may not override configurations"
22
24
  end
23
25
 
24
26
  all[name] || create(name, options)
25
27
  end
26
28
 
27
29
  def self.remove(group_name)
28
- return unless group = Group.find(group_name)
29
-
30
+ group = Group.find(group_name) || return
30
31
  group.reset
31
32
  @groups.delete(group_name)
32
33
  end
@@ -43,16 +44,19 @@ module SlowDown
43
44
  end
44
45
 
45
46
  def run
46
- expires_at, iteration = Time.now + config.timeout, 0
47
+ expires_at = Time.now + config.timeout
48
+ iteration = 0
47
49
  config.logger.info(name) { "Run attempt initiatied, times out at #{expires_at}" }
48
50
 
49
- begin
51
+ loop do
50
52
  return yield if free?
53
+
51
54
  wait(iteration += 1)
52
- end until Time.now > expires_at
55
+ break if Time.now > expires_at
56
+ end
53
57
 
54
58
  config.logger.info(name) { "Run attempt timed out" }
55
- if config.raise_on_timeout
59
+ if config.raise_on_timeout # rubocop:disable Style/GuardClause
56
60
  config.logger.error(name) { "Timeout error raised" }
57
61
  raise Timeout
58
62
  end
@@ -1,14 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlowDown
2
4
  module Strategy
3
5
  class Base
4
6
  attr_reader :n, :max
5
7
 
6
- def initialize(n, max)
7
- @n, @max = n, max
8
+ def initialize(n, max) # rubocop:disable Naming/MethodParameterName
9
+ @n = n
10
+ @max = max
8
11
  end
9
12
 
10
13
  def series
11
- fail NotImplemented
14
+ raise NotImplemented
12
15
  end
13
16
 
14
17
  def normalized_series
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "slow_down/strategy/base"
2
4
 
3
5
  module SlowDown
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "slow_down/strategy/base"
2
4
 
3
5
  module SlowDown
@@ -5,7 +7,7 @@ module SlowDown
5
7
  class InverseExponential < Base
6
8
  def series
7
9
  n.times.map do |i|
8
- 1 - Math::E ** (i + 1)
10
+ 1 - Math::E**(i + 1)
9
11
  end
10
12
  end
11
13
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "slow_down/strategy/base"
2
4
 
3
5
  module SlowDown
@@ -1,31 +1,31 @@
1
- # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "slow_down/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "slow_down"
8
- spec.version = SlowDown::VERSION
8
+ spec.version = "1.0.0"
9
9
  spec.authors = ["Florin Lipan"]
10
10
  spec.email = ["florinlipan@gmail.com"]
11
11
 
12
- spec.summary = %q{A centralized Redis-based lock to help you wait on throttled resources}
13
- spec.description = %q{A centralized Redis-based lock to help you wait on throttled resources}
12
+ spec.summary = "A centralized Redis-based lock to help you wait on throttled resources"
13
+ spec.description = "A centralized Redis-based lock to help you wait on throttled resources"
14
14
  spec.homepage = "https://github.com/lipanski/slow-down"
15
15
  spec.license = "MIT"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
17
 
17
18
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
19
  spec.require_paths = ["lib"]
21
20
 
22
21
  spec.add_dependency "redis"
23
22
 
24
23
  spec.add_development_dependency "bundler"
25
- spec.add_development_dependency "rake"
26
24
  spec.add_development_dependency "dotenv"
25
+ spec.add_development_dependency "m"
27
26
  spec.add_development_dependency "minitest"
28
27
  spec.add_development_dependency "minitest-reporters"
29
28
  spec.add_development_dependency "minitest-stub-const"
30
- spec.add_development_dependency "m"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rubocop"
31
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slow_down
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florin Lipan
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-22 00:00:00.000000000 Z
11
+ date: 2020-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: dotenv
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: dotenv
56
+ name: m
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -109,7 +109,21 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: m
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
129
  - - ">="
@@ -131,13 +145,12 @@ extra_rdoc_files: []
131
145
  files:
132
146
  - ".env.example"
133
147
  - ".gitignore"
148
+ - ".rubocop.yml"
134
149
  - ".travis.yml"
135
150
  - Gemfile
136
151
  - LICENSE.txt
137
152
  - README.md
138
153
  - Rakefile
139
- - bin/console
140
- - bin/setup
141
154
  - lib/slow_down.rb
142
155
  - lib/slow_down/configuration.rb
143
156
  - lib/slow_down/group.rb
@@ -145,7 +158,6 @@ files:
145
158
  - lib/slow_down/strategy/fibonacci.rb
146
159
  - lib/slow_down/strategy/inverse_exponential.rb
147
160
  - lib/slow_down/strategy/linear.rb
148
- - lib/slow_down/version.rb
149
161
  - slow_down.gemspec
150
162
  homepage: https://github.com/lipanski/slow-down
151
163
  licenses:
@@ -159,15 +171,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
171
  requirements:
160
172
  - - ">="
161
173
  - !ruby/object:Gem::Version
162
- version: '0'
174
+ version: 2.4.0
163
175
  required_rubygems_version: !ruby/object:Gem::Requirement
164
176
  requirements:
165
177
  - - ">="
166
178
  - !ruby/object:Gem::Version
167
179
  version: '0'
168
180
  requirements: []
169
- rubyforge_project:
170
- rubygems_version: 2.7.6
181
+ rubygems_version: 3.1.2
171
182
  signing_key:
172
183
  specification_version: 4
173
184
  summary: A centralized Redis-based lock to help you wait on throttled resources
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "slow_down"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
data/bin/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
-
5
- bundle install
6
-
7
- # Do any other automated setup that you need to do here
@@ -1,3 +0,0 @@
1
- module SlowDown
2
- VERSION = "0.3.1"
3
- end