fix(token manager): exception when fetch token concurrently · a-codespaces/appframework-java@4e23a75 · GitHub
Skip to content

Commit 4e23a75

Browse files
author
xionghan.00
committed
fix(token manager): exception when fetch token concurrently
1 parent ef0abfa commit 4e23a75

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

appframework-sdk/pom.xml

Lines changed: 1 addition & 1 deletion

appframework-sdk/src/main/java/com/larksuite/appframework/sdk/core/auth/AppTokenManager.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AppTokenManager {
3737

3838
private final Lock appAccessTokenFetchLock = new ReentrantLock();
3939

40-
private final Map<String, FutureTask<AppTenantAccessToken>> tenantTokenFetchFutureMap = new ConcurrentHashMap<>();
40+
private final Map<String, FutureTask<?>> tenantTokenFetchFutureMap = new ConcurrentHashMap<>();
4141

4242
public AppTokenManager(OpenApiClient openApiClient, App app, AppTicketStorage appTicketStorage) {
4343
this.app = app;
@@ -77,6 +77,8 @@ public void refreshAppAccessToken() {
7777
} finally {
7878
appAccessTokenFetchLock.unlock();
7979
}
80+
81+
LoggerUtil.GLOBAL_LOGGER.debug("app access token refreshed for appId: {}", app.getAppId());
8082
} else {
8183
// if someone is already fetching token, we do nothing but wait for fetching done
8284
try {
@@ -88,7 +90,7 @@ public void refreshAppAccessToken() {
8890
}
8991

9092
public void refreshTenantToken(String tenantKey) throws LarkClientException {
91-
FutureTask<AppTenantAccessToken> doingTask = tenantTokenFetchFutureMap.get(tenantKey);
93+
FutureTask<?> doingTask = tenantTokenFetchFutureMap.get(tenantKey);
9294

9395
if (doingTask == null) {
9496

@@ -100,6 +102,7 @@ public void refreshTenantToken(String tenantKey) throws LarkClientException {
100102
}
101103

102104
if (appAccessToken == null || appAccessToken.isExpired()) {
105+
appAccessToken = null;
103106
return null;
104107
}
105108

@@ -124,17 +127,29 @@ public void refreshTenantToken(String tenantKey) throws LarkClientException {
124127
}
125128
};
126129

127-
FutureTask<AppTenantAccessToken> newTask = new FutureTask<>(() -> {
130+
FutureTask<?> newTask = new FutureTask<Void>(() -> {
131+
AppTenantAccessToken appTenantAccessToken;
128132
// retry fetch tenant access token after refresh app access token
129133
try {
130-
return c.call();
134+
appTenantAccessToken = c.call();
131135
} catch (LarkClientException e) {
132136
if (e.getCode() == LarkClientException.APP_ACCESS_TOKEN_INVALID) {
133137
refreshAppAccessToken();
134-
return c.call();
138+
appTenantAccessToken = c.call();
139+
} else {
140+
throw e;
135141
}
136-
throw e;
137142
}
143+
144+
if (appTenantAccessToken != null) {
145+
tenantTokenMap.put(tenantKey, appTenantAccessToken);
146+
LoggerUtil.GLOBAL_LOGGER.debug("tenant access token refreshed for appId: {}, tenantKey: {}", app.getAppId(), tenantKey);
147+
} else {
148+
LoggerUtil.GLOBAL_LOGGER.error("tenant access token refreshed failed for appId: {}, tenantKey: {}", app.getAppId(), tenantKey);
149+
}
150+
151+
return null;
152+
138153
});
139154

140155
doingTask = tenantTokenFetchFutureMap.putIfAbsent(tenantKey, newTask);
@@ -144,11 +159,8 @@ public void refreshTenantToken(String tenantKey) throws LarkClientException {
144159
newTask.run();
145160

146161
try {
147-
AppTenantAccessToken appTenantAccessToken = newTask.get();
148-
if (appTenantAccessToken != null) {
149-
LoggerUtil.GLOBAL_LOGGER.debug("token refreshed for appId: {}, tenantKey: {}", app.getAppId(), tenantKey);
150-
tenantTokenMap.put(tenantKey, appTenantAccessToken);
151-
}
162+
newTask.get();
163+
152164
} catch (InterruptedException e) {
153165
// wont happen here
154166
} catch (ExecutionException e) {
@@ -168,6 +180,8 @@ public void refreshTenantToken(String tenantKey) throws LarkClientException {
168180
try {
169181
// others doing the task, wait for it done
170182
doingTask.get();
183+
LoggerUtil.GLOBAL_LOGGER.debug("wait for refreshed for appId: {}, tenantKey: {}, done.", app.getAppId(), tenantKey);
184+
171185
} catch (InterruptedException e) {
172186
LoggerUtil.GLOBAL_LOGGER.error("refreshTenantToken InterruptedException ", e);
173187
} catch (ExecutionException e) {

appframework-sdk/src/main/java/com/larksuite/appframework/sdk/utils/LoggerUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111

1212
public class LoggerUtil {
1313

14-
public static final Logger GLOBAL_LOGGER = LoggerFactory.getLogger("botframework");
14+
public static final Logger GLOBAL_LOGGER = LoggerFactory.getLogger("appframework");
1515
}

appframework-spring-boot-starter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.larksuite.appframework</groupId>
1313
<artifactId>appframework-parent</artifactId>
14-
<version>1.0.1</version>
14+
<version>1.0.2</version>
1515
</parent>
1616
<modelVersion>4.0.0</modelVersion>
1717

pom.xml

Lines changed: 4 additions & 2 deletions

0 commit comments

Comments
 (0)