forked from strimzi/strimzi-kafka-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimise initial grants requests for 'same token, different session' …
…case (strimzi#180) * Minimise initial grants requests for 'same token, different session' case If grants for the token exist in another session, reuse them, rather than fetch them again. Also introduce per-access-token semaphore to prevent parallel fetching of grants for the same token. Add `strimzi.authorization.reuse.grants` config option to enable this optimization. Co-authored-by: Jakub Scholz <[email protected]> Signed-off-by: Marko Strukelj <[email protected]>
- Loading branch information
Showing
11 changed files
with
225 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...eycloak-authorizer/src/main/java/io/strimzi/kafka/oauth/server/authorizer/Semaphores.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2017-2019, Strimzi authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.strimzi.kafka.oauth.server.authorizer; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
class Semaphores<T> { | ||
|
||
private final ConcurrentHashMap<String, Semaphore<T>> futures = new ConcurrentHashMap<>(); | ||
|
||
SemaphoreResult<T> acquireSemaphore(String key) { | ||
Semaphore<T> semaphore = futures.computeIfAbsent(key, v -> new Semaphore<>()); | ||
return new SemaphoreResult<>(semaphore); | ||
} | ||
|
||
void releaseSemaphore(String key) { | ||
futures.remove(key); | ||
} | ||
|
||
static class Semaphore<T> { | ||
|
||
private final CompletableFuture<T> future = new CompletableFuture<>(); | ||
private final AtomicBoolean acquired = new AtomicBoolean(true); | ||
|
||
private Semaphore() {} | ||
|
||
private boolean tryAcquire() { | ||
return acquired.getAndSet(false); | ||
} | ||
} | ||
|
||
static class SemaphoreResult<T> { | ||
|
||
private final boolean acquired; | ||
private final CompletableFuture<T> future; | ||
|
||
private SemaphoreResult(Semaphore<T> semaphore) { | ||
this.acquired = semaphore.tryAcquire(); | ||
this.future = semaphore.future; | ||
} | ||
|
||
boolean acquired() { | ||
return acquired; | ||
} | ||
|
||
CompletableFuture<T> future() { | ||
return future; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.