Class: LightrateClient::Client
- Inherits:
-
Object
- Object
- LightrateClient::Client
- Defined in:
- lib/lightrate_client/client.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#token_buckets ⇒ Object
readonly
Returns the value of attribute token_buckets.
Instance Method Summary collapse
-
#consume_local_bucket_token(operation: nil, path: nil, http_method: nil, user_identifier:) ⇒ Object
Consume tokens by operation or path using local bucket.
- #consume_token_from_bucket(bucket, provided_tokens = 0) ⇒ Object
- #consume_tokens(operation: nil, path: nil, http_method: nil, user_identifier:, tokens_requested:) ⇒ Object
-
#initialize(api_key = nil, application_id = nil, options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(api_key = nil, application_id = nil, options = {}) ⇒ Client
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lightrate_client/client.rb', line 12 def initialize(api_key = nil, application_id = nil, = {}) if api_key # Create a new configuration with the provided API key and application ID @configuration = LightrateClient::Configuration.new.tap do |c| c.api_key = api_key c.application_id = application_id c.timeout = [:timeout] || LightrateClient.configuration.timeout c.retry_attempts = [:retry_attempts] || LightrateClient.configuration.retry_attempts c.logger = [:logger] || LightrateClient.configuration.logger c.default_local_bucket_size = [:default_local_bucket_size] || LightrateClient.configuration.default_local_bucket_size end else @configuration = .is_a?(LightrateClient::Configuration) ? : LightrateClient.configuration end validate_configuration! setup_connection setup_token_buckets end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
10 11 12 |
# File 'lib/lightrate_client/client.rb', line 10 def configuration @configuration end |
#token_buckets ⇒ Object (readonly)
Returns the value of attribute token_buckets.
10 11 12 |
# File 'lib/lightrate_client/client.rb', line 10 def token_buckets @token_buckets end |
Instance Method Details
#consume_local_bucket_token(operation: nil, path: nil, http_method: nil, user_identifier:) ⇒ Object
Consume tokens by operation or path using local bucket
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/lightrate_client/client.rb', line 39 def consume_local_bucket_token(operation: nil, path: nil, http_method: nil, user_identifier:) # Synchronize the entire process to prevent race conditions # First, try to find an existing bucket that matches this request bucket = find_bucket_by_matcher(user_identifier, operation, path, http_method) if bucket && bucket.check_and_consume_token return LightrateClient::ConsumeLocalBucketTokenResponse.new( success: true, used_local_token: true, bucket_status: bucket.status ) end # No matching bucket or bucket is empty - make API call to get tokens and rule info tokens_to_fetch = @configuration.default_local_bucket_size # Make the API call response = consume_tokens(operation: operation, path: path, http_method: http_method, user_identifier: user_identifier, tokens_requested: tokens_to_fetch) if response.rule.is_default return LightrateClient::ConsumeLocalBucketTokenResponse.new( success: response.tokens_consumed > 0, used_local_token: false, bucket_status: nil ) end bucket = fill_bucket_and_create_if_not_exists(user_identifier, response.rule, response.tokens_consumed) tokens_available = bucket.check_and_consume_token return LightrateClient::ConsumeLocalBucketTokenResponse.new( success: tokens_available, used_local_token: false, bucket_status: bucket.status ) end |
#consume_token_from_bucket(bucket, provided_tokens = 0) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/lightrate_client/client.rb', line 77 def consume_token_from_bucket(bucket, provided_tokens = 0) bucket.synchronize do fetch_required = !bucket.has_tokens? || bucket.expired? token_available = bucket.check_and_consume_token [token_available, fetch_required] end end |
#consume_tokens(operation: nil, path: nil, http_method: nil, user_identifier:, tokens_requested:) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/lightrate_client/client.rb', line 87 def consume_tokens(operation: nil, path: nil, http_method: nil, user_identifier:, tokens_requested:) request = LightrateClient::ConsumeTokensRequest.new( application_id: @configuration.application_id, operation: operation, path: path, http_method: http_method, user_identifier: user_identifier, tokens_requested: tokens_requested, tokens_requested_for_default_bucket_match: 1 ) consume_tokens_with_request(request) end |