Skip to content

Commit

Permalink
Fix binarywang#1582 use lock.tryLock() avoid waiting for locks for a …
Browse files Browse the repository at this point in the history
…long time.
  • Loading branch information
lkqm committed May 31, 2020
1 parent 0a2e4d8 commit 829b103
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +13,7 @@
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;
Expand Down Expand Up @@ -72,11 +70,15 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
}

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);
Expand All @@ -92,8 +94,12 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
if (locked) {
lock.unlock();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -57,11 +57,14 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
}

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);
Expand All @@ -73,8 +76,12 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
}

return this.extractAccessToken(request.send().bodyText());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
if (locked) {
lock.unlock();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +8,7 @@

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;
Expand Down Expand Up @@ -47,20 +45,27 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
}

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();
Response response = getRequestHttpClient().newCall(request).execute();
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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
Expand Down Expand Up @@ -383,11 +384,15 @@ public String getAuthorizerAccessToken(String appId, boolean forceRefresh) throw
return config.getAuthorizerAccessToken(appId);
}
Lock lock = config.getWxMpConfigStorage(appId).getAccessTokenLock();
lock.lock();
boolean locked = false;
try {
if (!config.isAuthorizerAccessTokenExpired(appId) && !forceRefresh) {
return config.getAuthorizerAccessToken(appId);
}
do {
locked = lock.tryLock(100, TimeUnit.MILLISECONDS);
if (!forceRefresh && !config.isAuthorizerAccessTokenExpired(appId)) {
return config.getAuthorizerAccessToken(appId);
}
} while (!locked);

JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId());
jsonObject.addProperty("authorizer_appid", appId);
Expand All @@ -397,8 +402,12 @@ public String getAuthorizerAccessToken(String appId, boolean forceRefresh) throw
WxOpenAuthorizerAccessToken wxOpenAuthorizerAccessToken = WxOpenAuthorizerAccessToken.fromJson(responseContent);
config.updateAuthorizerAccessToken(appId, wxOpenAuthorizerAccessToken);
return config.getAuthorizerAccessToken(appId);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
if (locked) {
lock.unlock();
}
}
}

Expand Down

0 comments on commit 829b103

Please sign in to comment.