rack-delay 0.2.0
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 +7 -0
- data/.gitignore +18 -0
- data/README.md +136 -0
- data/lib/rack/delay.rb +77 -0
- data/rack-delay.gemspec +16 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1055f77eeedb3971ee085f94027664edd0295563
|
4
|
+
data.tar.gz: 80c959084f0fff29a83011062d22d65b94460d9b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a03cd24c0c16cf63855637c17443d4f72b604b393075936ac9683c2d3e8c0ddaae68fd1de478a558780937ba0aae6cbcfea648d841314bc1c53c793078de3b2c
|
7
|
+
data.tar.gz: 7640dbba80c71edfa4ae49fe797ed2290e4354edd94450b10381b21d6bfd6368390a45edc126bf6fac5a07683a0faef8de7ac9231d98e27011cad694d284e68d
|
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
Rack-delay
|
2
|
+
==========
|
3
|
+
|
4
|
+
A simple rack middleware to add a random delay for requests, so you can simulate network latency during development.
|
5
|
+
This library works with any rack capable framework, so you can use with sinatra or rails or whatever you want.
|
6
|
+
|
7
|
+
## Install
|
8
|
+
|
9
|
+
You can install this library via gem
|
10
|
+
|
11
|
+
gem install rack-delay
|
12
|
+
|
13
|
+
or via Bundler adding
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'rack-delay'
|
17
|
+
```
|
18
|
+
|
19
|
+
to your Gemfile and install:
|
20
|
+
|
21
|
+
bundle install
|
22
|
+
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
To use rack-delay simply add it in config.ru file.
|
27
|
+
Below is an example of middleware:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# config.ru
|
31
|
+
|
32
|
+
require 'rack/delay'
|
33
|
+
|
34
|
+
use Rack::Delay
|
35
|
+
|
36
|
+
app = proc do |env|
|
37
|
+
[ 200, {'Content-Type' => 'text/plain'}, "b" ]
|
38
|
+
end
|
39
|
+
|
40
|
+
run app
|
41
|
+
```
|
42
|
+
|
43
|
+
Rack-delay when use without options, will add a random delay to every request.
|
44
|
+
You probably want to customize this behaviour, so you can pass options to middleware ex:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# config.ru
|
48
|
+
|
49
|
+
require 'rack/delay'
|
50
|
+
|
51
|
+
use Rack::Delay, {
|
52
|
+
:min => 0, # ms
|
53
|
+
:max => 5000, # ms
|
54
|
+
:if => lambda { |request|
|
55
|
+
request.xhr?
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
app = proc do |env|
|
60
|
+
[ 200, {'Content-Type' => 'text/plain'}, "b" ]
|
61
|
+
end
|
62
|
+
|
63
|
+
run app
|
64
|
+
```
|
65
|
+
|
66
|
+
In previous example rack-delay will add a random delay (between :min/:max) to every ajax requests.
|
67
|
+
|
68
|
+
Also you can do more complex things like use a different delay for different requests, ex:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
# config.ru
|
72
|
+
|
73
|
+
require 'rack/delay'
|
74
|
+
|
75
|
+
use Rack::Delay, {
|
76
|
+
:delay => lambda { |request|
|
77
|
+
if request.get? # fixed 100ms delay to every get request
|
78
|
+
[100]
|
79
|
+
elsif request.post? # 300ms - 3sec delay to every post request
|
80
|
+
[300, 3000]
|
81
|
+
else
|
82
|
+
[0, rand(5000)] # use a random delay
|
83
|
+
end
|
84
|
+
|
85
|
+
# if you not return a number from this block, rack-delay will use defaults range :min/:max to calc random delay
|
86
|
+
},
|
87
|
+
:if => lambda { |request|
|
88
|
+
request.xhr? and request.ip == '127.0.0.1'
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
app = proc do |env|
|
93
|
+
[ 200, {'Content-Type' => 'text/plain'}, "b" ]
|
94
|
+
end
|
95
|
+
|
96
|
+
run app
|
97
|
+
```
|
98
|
+
|
99
|
+
## Options
|
100
|
+
|
101
|
+
Here a list of options which rack-delay can understand.
|
102
|
+
|
103
|
+
|
104
|
+
* **:min** / **:max**
|
105
|
+
Specifies range in msec of random delay. When :min == :max delay will be fixed
|
106
|
+
|
107
|
+
* **:delay**
|
108
|
+
Another way to customize delay generation. Value returned by block can be a single number or 2 item array.
|
109
|
+
When a number, then the request will be slowdown exactly by that number msec.
|
110
|
+
If returns 2 items array (eg. [min, max]), then will be used to build range.
|
111
|
+
|
112
|
+
* **:if** / **:unless**
|
113
|
+
Used to control activation of rack-delay.
|
114
|
+
|
115
|
+
## Copyright
|
116
|
+
|
117
|
+
Copyright (c) 2013 Vincenzo Farruggia.
|
118
|
+
|
119
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
120
|
+
a copy of this software and associated documentation files (the
|
121
|
+
"Software"), to deal in the Software without restriction, including
|
122
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
123
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
124
|
+
permit persons to whom the Software is furnished to do so, subject to
|
125
|
+
the following conditions:
|
126
|
+
|
127
|
+
The above copyright notice and this permission notice shall be
|
128
|
+
included in all copies or substantial portions of the Software.
|
129
|
+
|
130
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
131
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
132
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
133
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
134
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
135
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
136
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/rack/delay.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
module Rack
|
2
|
+
class Delay
|
3
|
+
HEADER = 'X-Rack-Delay'
|
4
|
+
|
5
|
+
attr_reader :app, :options
|
6
|
+
|
7
|
+
def initialize(app, options={})
|
8
|
+
@app = app
|
9
|
+
|
10
|
+
if options.has_key?(:unless)
|
11
|
+
options[:if] = options.delete(:unless)
|
12
|
+
options[:negate] = true
|
13
|
+
end
|
14
|
+
|
15
|
+
@options = {
|
16
|
+
:min => 50, # msec
|
17
|
+
:max => 5000, # msec
|
18
|
+
:delay => nil,
|
19
|
+
:if => nil,
|
20
|
+
:negate => false
|
21
|
+
}.merge(options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def peek_delay(min, max)
|
25
|
+
if min > max
|
26
|
+
tmp = max
|
27
|
+
max = min
|
28
|
+
min = tmp
|
29
|
+
end
|
30
|
+
return min / 1000.0 if min == max
|
31
|
+
(min + rand(max - min)) / 1000.0
|
32
|
+
end
|
33
|
+
|
34
|
+
def _call_block(block, request)
|
35
|
+
if block.arity == 0
|
36
|
+
block.call()
|
37
|
+
else
|
38
|
+
block.call(request)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def call(env)
|
43
|
+
request = Rack::Request.new(env)
|
44
|
+
should_delay = true
|
45
|
+
|
46
|
+
|
47
|
+
should_delay = !!_call_block(options[:if], request) if options[:if]
|
48
|
+
should_delay = !should_delay if options[:negate]
|
49
|
+
|
50
|
+
header_delay = 'none'
|
51
|
+
|
52
|
+
if should_delay
|
53
|
+
|
54
|
+
min_delay = options[:min]
|
55
|
+
max_delay = options[:max]
|
56
|
+
|
57
|
+
if options[:delay]
|
58
|
+
ret = _call_block(options[:delay], request)
|
59
|
+
unless ret.nil?
|
60
|
+
ret = [ret] unless ret.kind_of?(Array)
|
61
|
+
min_delay = ret.first
|
62
|
+
max_delay = ret.last
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
delay = peek_delay(min_delay, max_delay)
|
67
|
+
header_delay = delay
|
68
|
+
sleep(delay)
|
69
|
+
end
|
70
|
+
|
71
|
+
status , headers , response = app.call(env)
|
72
|
+
headers[ HEADER ] = header_delay.to_s
|
73
|
+
[status , headers , response]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/rack-delay.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'rack-delay'
|
3
|
+
s.version = '0.2.0'
|
4
|
+
s.date = '2013-04-17'
|
5
|
+
s.platform = Gem::Platform::RUBY
|
6
|
+
s.summary = %Q{#{ s.name } v#{ s.version }}
|
7
|
+
s.description = %q{A simple rack middleware to slowdown response}
|
8
|
+
s.authors = ["Vincenzo Farruggia"]
|
9
|
+
s.email = 'mastropinguino@networky.net'
|
10
|
+
s.files = `git ls-files`.split($/)
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.require_paths = ['lib']
|
13
|
+
s.add_runtime_dependency 'rack'
|
14
|
+
s.homepage =
|
15
|
+
'https://github.com/mastropinguino/rack-delay'
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-delay
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vincenzo Farruggia
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A simple rack middleware to slowdown response
|
28
|
+
email: mastropinguino@networky.net
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- README.md
|
35
|
+
- lib/rack/delay.rb
|
36
|
+
- rack-delay.gemspec
|
37
|
+
homepage: https://github.com/mastropinguino/rack-delay
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.0.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: rack-delay v0.2.0
|
61
|
+
test_files: []
|