Skip to content

Commit

Permalink
Merge pull request #2014 from ballerina-platform/caching-with-execute…
Browse files Browse the repository at this point in the history
…-8.x

[2201.8.x] Fix caching issues with execute action
  • Loading branch information
TharmiganK authored May 29, 2024
2 parents 1725057 + 194c4b9 commit e10eb33
Show file tree
Hide file tree
Showing 23 changed files with 80 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-timestamped-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jobs:
call_workflow:
name: Run Build Workflow
if: ${{ github.repository_owner == 'ballerina-platform' }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/build-timestamp-master-template.yml@main
uses: ballerina-platform/ballerina-library/.github/workflows/build-timestamp-master-template.yml@main
secrets: inherit
2 changes: 1 addition & 1 deletion .github/workflows/build-with-bal-test-graalvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
call_stdlib_workflow:
name: Run StdLib Workflow
if: ${{ github.event_name != 'schedule' || (github.event_name == 'schedule' && github.repository_owner == 'ballerina-platform') }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/build-with-bal-test-graalvm-template.yml@2201.8.x
uses: ballerina-platform/ballerina-library/.github/workflows/build-with-bal-test-graalvm-template.yml@2201.8.x
with:
lang_tag: ${{ inputs.lang_tag }}
lang_version: ${{ inputs.lang_version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/central-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
call_workflow:
name: Run Central Publish Workflow
if: ${{ github.repository_owner == 'ballerina-platform' }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/central-publish-template.yml@main
uses: ballerina-platform/ballerina-library/.github/workflows/central-publish-template.yml@main
secrets: inherit
with:
environment: ${{ github.event.inputs.environment }}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
call_workflow:
name: Run Build Workflow
if: ${{ github.repository_owner == 'ballerina-platform' }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/build-timestamp-master-template.yml@main
uses: ballerina-platform/ballerina-library/.github/workflows/build-timestamp-master-template.yml@main
with:
additional-build-flags: '-x check -x test'
secrets: inherit
2 changes: 1 addition & 1 deletion .github/workflows/process-load-test-result.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
jobs:
call_stdlib_process_load_test_results_workflow:
name: Run StdLib Process Load Test Results Workflow
uses: ballerina-platform/ballerina-standard-library/.github/workflows/process-load-test-results-template.yml@main
uses: ballerina-platform/ballerina-library/.github/workflows/process-load-test-results-template.yml@main
with:
results: ${{ toJson(github.event.client_payload.results) }}
secrets:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
call_workflow:
name: Run Release Workflow
if: ${{ github.repository_owner == 'ballerina-platform' }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/release-package-template.yml@main
uses: ballerina-platform/ballerina-library/.github/workflows/release-package-template.yml@main
secrets: inherit
with:
package-name: http
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ jobs:
call_workflow:
name: Run PR Build Workflow
if: ${{ github.repository_owner == 'ballerina-platform' }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/pull-request-build-template.yml@main
uses: ballerina-platform/ballerina-library/.github/workflows/pull-request-build-template.yml@main
secrets: inherit
2 changes: 1 addition & 1 deletion .github/workflows/trigger-load-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
call_stdlib_trigger_load_test_workflow:
name: Run StdLib Load Test Workflow
if: ${{ github.event_name != 'schedule' || (github.event_name == 'schedule' && github.repository_owner == 'ballerina-platform') }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/trigger-load-tests-template.yml@main
uses: ballerina-platform/ballerina-library/.github/workflows/trigger-load-tests-template.yml@main
with:
repo_name: 'module-ballerina-http'
runtime_artifacts_url: 'https://api.github.com/repos/ballerina-platform/module-ballerina-http/actions/artifacts'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/trivy-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ jobs:
call_workflow:
name: Run Trivy Scan Workflow
if: ${{ github.repository_owner == 'ballerina-platform' }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/trivy-scan-template.yml@main
uses: ballerina-platform/ballerina-library/.github/workflows/trivy-scan-template.yml@main
secrets: inherit
2 changes: 1 addition & 1 deletion ballerina-tests/http-advanced-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ isolated int noCacheHitCountNew = 0;
isolated int maxAgeHitCountNew = 0;
isolated int numberOfHitsNew = 0;
isolated int statusHits = 0;
isolated int greetingHits = 0;
final readonly & xml maxAgePayload1 = xml `<message>before cache expiration</message>`;
final readonly & xml maxAgePayload2 = xml `<message>after cache expiration</message>`;
readonly & string errorBody = "Error";
Expand Down Expand Up @@ -160,6 +161,13 @@ service / on new http:Listener(cacheAnnotationTestPort2, httpVersion = http:HTTP
return err;
}
}

resource function default greeting() returns @http:Cache {maxAge: 10} json {
lock {
greetingHits += 1;
}
return {"message": "Hello, World!"};
}
}

@test:Config {}
Expand Down Expand Up @@ -318,3 +326,50 @@ function testReturnStatusCodeResponsesWithAnnotation() returns error? {
common:assertTextPayload(response.getTextPayload(), errorBody);
return;
}

@test:Config {}
function testBasicCachingBehaviourWithExecute() returns error? {
check checkBasicCachingBehaviourWithExecute("GET", 1);
runtime:sleep(1);
check checkBasicCachingBehaviourWithExecute("get", 1);
runtime:sleep(1);
check checkBasicCachingBehaviourWithExecute("HEAD", 2);
runtime:sleep(1);
check checkBasicCachingBehaviourWithExecute("head", 2);
}

function checkBasicCachingBehaviourWithExecute(string method, int hitCount) returns error? {
http:Response|error response = cacheBackendEP->execute(method, "/greeting", new http:Request());
if response is http:Response {
test:assertEquals(response.statusCode, 200, msg = "Found unexpected output");
lock {
test:assertEquals(greetingHits, hitCount);
}
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}

runtime:sleep(1);

response = cacheBackendEP->execute(method, "/greeting", new http:Request());
if response is http:Response {
test:assertEquals(response.statusCode, 200, msg = "Found unexpected output");
lock {
test:assertEquals(greetingHits, hitCount);
}
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}

runtime:sleep(1);

response = cacheBackendEP->execute(method, "/greeting", new http:Request());
if response is http:Response {
test:assertEquals(response.statusCode, 200, msg = "Found unexpected output");
lock {
test:assertEquals(greetingHits, hitCount);
}
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
}
2 changes: 1 addition & 1 deletion ballerina-tests/http-client-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/http-dispatching-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/http-interceptor-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/http-misc-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/http-resiliency-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/http-security-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ modules = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/http-service-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/http2-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ modules = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.2.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down
2 changes: 1 addition & 1 deletion ballerina/caching_http_cache.bal
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,5 @@ isolated function weakValidatorEquals(string etag1, string etag2) returns boolea
}

isolated function getCacheKey(string httpMethod, string url) returns string {
return string `${httpMethod} ${url}`;
return string `${httpMethod.toUpperAscii()} ${url}`;
}
8 changes: 4 additions & 4 deletions ballerina/caching_http_caching_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ client isolated class HttpCachingClient {
Request request = <Request>message;
setRequestCacheControlHeader(request);

if httpMethod == HTTP_GET || httpMethod == HTTP_HEAD {
if httpMethod.equalsIgnoreCaseAscii(HTTP_GET) || httpMethod.equalsIgnoreCaseAscii(HTTP_HEAD) {
return getCachedResponse(self.cache, self.httpClient, request, httpMethod, path,
self.cacheConfig.isShared, false);
}
Expand Down Expand Up @@ -187,7 +187,7 @@ client isolated class HttpCachingClient {
# + request - The HTTP request to be forwarded
# + return - The response or an `http:ClientError` if failed to establish the communication with the upstream server
remote isolated function forward(string path, Request request) returns Response|ClientError {
if request.method == HTTP_GET || request.method == HTTP_HEAD {
if request.method.equalsIgnoreCaseAscii(HTTP_GET) || request.method.equalsIgnoreCaseAscii(HTTP_HEAD) {
return getCachedResponse(self.cache, self.httpClient, request, request.method, path,
self.cacheConfig.isShared, true);
}
Expand Down Expand Up @@ -367,9 +367,9 @@ isolated function sendNewRequest(HttpClient httpClient, Request request, string
if forwardRequest {
return httpClient->forward(path, request);
}
if httpMethod == HTTP_GET {
if httpMethod.equalsIgnoreCaseAscii(HTTP_GET) {
return httpClient->get(path, message = request);
} else if httpMethod == HTTP_HEAD {
} else if httpMethod.equalsIgnoreCaseAscii(HTTP_HEAD) {
return httpClient->head(path, message = request);
} else {
string message = "HTTP method not supported in caching client: " + httpMethod;
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Fixed

- [Fix caching behaviour with client execute method](https://github.com/ballerina-platform/ballerina-library/issues/6570)
- [Address CVE-2024-29025 netty's vulnerability](https://github.com/ballerina-platform/ballerina-library/issues/6242)

## [2.10.12] - 2024-03-21
Expand Down

0 comments on commit e10eb33

Please sign in to comment.