-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade the credential refresh mechanism and add fallbacks for …
…error scenarios
- Loading branch information
Showing
23 changed files
with
1,017 additions
and
166 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
59 changes: 59 additions & 0 deletions
59
src/main/java/com/aliyun/credentials/policy/NonBlocking.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,59 @@ | ||
package com.aliyun.credentials.policy; | ||
|
||
import com.aliyun.tea.logging.ClientLogger; | ||
|
||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
public class NonBlocking implements PrefetchStrategy { | ||
private static final ClientLogger logger = new ClientLogger(NonBlocking.class); | ||
private static final int MAX_CONCURRENT_REFRESHES = 100; | ||
private static final Semaphore CONCURRENT_REFRESH_LEASES = new Semaphore(MAX_CONCURRENT_REFRESHES); | ||
private final AtomicBoolean currentlyRefreshing = new AtomicBoolean(false); | ||
|
||
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 60, SECONDS, | ||
new SynchronousQueue<>(), | ||
new ThreadFactory() { | ||
public Thread newThread(Runnable r) { | ||
Thread t = Executors.defaultThreadFactory().newThread(r); | ||
t.setName("non-blocking-refresh"); | ||
t.setDaemon(true); | ||
return t; | ||
} | ||
}); | ||
|
||
@Override | ||
public void prefetch(Runnable valueUpdater) { | ||
if (currentlyRefreshing.compareAndSet(false, true)) { | ||
if (!CONCURRENT_REFRESH_LEASES.tryAcquire()) { | ||
logger.warning("Skipping a background refresh task because there are too many other tasks running."); | ||
currentlyRefreshing.set(false); | ||
return; | ||
} | ||
try { | ||
executor.submit(() -> { | ||
try { | ||
valueUpdater.run(); | ||
} catch (Throwable t) { | ||
logger.logThrowableAsWarning(t); | ||
} finally { | ||
CONCURRENT_REFRESH_LEASES.release(); | ||
currentlyRefreshing.set(false); | ||
} | ||
}); | ||
} catch (Throwable t) { | ||
logger.logThrowableAsWarning(t); | ||
} finally { | ||
CONCURRENT_REFRESH_LEASES.release(); | ||
currentlyRefreshing.set(false); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
executor.shutdown(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/aliyun/credentials/policy/OneCallerBlocks.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,18 @@ | ||
package com.aliyun.credentials.policy; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class OneCallerBlocks implements PrefetchStrategy { | ||
private final AtomicBoolean currentlyRefreshing = new AtomicBoolean(false); | ||
|
||
@Override | ||
public void prefetch(Runnable valueUpdater) { | ||
if (currentlyRefreshing.compareAndSet(false, true)) { | ||
try { | ||
valueUpdater.run(); | ||
} finally { | ||
currentlyRefreshing.set(false); | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/aliyun/credentials/policy/PrefetchStrategy.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,9 @@ | ||
package com.aliyun.credentials.policy; | ||
|
||
@FunctionalInterface | ||
public interface PrefetchStrategy extends AutoCloseable { | ||
void prefetch(Runnable valueUpdater); | ||
|
||
default void close() { | ||
} | ||
} |
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.