-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace AppSec rate limiter with core rate limiter
This allows us to be more precise in throttling outgoing AppSec traces and removes additional custom logic of rate limiting. Also RBS definitions of rate limiters are updated
- Loading branch information
Showing
5 changed files
with
102 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
module Datadog | ||
module AppSec | ||
class RateLimiter | ||
type timestamp = ::Float | ||
type rate = ::Integer | ||
@rate_limiter: Core::TokenBucket | ||
|
||
@rate: ::Integer | ||
@timestamps: ::Array[timestamp] | ||
TRACES_KEY: :datadog_security_traces_rate_limiter | ||
|
||
def initialize: (rate rate) -> void | ||
def self.limit: (::Symbol name) { (?) -> untyped } -> untyped | ||
|
||
# TODO: return type of limit is return type of block | ||
def limit: () { () -> untyped } -> untyped | ||
|
||
def self.limit: (::Symbol name) { () -> untyped } -> untyped | ||
def self.reset!: (::Symbol name) -> untyped | ||
|
||
def self.reset!: (::Symbol name) -> void | ||
private | ||
|
||
def self.rate_limiter: (::Symbol name) -> RateLimiter | ||
|
||
def self.trace_rate_limit: () -> rate | ||
def self.trace_rate_limit: () -> untyped | ||
|
||
public | ||
|
||
def initialize: (::Integer rate) -> void | ||
|
||
def limit: () { () -> untyped } -> untyped | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'datadog/appsec/spec_helper' | ||
require 'datadog/appsec/rate_limiter' | ||
|
||
RSpec.describe Datadog::AppSec::RateLimiter do | ||
before { described_class.reset!(:traces) } | ||
|
||
describe '#limit' do | ||
context 'in different threads' do | ||
before { stub_const("#{described_class}::TRACES_KEY", :__spec_traces) } | ||
|
||
it 'creates separate rate limiter per thread' do | ||
thread_1 = Thread.new do | ||
described_class.limit(:traces) { 'workload' } | ||
end | ||
|
||
thread_2 = Thread.new do | ||
described_class.limit(:traces) { 'workload' } | ||
end | ||
|
||
thread_1.join | ||
thread_2.join | ||
|
||
rate_limiter_1 = thread_1.thread_variable_get(:__spec_traces) | ||
rate_limiter_2 = thread_2.thread_variable_get(:__spec_traces) | ||
|
||
expect(rate_limiter_1.object_id).not_to eq(rate_limiter_2.object_id) | ||
end | ||
end | ||
end | ||
end |