Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix call endpoint hang for non isolated strands #1326

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ group=io.ballerina.stdlib
version=2.12.1-SNAPSHOT
puppycrawlCheckstyleVersion=10.12.0
ballerinaGradlePluginVersion=2.0.1
ballerinaLangVersion=2201.10.0-20241007-143200-6b69ca80
ballerinaLangVersion=2201.10.0-20241025-103700-5c9e6a27
githubJohnrengelmanShadowVersion=8.1.1
underCouchDownloadVersion=5.4.0
researchgateReleaseVersion=2.8.0
Expand Down
31 changes: 17 additions & 14 deletions native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.stdlib.oauth2;

import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BError;
Expand Down Expand Up @@ -56,8 +57,8 @@ public class OAuth2Client {

private OAuth2Client() {}

public static Object doHttpRequest(BString url, BMap<BString, Object> clientConfig, BMap<BString, BString> headers,
BString payload) {
public static Object doHttpRequest(Environment env, BString url, BMap<BString, Object> clientConfig,
BMap<BString, BString> headers, BString payload) {
BString customPayload = getBStringValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_PAYLOAD);
String textPayload = payload.getValue();
if (customPayload != null) {
Expand Down Expand Up @@ -100,13 +101,13 @@ public static Object doHttpRequest(BString url, BMap<BString, Object> clientConf
try {
SSLContext sslContext = getSslContext(secureSocket);
HttpClient client = buildHttpClient(httpVersion, sslContext);
return callEndpoint(client, request);
return callEndpoint(env, client, request);
} catch (Exception e) {
return createError("Failed to init SSL context. " + e.getMessage());
}
}
HttpClient client = buildHttpClient(httpVersion);
return callEndpoint(client, request);
return callEndpoint(env, client, request);
}

private static URI buildUri(String url, BMap<BString, ?> secureSocket) throws IllegalArgumentException {
Expand Down Expand Up @@ -300,17 +301,19 @@ private static HttpRequest buildHttpRequest(URI uri, String[] headers, String pa
.build();
}

private static Object callEndpoint(HttpClient client, HttpRequest request) {
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() >= 200 && response.statusCode() < 300) {
return StringUtils.fromString(response.body());
private static Object callEndpoint(Environment env, HttpClient client, HttpRequest request) {
return env.yieldAndRun(() -> {
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() >= 200 && response.statusCode() < 300) {
return StringUtils.fromString(response.body());
}
return createError("Failed to get a success response from the endpoint. Response code: '" +
response.statusCode() + "'. Response body: '" + response.body() + "'");
} catch (IOException | InterruptedException e) {
return createError("Failed to send the request to the endpoint. " + e.getMessage());
}
return createError("Failed to get a success response from the endpoint. Response code: '" +
response.statusCode() + "'. Response body: '" + response.body() + "'");
} catch (IOException | InterruptedException e) {
return createError("Failed to send the request to the endpoint. " + e.getMessage());
}
});
}

private static BMap<BString, ?> getBMapValueIfPresent(BMap<BString, ?> config, BString key) {
Expand Down
Loading