Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some problems #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/rack/redis_throttle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
require 'redis'
require 'hiredis'
require 'redis-namespace'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/time/calculations'
require 'active_support/core_ext/date/calculations'

module Rack
module RedisThrottle
Expand Down
6 changes: 3 additions & 3 deletions lib/rack/redis_throttle/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module RedisThrottle
class Connection

def self.create(options={})
url = redis_provider || 'redis://localhost:6379/0'
options.reverse_merge!({ url: url })
client = Redis.connect(url: options[:url], driver: :hiredis)
options[:url] = redis_provider || 'redis://localhost:6379/0' unless options.has_key?(:url)
method = Redis::VERSION.to_i >= 3 ? :new : :connect
client = Redis.send(method, url: options[:url], driver: :hiredis)
Redis::Namespace.new("redis-throttle:#{ENV['RACK_ENV']}:rate", redis: client)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rack/redis_throttle/limiter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module RedisThrottle
class Limiter < Rack::Throttle::Limiter

def initialize(app, options = {})
options.reverse_merge!({ cache: Rack::RedisThrottle::Connection.create })
options[:cache] = Rack::RedisThrottle::Connection.create(options) unless options.has_key?(:cache)
@app, @options = app, options
end

Expand Down Expand Up @@ -53,7 +53,7 @@ def cache_incr(request)
begin
key = cache_key(request)
count = cache.incr(key)
cache.expire(key, 1.day) if count == 1
cache.expire(key, 86400) if count == 1
count
rescue Redis::BaseConnectionError => e
puts "ERROR: Redis connection not available. Rescuing cache.incr(key)" if ENV['DEBUG']
Expand Down
2 changes: 1 addition & 1 deletion redis_throttle.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'redis'
gem.add_dependency 'hiredis'
gem.add_dependency 'redis-namespace'
gem.add_dependency 'activesupport'

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
Expand All @@ -45,4 +44,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'guard-rspec'
gem.add_development_dependency 'fuubar'
gem.add_development_dependency 'growl'
gem.add_development_dependency 'activesupport'
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'mock_redis'
require 'rspec'
require 'timecop'
require 'active_support/core_ext/time/calculations'

require File.dirname(__FILE__) + '/fixtures/fake_app'

Expand Down