ratelimit-ruby 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cf58e13eb3d3e2371dc2dd10833cf597d2ab108
4
- data.tar.gz: 24eb31d4a576763298d489bfe6d11454f7c5eb8e
3
+ metadata.gz: 2f3b746eec2993e0825020e4c703de39a6a3458a
4
+ data.tar.gz: e49e70c0153e2a0c5aa5d8f8a87382e4664ed412
5
5
  SHA512:
6
- metadata.gz: df86c38c7dd3b1c57f7ed345e245aac1b9962df2f831e2daab790b3c34bae842e9e75bd0e85d08c435e61c516a3f7ff94aba56283d5370cf380c8b464ae83e50
7
- data.tar.gz: fcd2f9261d6c38bfc779055baac475b26e3a8fa5fe9d88f7a68aa7e28690f850286137ef0cd28c96fd32e9ad08c1ab711727971aec5ba5377ee115fe41fb5a0a
6
+ metadata.gz: 9f18859ffe8e2805dce9597d79c6ee3b9f50a8a642b520e4bd22ef95a10750b5ed3324404ec377a6e72a16506bc20a31fcf50abfb0abc64a9e1a566eccba0354
7
+ data.tar.gz: 7435ee478458b536a662854e9f99c15eee732b739ce1cfebba7330911bab8b2613fc6627e43cc601d442d58ddba1b68cf257a3f50c8d7d5cb3025fe812c7376a
data/README.md CHANGED
@@ -3,13 +3,13 @@
3
3
  Rate Limit your Ruby app using http://www.ratelim.it
4
4
 
5
5
  ```ruby
6
- limiter = RateLimit::Limiter.new(apikey: "APIKEY", account_id: ACCT_ID)
6
+ limiter = RateLimit::Limiter.new(apikey: "ACCT_ID|APIKEY")
7
7
 
8
8
  # only need to do this on startup
9
9
  # limit to 1 per hour
10
- limiter.upsert_limit(Limit.new("pageload", 1, RateLimIt::HOURLY_ROLLING))
10
+ limiter.upsert_limit("pageload", 1, RateLimIt::HOURLY_ROLLING)
11
11
 
12
- if limiter.check?("pageload")
12
+ if limiter.pass?("pageload")
13
13
  do_hourly_thing()
14
14
  end
15
15
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,4 +1,6 @@
1
1
  module RateLimit
2
+ WAIT_INCR_MAX = 0.5
3
+
2
4
  class WaitExceeded < StandardError
3
5
  end
4
6
 
@@ -7,24 +9,36 @@ module RateLimit
7
9
  local ? 'http://localhost:8080' : 'http://www.ratelim.it'
8
10
  end
9
11
 
10
- def initialize(apikey:, account_id:, local: false, debug: false)
12
+ def initialize(apikey:, local: false, debug: false)
11
13
  @conn = Faraday.new(:url => self.base_url(local)) do |faraday|
12
14
  faraday.request :json # form-encode POST params
13
15
  faraday.response :logger if debug
14
16
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
15
17
  end
16
- @conn.basic_auth(account_id, apikey)
18
+ (username, pass) = apikey.split("|")
19
+ @conn.basic_auth(username, pass)
20
+ end
21
+
22
+
23
+ def create_concurrency_limit(group, concurrent, timeout_seconds)
24
+ upsert_concurrency_limit(group, concurrent, timeout_seconds, method: :post)
25
+ end
26
+
27
+ def upsert_concurrency_limit(group, concurrent, timeout_seconds, method: :put)
28
+ recharge_rate = (24*60*60)/timeout_seconds
29
+ recharge_policy = DAILY_ROLLING
30
+ upsert(LimitDefinition.new(group, recharge_rate, recharge_policy, true, concurrent), method)
17
31
  end
18
32
 
19
- def upsert_semaphore(group, limit, policy)
20
- upsert(LimitDefinition.new(group, limit, policy, true))
33
+ def create_limit(group, limit, policy, burst: nil)
34
+ upsert(LimitDefinition.new(group, limit, policy, false, burst || limit), :post)
21
35
  end
22
36
 
23
- def upsert_limit(group, limit, policy)
24
- upsert(LimitDefinition.new(group, limit, policy, false))
37
+ def upsert_limit(group, limit, policy, burst: nil)
38
+ upsert(LimitDefinition.new(group, limit, policy, false, burst || limit), :put)
25
39
  end
26
40
 
27
- def check?(group)
41
+ def pass?(group)
28
42
  result = acquire(group, 1)
29
43
  return result.passed
30
44
  end
@@ -47,7 +61,7 @@ module RateLimit
47
61
  if res.passed
48
62
  return res
49
63
  end
50
- sleep += rand
64
+ sleep += rand * WAIT_INCR_MAX
51
65
  end
52
66
  raise RateLimit::WaitExceeded
53
67
  end
@@ -57,17 +71,18 @@ module RateLimit
57
71
  @conn.post '/api/v1/limitreturn',
58
72
  { enforcedGroup: limit_result.enforcedGroup,
59
73
  amount: limit_result.amount }.to_json
60
- puts result.body
74
+ puts "RETURN #{result.body} #{result.status}"
61
75
  end
62
76
 
63
77
  private
64
78
 
65
- def upsert(limit_definition)
79
+ def upsert(limit_definition, method)
66
80
  to_send = { limit: limit_definition.limit,
67
81
  group: limit_definition.group,
82
+ burst: limit_definition.burst,
68
83
  policyName: limit_definition.policy,
69
84
  returnable: limit_definition.returnable }.to_json
70
- result= @conn.post '/api/v1/limits', to_send
85
+ result= @conn.send(method, '/api/v1/limits', to_send)
71
86
  end
72
87
  end
73
88
 
@@ -11,14 +11,15 @@ module RateLimit
11
11
  POLICIES = [SECONDLY, MINUTELY, MINUTELY_ROLLING, HOURLY, HOURLY_ROLLING, DAILY, DAILY_ROLLING, MINUTELY, INFINITE]
12
12
 
13
13
  class LimitDefinition
14
- attr_reader :limit, :group, :policy, :returnable
14
+ attr_reader :limit, :group, :policy, :returnable, :burst
15
15
 
16
- def initialize(group, limit, policy, returnable)
16
+ def initialize(group, limit, policy, returnable, burst)
17
17
  raise "Invalid Policy" unless POLICIES.include? policy
18
18
  @limit = limit
19
19
  @group = group
20
20
  @policy = policy
21
- @returnable= returnable
21
+ @returnable = returnable
22
+ @burst = burst
22
23
  end
23
24
  end
24
25
  end
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: ratelimit-ruby 0.1.0 ruby lib
5
+ # stub: ratelimit-ruby 0.1.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "ratelimit-ruby"
9
- s.version = "0.1.0"
9
+ s.version = "0.1.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Jeff Dwyer"]
14
- s.date = "2016-11-18"
14
+ s.date = "2016-12-13"
15
15
  s.description = "rate limit your ruby"
16
16
  s.email = "jdwyah@gmail.com"
17
17
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ratelimit-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dwyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-18 00:00:00.000000000 Z
11
+ date: 2016-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday