-
Notifications
You must be signed in to change notification settings - Fork 57
Add Circuit breaker support #1534
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
93c87f1
Add circuit breaker support
niveathika 5f99bf1
Add testcases
niveathika 361ae0c
Add documentation
niveathika 124829a
Fix logic flows
niveathika cb25070
Add testcases
niveathika 6b92b24
Remove unused function
niveathika 7585ca2
Improve doc
niveathika ac99f00
Fail fast for cicuit open
niveathika e3c81ae
Add test cases
niveathika d9d7d58
Fix license
niveathika 32b14d0
Initial plan
Copilot ab22298
Replace synchronized blocks with StampedLock in CircuitBreaker for be…
Copilot 11b59c8
Merge pull request #1541 from ballerina-platform/copilot/update-circu…
niveathika bb9415e
Fix unnecessary comments and warning suppression
niveathika e64f85f
Add empty failure condition validation
niveathika 9d506b3
Improve code
niveathika File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import ballerina/test; | ||
|
|
||
| // Test client creation with invalid failure threshold (> 1.0) | ||
| @test:Config {} | ||
| public function testClientWithInvalidFailureThresholdHigh() { | ||
| ClientConfiguration config = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21212, | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| circuitBreaker: { | ||
| failureThreshold: 1.5, | ||
| resetTime: 30 | ||
| } | ||
| }; | ||
| Client|Error cbClient = new (config); | ||
| test:assertTrue(cbClient is Error, msg = "Client creation should fail with invalid failure threshold > 1.0"); | ||
| if cbClient is Error { | ||
| test:assertTrue(cbClient.message().includes("failureThreshold"), | ||
| msg = "Error message should mention failureThreshold"); | ||
| } | ||
| } | ||
|
|
||
| // Test client creation with invalid failure threshold (< 0.0) | ||
| @test:Config {} | ||
| public function testClientWithInvalidFailureThresholdLow() { | ||
| ClientConfiguration config = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21212, | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| circuitBreaker: { | ||
| failureThreshold: -0.1, | ||
| resetTime: 30 | ||
| } | ||
| }; | ||
| Client|Error cbClient = new (config); | ||
| test:assertTrue(cbClient is Error, msg = "Client creation should fail with invalid failure threshold < 0.0"); | ||
| if cbClient is Error { | ||
| test:assertTrue(cbClient.message().includes("failureThreshold"), | ||
| msg = "Error message should mention failureThreshold"); | ||
| } | ||
| } | ||
|
|
||
| // Test client creation with bucketSize greater than timeWindow | ||
| @test:Config {} | ||
| public function testClientWithInvalidBucketSize() { | ||
| ClientConfiguration config = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21212, | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| circuitBreaker: { | ||
| rollingWindow: { | ||
| timeWindow: 30, | ||
| bucketSize: 60 // bucket larger than window | ||
| } | ||
| } | ||
| }; | ||
| Client|Error cbClient = new (config); | ||
| test:assertTrue(cbClient is Error, msg = "Client creation should fail when bucketSize > timeWindow"); | ||
| if cbClient is Error { | ||
| test:assertTrue(cbClient.message().includes("timeWindow") || cbClient.message().includes("bucketSize"), | ||
| msg = "Error message should mention timeWindow or bucketSize"); | ||
| } | ||
| } | ||
|
|
||
| // Test that operations work normally with circuit breaker enabled | ||
| @test:Config {} | ||
| public function testOperationsWithCircuitBreaker() returns error? { | ||
| ClientConfiguration config = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21212, | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| circuitBreaker: { | ||
| rollingWindow: { | ||
| requestVolumeThreshold: 5, | ||
| timeWindow: 60, | ||
| bucketSize: 10 | ||
| }, | ||
| failureThreshold: 0.5, | ||
| resetTime: 30 | ||
| } | ||
| }; | ||
| Client cbClient = check new (config); | ||
|
|
||
| // Test basic operations work with circuit breaker enabled | ||
| boolean exists = check cbClient->exists("/home/in"); | ||
| test:assertTrue(exists, msg = "Directory should exist"); | ||
|
|
||
| FileInfo[] files = check cbClient->list("/home/in"); | ||
| test:assertTrue(files.length() >= 0, msg = "List operation should return files array"); | ||
|
|
||
| check cbClient->close(); | ||
| } | ||
|
|
||
| // Test that client creation fails when server doesn't exist (connection error, not circuit breaker) | ||
| @test:Config {} | ||
| public function testClientCreationFailsForNonExistentServer() { | ||
| // Create a client pointing to a non-existent server | ||
| ClientConfiguration config = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21299, // Non-existent port | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| circuitBreaker: { | ||
| rollingWindow: { | ||
| requestVolumeThreshold: 1, | ||
| timeWindow: 60, | ||
| bucketSize: 10 | ||
| }, | ||
| failureThreshold: 0.5, | ||
| resetTime: 5, | ||
| failureCategories: [CONNECTION_ERROR] | ||
| } | ||
| }; | ||
|
|
||
| Client|Error cbClient = new (config); | ||
| test:assertTrue(cbClient is Error, msg = "Client creation should fail for non-existent server"); | ||
| } | ||
|
|
||
| // Test that circuit breaker opens after failures and returns CircuitBreakerOpenError | ||
| @test:Config {} | ||
| public function testCircuitBreakerOpensAfterFailures() returns error? { | ||
| // Configure circuit breaker with low thresholds to trigger quickly | ||
| // - requestVolumeThreshold: 2 (only need 2 requests before circuit can trip) | ||
| // - failureThreshold: 0.5 (50% failure rate trips the circuit) | ||
| // - ALL_ERRORS: any error counts as failure | ||
| ClientConfiguration config = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21212, | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| circuitBreaker: { | ||
| rollingWindow: { | ||
| requestVolumeThreshold: 2, | ||
| timeWindow: 60, | ||
| bucketSize: 10 | ||
| }, | ||
| failureThreshold: 0.5, | ||
| resetTime: 30, | ||
| failureCategories: [ALL_ERRORS] | ||
| } | ||
| }; | ||
|
|
||
| Client cbClient = check new (config); | ||
|
|
||
| // Make requests that will fail (reading non-existent files) | ||
| // These failures should accumulate and trip the circuit | ||
| string nonExistentPath = "/non/existent/file/path/that/does/not/exist.txt"; | ||
|
|
||
| // First failure | ||
| byte[]|Error result1 = cbClient->getBytes(nonExistentPath); | ||
| test:assertTrue(result1 is Error, msg = "First request should fail (file not found)"); | ||
| test:assertFalse(result1 is CircuitBreakerOpenError, | ||
| msg = "First failure should be regular error, not circuit breaker error"); | ||
|
|
||
| // Second failure - after this, we have 2 requests with 100% failure rate | ||
| byte[]|Error result2 = cbClient->getBytes(nonExistentPath); | ||
| test:assertTrue(result2 is Error, msg = "Second request should fail"); | ||
| test:assertFalse(result2 is CircuitBreakerOpenError, | ||
| msg = "Second failure should be regular error, not circuit breaker error"); | ||
|
|
||
| // Third request - circuit should now be OPEN and reject immediately | ||
| byte[]|Error result3 = cbClient->getBytes(nonExistentPath); | ||
| test:assertTrue(result3 is CircuitBreakerOpenError, | ||
| msg = "Third request should fail with CircuitBreakerOpenError (circuit is open)"); | ||
|
|
||
| if result3 is CircuitBreakerOpenError { | ||
| string message = result3.message(); | ||
| test:assertTrue(message.includes("Circuit breaker is OPEN"), | ||
| msg = "Error message should indicate circuit breaker is open"); | ||
| } | ||
|
|
||
| check cbClient->close(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.