🐛 #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
4 changed files with 57 additions and 30 deletions

View File

@ -1,8 +1,5 @@
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.util.http.HttpType;
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 java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
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();
boolean locked = false;
try {
if (!config.isAccessTokenExpired() && !forceRefresh) {
return config.getAccessToken();
}
do {
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());
try {
HttpGet httpGet = new HttpGet(url);
@ -92,8 +94,12 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
if (locked) {
lock.unlock();
}
}
}

View File

@ -1,14 +1,14 @@
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 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.util.http.HttpType;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
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();
boolean locked = false;
try {
if (!config.isAccessTokenExpired() && !forceRefresh) {
return config.getAccessToken();
}
do {
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());
HttpRequest request = HttpRequest.get(url);
@ -73,8 +76,12 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP
}
return this.extractAccessToken(request.send().bodyText());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
if (locked) {
lock.unlock();
}
}
}

View File

@ -1,8 +1,5 @@
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.util.http.HttpType;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
@ -11,6 +8,7 @@ import okhttp3.*;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
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();
boolean locked = false;
try {
if (!config.isAccessTokenExpired() && !forceRefresh) {
return config.getAccessToken();
}
do {
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());
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());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
if (locked) {
lock.unlock();
}
}
}