flood 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +6 -0
- data/LICENSE +20 -0
- data/README.rdoc +75 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/flood.gemspec +49 -0
- data/lib/flood.rb +105 -0
- data/test/flood_test.rb +99 -0
- metadata +64 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Neil Kohl
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
= flood
|
2
|
+
|
3
|
+
Flood is a Ruby library to limit events to a count/time ratio.
|
4
|
+
|
5
|
+
= Installation
|
6
|
+
|
7
|
+
Install the gem:
|
8
|
+
|
9
|
+
gem install flood
|
10
|
+
|
11
|
+
When requiring, use 'flood' as the gem name:
|
12
|
+
|
13
|
+
require 'flood'
|
14
|
+
|
15
|
+
= Introduction
|
16
|
+
|
17
|
+
There are many situations when it's desirable to limit events that occur in a
|
18
|
+
given time period. This library provides a framework for implementing rate
|
19
|
+
limits.
|
20
|
+
|
21
|
+
Given a time interval and a maximum number of events that can occur in that
|
22
|
+
period, the library determines whether the event should be permitted.
|
23
|
+
|
24
|
+
= Storage
|
25
|
+
|
26
|
+
If you want flood control to persist across invocations of a script you must
|
27
|
+
save the data. The storage method returns the flood data; you have to handle
|
28
|
+
storage. To load stored flood data, call the storage= method with the flood
|
29
|
+
data as a parameter.
|
30
|
+
|
31
|
+
== Examples
|
32
|
+
|
33
|
+
A monitoring script should send no more than alert 1 email every 10 minutes.
|
34
|
+
|
35
|
+
email_limit = 1
|
36
|
+
email_interval = 10 * 60 # interval in seconds
|
37
|
+
flood = FloodControl.new(email_limit, email_interval)
|
38
|
+
|
39
|
+
if (flood.check() == 0)
|
40
|
+
send_email
|
41
|
+
end
|
42
|
+
|
43
|
+
A form should only accept 5 submissions from a single IP address every hour.
|
44
|
+
|
45
|
+
submit_limit = 5
|
46
|
+
submit_interval = 60 * 60 # interval in seconds
|
47
|
+
flood = FloodControl.new(submit_limit, submit_interval)
|
48
|
+
|
49
|
+
if (flood.check(ip_address) == 0)
|
50
|
+
send_email
|
51
|
+
else
|
52
|
+
show_sorry_page
|
53
|
+
end
|
54
|
+
|
55
|
+
Users have varying limits based on group.
|
56
|
+
|
57
|
+
TODO: Variable limits example
|
58
|
+
|
59
|
+
Save and load flood data for persistence across invocations.
|
60
|
+
|
61
|
+
TODO: Persistence example
|
62
|
+
|
63
|
+
|
64
|
+
== Note on Patches/Pull Requests
|
65
|
+
|
66
|
+
* Fork the project.
|
67
|
+
* Make your feature addition or bug fix.
|
68
|
+
* Add tests for it. This is important so I don't break it in a
|
69
|
+
future version unintentionally.
|
70
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
71
|
+
* Send me a pull request.
|
72
|
+
|
73
|
+
== Copyright
|
74
|
+
|
75
|
+
Copyright (c) 2009 Neil Kohl. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "flood"
|
8
|
+
gem.summary = %Q{Ruby library to implement flood control}
|
9
|
+
gem.description = %Q{Flood is a Ruby library for flood control. Flood control is limiting events processed to a maximum number in a specified time period.}
|
10
|
+
gem.email = "neil@kohlweb.com"
|
11
|
+
gem.homepage = "http://github.com/nedski/flood"
|
12
|
+
gem.authors = ["Neil Kohl"]
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
desc "run test suite"
|
22
|
+
Rake::TestTask.new("test") {|t|
|
23
|
+
t.pattern = 'test/*_test.rb'
|
24
|
+
t.verbose = true
|
25
|
+
t.warning = true
|
26
|
+
}
|
27
|
+
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/*_test.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
task :default => :test
|
46
|
+
|
47
|
+
require 'rake/rdoctask'
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
if File.exist?('VERSION')
|
50
|
+
version = File.read('VERSION')
|
51
|
+
else
|
52
|
+
version = ""
|
53
|
+
end
|
54
|
+
|
55
|
+
rdoc.rdoc_dir = 'rdoc'
|
56
|
+
rdoc.title = "flood #{version}"
|
57
|
+
rdoc.rdoc_files.include('README*')
|
58
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
59
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/flood.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{flood}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Neil Kohl"]
|
12
|
+
s.date = %q{2010-02-14}
|
13
|
+
s.description = %q{Flood is a Ruby library for flood control. Flood control is limiting events processed to a maximum number in a specified time period.}
|
14
|
+
s.email = %q{neil@kohlweb.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"flood.gemspec",
|
27
|
+
"lib/flood.rb",
|
28
|
+
"test/flood_test.rb"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/nedski/flood}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.3.5}
|
34
|
+
s.summary = %q{Ruby library to implement flood control}
|
35
|
+
s.test_files = [
|
36
|
+
"test/flood_test.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
44
|
+
else
|
45
|
+
end
|
46
|
+
else
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
data/lib/flood.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
|
2
|
+
# Provides a means to limit events to a count/time
|
3
|
+
# ratio. This is a more or less straight port of the Perl
|
4
|
+
# Algorithm::FloodControl library by Vladi Belperchinov-Shabanski.
|
5
|
+
#
|
6
|
+
# See README for more information and examples of use.
|
7
|
+
class FloodControl
|
8
|
+
|
9
|
+
attr_accessor :interval, :flood
|
10
|
+
|
11
|
+
attr_reader :max_events
|
12
|
+
|
13
|
+
def initialize(max_events, interval)
|
14
|
+
@flood = Hash.new
|
15
|
+
@max_events = max_events
|
16
|
+
@interval = interval
|
17
|
+
end
|
18
|
+
|
19
|
+
# Reset the event count for a given event, or if no argument
|
20
|
+
# supplied reset the event count for all events.
|
21
|
+
def reset(event=nil)
|
22
|
+
if event.nil?
|
23
|
+
@flood.clear
|
24
|
+
else
|
25
|
+
@flood.delete(event)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get/set the maximum number of events. Setting will truncate
|
30
|
+
# all event queues so they're no longer than max_events.
|
31
|
+
def max_events=(size)
|
32
|
+
old_max_events = @max_events
|
33
|
+
@max_events=size
|
34
|
+
|
35
|
+
if (old_max_events > @max_events)
|
36
|
+
# New event queue is shorter than old;
|
37
|
+
# trim long event queues if needed
|
38
|
+
@flood.each do |e, v|
|
39
|
+
v = v[0..@max_events - 1] if v.length > @max_events
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Check if an event can proceed.
|
45
|
+
#
|
46
|
+
# If no event id is given as an argument, the name of the
|
47
|
+
# caller is used.
|
48
|
+
#
|
49
|
+
# max_events and interval can be overridden on a per-call basis.
|
50
|
+
#
|
51
|
+
# The return value is 0 if event can proceed, or a positive integer
|
52
|
+
# if the limit has been exceeded. The value represents the number
|
53
|
+
# of seconds to wait so the event will occur within the limit set.
|
54
|
+
def check(event='',max_events=@max_events, interval=@interval)
|
55
|
+
|
56
|
+
#provide event key if not supplied
|
57
|
+
if (event == '')
|
58
|
+
# TEST: is this unique?
|
59
|
+
event = caller[0].gsub(/\s/,'')
|
60
|
+
# print STDERR "EN: $en\n";
|
61
|
+
end
|
62
|
+
|
63
|
+
# make empty flood array for this event key
|
64
|
+
@flood[event] ||= Array.new;
|
65
|
+
|
66
|
+
event_count = @flood[event].length;
|
67
|
+
|
68
|
+
if( event_count >= max_events )
|
69
|
+
# flood array has enough events to do real flood check
|
70
|
+
ot = @flood[event][0]; # oldest event timestamp in the flood array
|
71
|
+
tp = Time.now.to_i - ot; # time period between current and oldest event
|
72
|
+
|
73
|
+
# now calculate time in seconds until next allowed event
|
74
|
+
wait = ot + ( event_count * interval / max_events ) - Time.now.to_i
|
75
|
+
if( wait > 0 )
|
76
|
+
# positive number of seconds means flood in progress
|
77
|
+
# event_count should be rejected or postponed
|
78
|
+
# print "WARNING: next event will be allowed in $wait seconds\n";
|
79
|
+
return wait;
|
80
|
+
end
|
81
|
+
|
82
|
+
# negative or 0 seconds means that event should be accepted
|
83
|
+
# oldest event is removed from the flood array
|
84
|
+
@flood[event].shift;
|
85
|
+
|
86
|
+
end
|
87
|
+
# flood array is not full or oldest event is already removed
|
88
|
+
# so current event has to be added
|
89
|
+
|
90
|
+
@flood[event].push(Time.now.to_i);
|
91
|
+
# event is ok
|
92
|
+
return 0
|
93
|
+
end
|
94
|
+
|
95
|
+
# Get flood data
|
96
|
+
def storage
|
97
|
+
@flood
|
98
|
+
end
|
99
|
+
|
100
|
+
# Set flood data
|
101
|
+
def storage=(flood)
|
102
|
+
@flood=flood
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
data/test/flood_test.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# run test suite
|
4
|
+
require 'test/unit'
|
5
|
+
require 'flood'
|
6
|
+
|
7
|
+
class TestFlood < Test::Unit::TestCase
|
8
|
+
|
9
|
+
EVENT_LIMIT = 15
|
10
|
+
EXTRA_EVENTS = 5
|
11
|
+
TIME_LIMIT = 10
|
12
|
+
EXTRA_TIME = 5
|
13
|
+
|
14
|
+
def setup
|
15
|
+
@t = FloodControl.new(EVENT_LIMIT, TIME_LIMIT)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_basic
|
19
|
+
|
20
|
+
EVENT_LIMIT.times do
|
21
|
+
assert_equal(0, @t.check('test'))
|
22
|
+
end
|
23
|
+
assert(@t.check('test') > 0, "Should return wait > 0")
|
24
|
+
|
25
|
+
sleep TIME_LIMIT
|
26
|
+
assert_equal(0, @t.check('test'))
|
27
|
+
|
28
|
+
assert_not_nil(@t.storage)
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_call_with_args
|
33
|
+
@t.reset
|
34
|
+
|
35
|
+
EVENT_LIMIT.times do
|
36
|
+
assert_equal(0, @t.check('test'))
|
37
|
+
end
|
38
|
+
assert(@t.check('test') > 0, "Should return wait > 0")
|
39
|
+
|
40
|
+
assert_equal(0, @t.check('test', EVENT_LIMIT + 1, TIME_LIMIT))
|
41
|
+
assert(@t.check('test', EVENT_LIMIT - 1, TIME_LIMIT) > 0,
|
42
|
+
"Should return wait > 0")
|
43
|
+
|
44
|
+
sleep TIME_LIMIT
|
45
|
+
assert_equal(0, @t.check('test'))
|
46
|
+
|
47
|
+
assert_not_nil(@t.storage)
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_reset_max
|
52
|
+
@t.reset
|
53
|
+
|
54
|
+
# test event
|
55
|
+
EVENT_LIMIT.times do
|
56
|
+
assert_equal(0, @t.check('test'))
|
57
|
+
end
|
58
|
+
assert(@t.check('test') != 0, "Should return wait > 0")
|
59
|
+
|
60
|
+
# change limit
|
61
|
+
new_limit = EVENT_LIMIT + EXTRA_EVENTS
|
62
|
+
assert_equal(new_limit, @t.max_events=new_limit)
|
63
|
+
EXTRA_EVENTS.times do
|
64
|
+
assert_equal(0, @t.check('test'))
|
65
|
+
end
|
66
|
+
assert(@t.check('test') != 0, "Should return wait > 0")
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_reset_time
|
71
|
+
@t.reset
|
72
|
+
|
73
|
+
# test event
|
74
|
+
EVENT_LIMIT.times do
|
75
|
+
assert_equal(0, @t.check('test'))
|
76
|
+
end
|
77
|
+
sleep TIME_LIMIT + EXTRA_TIME
|
78
|
+
assert_equal(0, @t.check('test'))
|
79
|
+
|
80
|
+
# change time
|
81
|
+
assert_equal(TIME_LIMIT + EXTRA_TIME, @t.interval=TIME_LIMIT + EXTRA_TIME)
|
82
|
+
assert_equal(0, @t.check('test'))
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_generated_key
|
87
|
+
@t.reset
|
88
|
+
|
89
|
+
# test
|
90
|
+
EVENT_LIMIT + 1.times do |c|
|
91
|
+
c < EVENT_LIMIT ? assert_equal(0, @t.check()) : assert(@t.check() != 0,
|
92
|
+
"Should return wait > 0")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# TODO: Test store/restore flood data
|
97
|
+
|
98
|
+
end
|
99
|
+
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flood
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Neil Kohl
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-14 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Flood is a Ruby library for flood control. Flood control is limiting events processed to a maximum number in a specified time period.
|
17
|
+
email: neil@kohlweb.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- flood.gemspec
|
33
|
+
- lib/flood.rb
|
34
|
+
- test/flood_test.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/nedski/flood
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.5
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Ruby library to implement flood control
|
63
|
+
test_files:
|
64
|
+
- test/flood_test.rb
|