🐛 #1582 use lock.tryLock() to avoid waiting for locks for a long time

This commit is contained in:
Mario Luo 2020-05-31 22:54:11 +08:00 committed by GitHub
parent 2446fa626b
commit 6c3d090ebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 30 deletions

View File

@ -1,8 +1,5 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
@ -16,6 +13,7 @@ import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL; import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
@ -72,11 +70,15 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp
} }
Lock lock = config.getAccessTokenLock(); Lock lock = config.getAccessTokenLock();
lock.lock(); boolean locked = false;
try { try {
if (!config.isAccessTokenExpired() && !forceRefresh) { do {
return config.getAccessToken(); locked = lock.tryLock(100, TimeUnit.MILLISECONDS);
} if (!forceRefresh && !config.isAccessTokenExpired()) {
return config.getAccessToken();
}
} while (!locked);
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret()); String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
try { try {
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
@ -92,8 +94,12 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally { } finally {
lock.unlock(); if (locked) {
lock.unlock();
}
} }
} }

View File

@ -1,14 +1,14 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;
import jodd.http.*; import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.ProxyInfo;
import jodd.http.net.SocketHttpConnectionProvider; import jodd.http.net.SocketHttpConnectionProvider;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL; import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
@ -57,11 +57,14 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP
} }
Lock lock = config.getAccessTokenLock(); Lock lock = config.getAccessTokenLock();
lock.lock(); boolean locked = false;
try { try {
if (!config.isAccessTokenExpired() && !forceRefresh) { do {
return config.getAccessToken(); locked = lock.tryLock(100, TimeUnit.MILLISECONDS);
} if (!forceRefresh && !config.isAccessTokenExpired()) {
return config.getAccessToken();
}
} while (!locked);
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret()); String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
HttpRequest request = HttpRequest.get(url); HttpRequest request = HttpRequest.get(url);
@ -73,8 +76,12 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP
} }
return this.extractAccessToken(request.send().bodyText()); return this.extractAccessToken(request.send().bodyText());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally { } finally {
lock.unlock(); if (locked) {
lock.unlock();
}
} }
} }

View File

@ -1,8 +1,5 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
@ -11,6 +8,7 @@ import okhttp3.*;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL; import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
@ -47,11 +45,14 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH
} }
Lock lock = config.getAccessTokenLock(); Lock lock = config.getAccessTokenLock();
lock.lock(); boolean locked = false;
try { try {
if (!config.isAccessTokenExpired() && !forceRefresh) { do {
return config.getAccessToken(); locked = lock.tryLock(100, TimeUnit.MILLISECONDS);
} if (!forceRefresh && !config.isAccessTokenExpired()) {
return config.getAccessToken();
}
} while (!locked);
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret()); String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
Request request = new Request.Builder().url(url).get().build(); Request request = new Request.Builder().url(url).get().build();
@ -59,8 +60,12 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH
return this.extractAccessToken(Objects.requireNonNull(response.body()).string()); return this.extractAccessToken(Objects.requireNonNull(response.body()).string());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally { } finally {
lock.unlock(); if (locked) {
lock.unlock();
}
} }
} }

View File

@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
/** /**
@ -383,11 +384,15 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
return config.getAuthorizerAccessToken(appId); return config.getAuthorizerAccessToken(appId);
} }
Lock lock = config.getWxMpConfigStorage(appId).getAccessTokenLock(); Lock lock = config.getWxMpConfigStorage(appId).getAccessTokenLock();
lock.lock(); boolean locked = false;
try { try {
if (!config.isAuthorizerAccessTokenExpired(appId) && !forceRefresh) { do {
return config.getAuthorizerAccessToken(appId); locked = lock.tryLock(100, TimeUnit.MILLISECONDS);
} if (!forceRefresh && !config.isAuthorizerAccessTokenExpired(appId)) {
return config.getAuthorizerAccessToken(appId);
}
} while (!locked);
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId());
jsonObject.addProperty("authorizer_appid", appId); jsonObject.addProperty("authorizer_appid", appId);
@ -397,8 +402,12 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
WxOpenAuthorizerAccessToken wxOpenAuthorizerAccessToken = WxOpenAuthorizerAccessToken.fromJson(responseContent); WxOpenAuthorizerAccessToken wxOpenAuthorizerAccessToken = WxOpenAuthorizerAccessToken.fromJson(responseContent);
config.updateAuthorizerAccessToken(appId, wxOpenAuthorizerAccessToken); config.updateAuthorizerAccessToken(appId, wxOpenAuthorizerAccessToken);
return config.getAuthorizerAccessToken(appId); return config.getAuthorizerAccessToken(appId);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally { } finally {
lock.unlock(); if (locked) {
lock.unlock();
}
} }
} }