bozos_buckets 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0d3bb1c894e1ca2de1e7af0ddd3caa4f811714b26d3d4c2167d29f244912c0d8
4
+ data.tar.gz: ea3fb4defd1bafe75f0f19027d731f20864ff677856bab6bee24f5f12a3a6b33
5
+ SHA512:
6
+ metadata.gz: 3094d1293de5d27a9400d2dabe1c040f0d897f61d1f7e2920590e00e117f7d305e640f85f87bb318b1b4da9a925a5f8748ae390b2db506dc36d119f1e31ab685
7
+ data.tar.gz: 03d6d2bf6e953233c5e71ce2d433138333ce9c78253afdf53020b4eb4e75a12a687b394be49c339d152e1bda20d852e881a51fd7483c3a50bc7d5a850a855d4a
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Top level module for BozosBuckets
4
+ module BozosBuckets
5
+ # Version constant
6
+ VERSION = '0.0.1'
7
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './bozos_buckets/version'
4
+
5
+ module BozosBuckets
6
+
7
+ # Class representing a token bucket
8
+ class Bucket
9
+ attr_reader :current_token_count, :refill_rate, :max_token_count, :last_refilled
10
+
11
+ # Constructs a Bucket instance
12
+ #
13
+ # @param initial_token_count [Integer] the number of tokens the bucket
14
+ # starts with
15
+ # @param refill_rate [Double] How many tokens per second should be added to
16
+ # the bucket. For example, 1 would be 1 token per second. 0.1 would be one
17
+ # token per 10 seconds
18
+ # @param max_token_count [Integer] the maximum number of tokens the bucket
19
+ # can hold. Defaults to the initial_token_count if not provided
20
+ # @return An instance of BozosBucket
21
+ def initialize(initial_token_count: 100, refill_rate: 1, max_token_count: 100)
22
+ @current_token_count = initial_token_count
23
+ @refill_rate = refill_rate
24
+ @max_token_count = max_token_count || initial_token_count
25
+ reset_last_refilled
26
+ end
27
+
28
+ # Attempt to use tokens from the bucket. If there are sufficient
29
+ # tokens, @current_token_count is decremented by the `count` and method
30
+ # returns `true`. If there are not sufficient tokens, method returns
31
+ # false without changing the @current_token_count
32
+ #
33
+ # @param count [Integer] Number of tokens that should be used
34
+ # @return [Boolean] Whether there were sufficient tokens when called
35
+ def use_tokens(count: 1)
36
+ refill_bucket
37
+
38
+ if (@current_token_count - count) >= 0
39
+ @current_token_count -= count
40
+ return true
41
+ else
42
+ return false
43
+ end
44
+ end
45
+
46
+ # Determines how many seconds have passed since the last time the bucket
47
+ # was used, calculates how many tokens should be added to the bucket, and
48
+ # adds them by updating @current_token_count
49
+ #
50
+ # @return [Integer] the current_token_count after refilling the bucket
51
+ def refill_bucket
52
+ elapsed_seconds = Time.now.to_i - last_refilled
53
+ tokens_to_add = (elapsed_seconds * refill_rate).floor
54
+
55
+ reset_last_refilled
56
+
57
+ @current_token_count = [@current_token_count + tokens_to_add, max_token_count].min
58
+ end
59
+
60
+ private
61
+
62
+ # Resets the last_refilled timestamp to the current time. Called by
63
+ # {#refill_bucket} after calculating elapsed time
64
+ #
65
+ # @return [Integer] last used timestamp in Unix epoch format. Should
66
+ # always be the current time
67
+ def reset_last_refilled
68
+ @last_refilled = Time.now.to_i
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bozos_buckets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Glover
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |2
42
+ A low overhead implementation of a token bucket for rate limiting.
43
+ Does not use an array or linked list, and therefore has a tiny memory footprint
44
+ email: alexdglover@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/bozos_buckets.rb
50
+ - lib/bozos_buckets/version.rb
51
+ homepage:
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.5.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.2
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: A low overhead implementation of a token bucket
74
+ test_files: []