Skip to content

Commit

Permalink
Merge pull request #422 from grzuy/discriminator_in_throttle_data
Browse files Browse the repository at this point in the history
feat: provide discriminator in throttle_data
  • Loading branch information
grzuy authored Jul 11, 2019
2 parents d1b71da + 58ba650 commit 50fde1e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ end
For responses that did not exceed a throttle limit, Rack::Attack annotates the env with match data:

```ruby
request.env['rack.attack.throttle_data'][name] # => { :count => n, :period => p, :limit => l, :epoch_time => t }
request.env['rack.attack.throttle_data'][name] # => { discriminator: d, count: n, period: p, limit: l, epoch_time: t }
```

## Logging & Instrumentation
Expand Down
1 change: 1 addition & 0 deletions lib/rack/attack/throttle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def matched_by?(request)
epoch_time = cache.last_epoch_time

data = {
discriminator: discriminator,
count: count,
period: current_period,
limit: current_limit,
Expand Down
37 changes: 33 additions & 4 deletions spec/rack_attack_throttle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
end

it 'should populate throttle data' do
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
data = {
count: 1,
limit: 1,
period: @period,
epoch_time: Rack::Attack.cache.last_epoch_time.to_i,
discriminator: "1.2.3.4"
}

last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
end
end
Expand All @@ -39,7 +46,15 @@
it 'should tag the env' do
last_request.env['rack.attack.matched'].must_equal 'ip/sec'
last_request.env['rack.attack.match_type'].must_equal :throttle
last_request.env['rack.attack.match_data'].must_equal(count: 2, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i)

last_request.env['rack.attack.match_data'].must_equal(
count: 2,
limit: 1,
period: @period,
epoch_time: Rack::Attack.cache.last_epoch_time.to_i,
discriminator: "1.2.3.4"
)

last_request.env['rack.attack.match_discriminator'].must_equal('1.2.3.4')
end

Expand Down Expand Up @@ -67,7 +82,14 @@
end

it 'should populate throttle data' do
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
data = {
count: 1,
limit: 1,
period: @period,
epoch_time: Rack::Attack.cache.last_epoch_time.to_i,
discriminator: "1.2.3.4"
}

last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
end
end
Expand All @@ -91,7 +113,14 @@
end

it 'should populate throttle data' do
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
data = {
count: 1,
limit: 1,
period: @period,
epoch_time: Rack::Attack.cache.last_epoch_time.to_i,
discriminator: "1.2.3.4"
}

last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
end
end
Expand Down

0 comments on commit 50fde1e

Please sign in to comment.