gimme_a_break 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
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/Manifest ADDED
@@ -0,0 +1,6 @@
1
+ MIT-LICENSE
2
+ README.rdoc
3
+ Rakefile
4
+ init.rb
5
+ lib/gimme_a_break.rb
6
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,51 @@
1
+ = Gimme A Break
2
+
3
+ This plugin adds a way to throttle ActiveRecord creation.
4
+
5
+ Add +gimme_a_break+ to your model to enable the throttling.
6
+ It will then not be possible to create models in quick succession.
7
+ Object creation is usually very "expensive" (in time) and blocks most
8
+ database servers. Database IO is usually the bottleneck
9
+ in web applications, and thottling this back is a great way to
10
+ increase overall availability.
11
+
12
+ An error message will be added to the base object and the object
13
+ will not be valid. It's up to you to display this error to the user.
14
+ When you follow ActiveRecord convention you will be fine. The error
15
+ message can be localized using the key <tt>gimme_a_break.notice</tt> with
16
+ <tt>{{count}}</tt> as the timeout in seconds.
17
+
18
+ The throttle is applied per running instance. If you have two Passenger
19
+ instances running, people will be able to create twice the amount
20
+ of objects. This is exactly what you want, since the limit should scale
21
+ linearly with the capacity of your servers. If your database keeps
22
+ getting overloaded, reduce the timeout.
23
+
24
+
25
+ == Options
26
+
27
+ +timeout+: the time to wait between creates, in seconds. Default is 6.
28
+
29
+
30
+ == Prerequisites
31
+
32
+ Your model needs to have a created_at column for this to work.
33
+
34
+
35
+ == Installation
36
+
37
+ ruby script/plugin install git://github.com/tilsammans/gimme_a_break
38
+
39
+
40
+ == Examples
41
+
42
+ class Comment < ActiveRecord::Base
43
+ gimme_a_break
44
+ end
45
+
46
+ class Tweet < ActiveRecord::Base
47
+ gimme_a_break :timeout => 2
48
+ end
49
+
50
+
51
+ Copyright (c) 2010 Joost Baaij, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'psych'
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'echoe'
5
+
6
+ Echoe.new('gimme_a_break', '0.1.0') do |p|
7
+ p.summary = "Throttle ActiveRecord creation"
8
+ p.url = "http://github.com/tilsammans/gimme_a_break"
9
+ p.author = "Joost Baaij"
10
+ p.email = "joost@spacebabies.nl"
11
+ p.development_dependencies = []
12
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{gimme_a_break}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = [%q{Joost Baaij}]
9
+ s.date = %q{2011-07-20}
10
+ s.description = %q{Throttle ActiveRecord creation}
11
+ s.email = %q{joost@spacebabies.nl}
12
+ s.extra_rdoc_files = [%q{README.rdoc}, %q{lib/gimme_a_break.rb}]
13
+ s.files = [%q{MIT-LICENSE}, %q{README.rdoc}, %q{Rakefile}, %q{init.rb}, %q{lib/gimme_a_break.rb}, %q{Manifest}, %q{gimme_a_break.gemspec}]
14
+ s.homepage = %q{http://github.com/tilsammans/gimme_a_break}
15
+ s.rdoc_options = [%q{--line-numbers}, %q{--inline-source}, %q{--title}, %q{Gimme_a_break}, %q{--main}, %q{README.rdoc}]
16
+ s.require_paths = [%q{lib}]
17
+ s.rubyforge_project = %q{gimme_a_break}
18
+ s.rubygems_version = %q{1.8.5}
19
+ s.summary = %q{Throttle ActiveRecord creation}
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'gimme_a_break'
@@ -0,0 +1,30 @@
1
+ # This plugin adds a way to throttle ActiveRecord creation.
2
+ # Add +gimme_a_break+ to your model to enable the throttling.
3
+ # It will then not be possible to create models in quick succession.
4
+ # The error message will be added to the base object, and can be
5
+ # localized using the key +gimme_a_break.notice+.
6
+ module GimmeABreak
7
+ protected
8
+ def gimme_a_break
9
+ if last_created && last_created.created_at > self.class.gimme_a_break_timeout.seconds.ago
10
+ errors.add_to_base I18n.t("gimme_a_break.notice", :default => "Temporarily disabled due to a safety measure. Please try again in %{count} seconds", :count => self.class.gimme_a_break_timeout)
11
+ end
12
+ end
13
+
14
+ def last_created
15
+ self.class.find(:first, :order => 'created_at desc')
16
+ end
17
+ end
18
+
19
+ # Reopening ActiveRecord::Base not bad, see http://yehudakatz.com/2009/11/12/better-ruby-idioms/
20
+ class ActiveRecord::Base
21
+ def self.gimme_a_break(options ={})
22
+ cattr_accessor :gimme_a_break_timeout
23
+ self.gimme_a_break_timeout = (options[:timeout] || 6)
24
+ include GimmeABreak
25
+
26
+ class_eval <<-EOV
27
+ validate :gimme_a_break
28
+ EOV
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gimme_a_break
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joost Baaij
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-20 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Throttle ActiveRecord creation
15
+ email: joost@spacebabies.nl
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ - lib/gimme_a_break.rb
21
+ files:
22
+ - MIT-LICENSE
23
+ - README.rdoc
24
+ - Rakefile
25
+ - init.rb
26
+ - lib/gimme_a_break.rb
27
+ - Manifest
28
+ - gimme_a_break.gemspec
29
+ homepage: http://github.com/tilsammans/gimme_a_break
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --line-numbers
34
+ - --inline-source
35
+ - --title
36
+ - Gimme_a_break
37
+ - --main
38
+ - README.rdoc
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '1.2'
53
+ requirements: []
54
+ rubyforge_project: gimme_a_break
55
+ rubygems_version: 1.8.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Throttle ActiveRecord creation
59
+ test_files: []