Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token-cache-PLT-19 #19

Merged
merged 47 commits into from
Apr 6, 2020
Merged

token-cache-PLT-19 #19

merged 47 commits into from
Apr 6, 2020

Conversation

jakobsvenning
Copy link
Contributor

@jakobsvenning jakobsvenning commented Mar 18, 2020

I have added a GenServer which is responsible for managing cached tokens. The API of oauth2_client has not changed, it is identical to what it was before. Whenever an application invokes retrieve_access_token, the function first checks whether the caching GenServer (oauth2c_token_cache_gen_server) is running and if this is the case it will first check in the cache before trying to fetch a new token. There should be no breaking changes since if the caching GenServer is not running, retrieve_access_token should work exactly as before. However, applications that want to utilize the caching mechanism needs to start the GenServer responsible for the caching. By default, a cached token is saved for 1 hour before being discarded. However, it is possible to specify the expiration time when starting the caching GenServer.

UPDATE:

The cached tokens are stored in a ETS-table which can be concurrently read by multiple processes and thus avoiding the "single process botlleneck" which existed in the previous implementation that only allowed the GenServer process to read and write the cache. The new implementation also handles "bursts" better than the previous implementation. If multiple concurrent requests for the same key to an empty cache would be performed in the old implementation, it is possible that several processes would have asked the remote provider for a token that would later be cached. In the new implementation, only a single process would ask the remote provider for a token and all other concurrent request would use the token returned to the single requesting process from the remote token provider.

For instance, if multiple token requests are made concurrently for the same key to an empty cache; only a single of those requests will ask the remote provider for a token and the rest will use the cached one.

Copy link

@rolkar-kivra rolkar-kivra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
It passes tests
Dialyzer is happy :)
I am not sure about indentations, but lets check and fix that later. Looks Ok at a quick glance.

src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
test/oauth2c_token_cache_gen_server_SUITE.erl Outdated Show resolved Hide resolved
src/oauth2c.erl Outdated Show resolved Hide resolved
Copy link

@huss-kivra huss-kivra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of things to make it follow general OTP patterns a bit better

src/oauth2c.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
src/oauth2c_token_cache_gen_server.erl Outdated Show resolved Hide resolved
test/oauth2c_token_cache_gen_server_SUITE.erl Outdated Show resolved Hide resolved
test/oauth2c_token_cache_gen_server_SUITE.erl Outdated Show resolved Hide resolved
src/oauth2c.erl Outdated Show resolved Hide resolved
Copy link
Contributor

@alexandre-kivra alexandre-kivra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try this out.

@alexandre-kivra alexandre-kivra merged commit c89d6ec into master Apr 6, 2020
@alexandre-kivra alexandre-kivra deleted the js-token-cache branch April 6, 2020 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants