Skip to content

Commit 8bd5798

Browse files
committed
revert(java): synchronized function
1 parent fe9f603 commit 8bd5798

3 files changed

Lines changed: 7 additions & 36 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ The `ConfidentialClient` refreshes access tokens proactively before their actual
187187
Default behaviour:
188188
- A 30 second (30,000 ms) proactive offset is applied automatically.
189189
- Calls to `getAccessToken()` (or `getAccessToken(false)`) reuse the cached token while it is still considered valid under this adjusted expiry.
190-
- `getAccessToken(true)` forces a fresh token unless one was very recently refreshed (within 5 seconds) to avoid unnecessary duplicate requests from concurrent threads.
190+
- `getAccessToken(true)` forces a fresh token unless one was very recently refreshed (within 5 seconds) to avoid unnecessary duplicate requests.
191191

192192
You can override the proactive offset by configuring it in `RequestOptions`:
193193

src/main/java/com/factset/sdk/utils/authentication/ConfidentialClient.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.List;
3434
import java.util.Objects;
3535
import java.util.concurrent.TimeUnit;
36-
import java.time.Duration; // added for internal duration usage
36+
import java.time.Duration;
3737
import org.slf4j.Logger;
3838
import org.slf4j.LoggerFactory;
3939

@@ -194,16 +194,15 @@ protected ConfidentialClient(final Configuration config, final TokenRequestBuild
194194
* @throws AccessTokenException If it can't make a successful request or parse the TokenRequest.
195195
* @throws SigningJwsException If the signing of the JWS fails.
196196
*/
197-
public synchronized String getAccessToken(boolean forceRefresh) throws AccessTokenException, SigningJwsException {
197+
public String getAccessToken(boolean forceRefresh) throws AccessTokenException, SigningJwsException {
198198
if (this.isCachedTokenValid()) {
199199
if (!forceRefresh) {
200200
LOGGER.info("Retrieved access token which expires in: {} seconds", TimeUnit.MILLISECONDS.toSeconds(this.accessTokenExpireTime - System.currentTimeMillis()));
201201
return this.accessToken.toString();
202202
}
203203

204+
// Implement a grace period of 5 seconds to avoid unnecessary token refreshes
204205
long currentTime = System.currentTimeMillis();
205-
206-
// Implementing a grace period of 5 seconds to avoid unnecessary token refreshes
207206
boolean recentlyRefreshed = (currentTime - this.lastRefreshTime) < 5000;
208207
if (recentlyRefreshed) {
209208
LOGGER.debug("Force refresh requested but token was recently refreshed within grace period, returning cached token");
@@ -225,13 +224,8 @@ public synchronized String getAccessToken(boolean forceRefresh) throws AccessTok
225224
* @throws SigningJwsException If the signing of the JWS fails.
226225
*/
227226
@Override
228-
public synchronized String getAccessToken() throws AccessTokenException, SigningJwsException {
229-
if (this.isCachedTokenValid()) {
230-
LOGGER.info("Retrieved access token which expires in: {} seconds", TimeUnit.MILLISECONDS.toSeconds(this.accessTokenExpireTime - System.currentTimeMillis()));
231-
return this.accessToken.toString();
232-
}
233-
234-
return this.fetchAccessToken();
227+
public String getAccessToken() throws AccessTokenException, SigningJwsException {
228+
return getAccessToken(false);
235229
}
236230

237231
private void requestProviderMetadata() throws AuthServerMetadataContentException, AuthServerMetadataException {
@@ -264,7 +258,7 @@ private void requestProviderMetadata() throws AuthServerMetadataContentException
264258
new TokenRequestBuilder().uri(this.providerMetadata.getTokenEndpointURI());
265259
}
266260

267-
private synchronized boolean isCachedTokenValid() {
261+
private boolean isCachedTokenValid() {
268262
if (this.accessToken == null) {
269263
return false;
270264
}

src/test/java/com/factset/sdk/utils/authentication/ConfidentialClientTest.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -330,29 +330,6 @@ void getAccessTokenForceRefreshThenCachedReturnsCorrectTokens() throws Exception
330330
verify(harness.httpRequestMock, times(1)).send();
331331
}
332332

333-
@Test
334-
void getAccessTokenTwoDifferentThreadsSimultaneouslyOnlyFetchesOnce() throws Exception {
335-
TestHarness harness = createClientWithTokens(899, "threadedToken");
336-
337-
Runnable task = () -> {
338-
String token;
339-
try {
340-
token = harness.client.getAccessToken();
341-
} catch (AccessTokenException | SigningJwsException e) {
342-
throw new RuntimeException(e);
343-
}
344-
assertEquals("threadedToken", token);
345-
};
346-
347-
Thread thread1 = new Thread(task);
348-
Thread thread2 = new Thread(task);
349-
thread1.start();
350-
thread2.start();
351-
thread1.join();
352-
thread2.join();
353-
354-
verify(harness.httpRequestMock, times(1)).send();
355-
}
356333

357334
@Test
358335
void forceRefreshWithinGracePeriodReturnsCachedToken() throws Exception {

0 commit comments

Comments
 (0)