Skip to content

Commit

Permalink
Add token_lookup_batch_size config option
Browse files Browse the repository at this point in the history
In case you enabled `reuse_access_token` option Doorkeeper will try to find matching
token using `matching_token_for` Access Token API that searches for valid records
in batches in order not to pollute the memory with all the database records. By default
Doorkeeper uses batch size of 10 000 records. You can increase or decrease this value
depending on your needs and server capabilities.

```ruby
token_lookup_batch_size 10_000
```
  • Loading branch information
nbulaj committed Feb 9, 2020
1 parent 4fe3797 commit 1fd6541
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/doorkeeper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def configure_secrets_for(type, using:, fallback:)
option :active_record_options, default: {}
option :grant_flows, default: %w[authorization_code client_credentials]
option :handle_auth_errors, default: :render
option :token_lookup_batch_size, default: 10_000

# Allows to customize OAuth grant flows that +each+ application support.
# You can configure a custom block (or use a class respond to `#call`) that must
Expand Down
3 changes: 2 additions & 1 deletion lib/doorkeeper/models/access_token_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ def find_matching_token(relation, application, scopes)
return nil unless relation

matching_tokens = []
batch_size = Doorkeeper.configuration.token_lookup_batch_size

find_access_token_in_batches(relation) do |batch|
find_access_token_in_batches(relation, batch_size: batch_size) do |batch|
tokens = batch.select do |token|
scopes_match?(token.scopes, scopes, application.try(:scopes))
end
Expand Down
8 changes: 8 additions & 0 deletions lib/generators/doorkeeper/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@
#
# reuse_access_token

# In case you enabled `reuse_access_token` option Doorkeeper will try to find matching
# token using `matching_token_for` Access Token API that searches for valid records
# in batches in order not to pollute the memory with all the database records. By default
# Doorkeeper uses batch size of 10 000 records. You can increase or decrease this value
# depending on your needs and server capabilities.
#
# token_lookup_batch_size 10_000

# Set a limit for token_reuse if using reuse_access_token option
#
# This option limits token_reusability to some extent.
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,21 @@ class FakeCustomModel; end
end
end

describe "token_lookup_batch_size" do
it "uses default doorkeeper value" do
expect(subject.token_lookup_batch_size).to eq(10_000)
end

it "can change the value" do
Doorkeeper.configure do
orm DOORKEEPER_ORM
token_lookup_batch_size 100_000
end

expect(subject.token_lookup_batch_size).to eq(100_000)
end
end

describe "strict_content_type" do
it "is false by default" do
expect(subject.enforce_content_type).to eq(false)
Expand Down

0 comments on commit 1fd6541

Please sign in to comment.