Skip to content

Commit 98396b9

Browse files
committed
Add documentation
1 parent e8fd86c commit 98396b9

File tree

2 files changed

+104
-55
lines changed

2 files changed

+104
-55
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55

66
## unreleased
77

8+
### Added
9+
10+
- [Add Circuit breaker support](https://github.com/ballerina-platform/ballerina-library/issues/8382)
811
- [Add automatic retry support with exponential backoff for FTP client and listener operations](https://github.com/ballerina-platform/ballerina-library/issues/8585)
12+
- [Add distinct error types](https://github.com/ballerina-platform/ballerina-library/issues/8597)
913
- [Incorporate the csv fail safe support in the FTP listener](https://github.com/ballerina-platform/ballerina-library/issues/8502)
1014

1115
### Fixed

docs/spec/spec.md

Lines changed: 100 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
_Owners_: @shafreenAnfar @dilanSachi @Bhashinee
44
_Reviewers_: @shafreenAnfar @Bhashinee
55
_Created_: 2020/10/28
6-
_Updated_: 2025/11/20
6+
_Updated_: 2026/01/28
77
_Edition_: Swan Lake
88

99
## Introduction
@@ -24,21 +24,21 @@ The conforming implementation of the specification is released and included in t
2424
- [2.1. Security Configurations](#21-security-configurations)
2525
- [2.2. FileInfo](#22-fileinfo)
2626
- [2.3. Error Types](#23-error-types)
27-
- [2.3. Error Types](#23-error-types-1)
2827
- [2.4. Retry Configuration](#24-retry-configuration)
2928
- [3. Client](#3-client)
3029
- [3.1. Configurations](#31-configurations)
31-
- [3.2. Initialization](#32-initialization)
32-
- [3.2.1. Insecure Client](#321-insecure-client)
33-
- [3.2.2. Secure Client](#322-secure-client)
34-
- [3.2.3. Client with Retry Configuration](#323-client-with-retry-configuration)
35-
- [3.3. Functions](#33-functions)
30+
- [3.2. Circuit Breaker](#32-circuit-breaker)
31+
- [3.3. Initialization](#33-initialization)
32+
- [3.3.1. Insecure Client](#331-insecure-client)
33+
- [3.3.2. Secure Client](#332-secure-client)
34+
- [3.3.3. Client with Retry Configuration](#333-client-with-retry-configuration)
35+
- [3.4. Functions](#34-functions)
3636
- [4. Listener](#4-listener)
3737
- [4.1. Configurations](#41-configurations)
3838
- [4.2. Initialization](#42-initialization)
3939
- [4.2.1. Insecure Listener](#421-insecure-listener)
4040
- [4.2.2. Secure Listener](#422-secure-listener)
41-
- [4.3. Usage](#43-usage)
41+
- [4.3. Usage](#43-usage)
4242
- [4.3.1. Format-Specific Listener Callbacks](#431-format-specific-listener-callbacks)
4343
- [5. Caller](#5-caller)
4444
- [5.1. Initialization](#51-initialization)
@@ -161,7 +161,6 @@ public type Error distinct error;
161161

162162
* `ConnectionError` - Represents errors that occur when connecting to the FTP/SFTP server. This includes network failures, host unreachable, connection refused, etc.
163163
```ballerina
164-
# Represents an error that occurs when connecting to the FTP/SFTP server.
165164
public type ConnectionError distinct Error;
166165
```
167166

@@ -180,55 +179,19 @@ public type FileAlreadyExistsError distinct Error;
180179
public type InvalidConfigurationError distinct Error;
181180
```
182181

183-
* `ServiceUnavailableError` - Represents errors that occur when the FTP/SFTP service is temporarily unavailable. This is a transient error indicating the operation may succeed on retry. Common causes include server overload (FTP code 421), connection issues (425, 426), temporary file locks (450), or server-side processing errors (451). This error type is designed for use with retry and circuit breaker patterns.
182+
* `ServiceUnavailableError` - Represents errors that occur when the FTP/SFTP service is temporarily unavailable. This is a transient error indicating the operation may succeed on retry. Common causes include server overload (FTP code 421), connection issues (425, 426), temporary file locks (450), or server-side processing errors (451).
184183
```ballerina
185184
public type ServiceUnavailableError distinct Error;
186185
```
187186

188-
All specific error types are subtypes of the base `Error` type, allowing for both specific and general error handling:
189-
```ballerina
190-
// Handle specific error types
191-
ftp:Client|ftp:Error result = new(config);
192-
if result is ftp:ConnectionError {
193-
// Handle connection failures specifically
194-
} else if result is ftp:ServiceUnavailableError {
195-
// Transient error - retry the operation
196-
} else if result is ftp:Error {
197-
// Handle any other FTP error
198-
}
199-
```
200-
### 2.3. Error Types
201-
The FTP module provides a hierarchy of error types for better error handling and more precise error identification.
202-
203-
* `Error` - The base error type for all FTP-related errors.
204-
```ballerina
205-
public type Error distinct error;
206-
```
207-
208-
* `ConnectionError` - Represents errors that occur when connecting to the FTP/SFTP server. This includes network failures, host unreachable, connection refused, etc.
209-
```ballerina
210-
# Represents an error that occurs when connecting to the FTP/SFTP server.
211-
public type ConnectionError distinct Error;
212-
```
213-
214-
* `FileNotFoundError` - Represents errors that occur when a requested file or directory is not found on the remote server.
215-
```ballerina
216-
public type FileNotFoundError distinct Error;
217-
```
218-
219-
* `FileAlreadyExistsError` - Represents errors that occur when attempting to create a file or directory that already exists.
187+
* `AllRetryAttemptsFailedError` - Represents an error that occurs when all retry attempts have been exhausted. This error wraps the last failure encountered during retry attempts.
220188
```ballerina
221-
public type FileAlreadyExistsError distinct Error;
222-
```
223-
224-
* `InvalidConfigurationError` - Represents errors that occur when FTP/SFTP configuration is invalid (e.g., invalid port numbers, invalid regex patterns, invalid timeout values).
225-
```ballerina
226-
public type InvalidConfigurationError distinct Error;
189+
public type AllRetryAttemptsFailedError distinct Error;
227190
```
228191

229-
* `ServiceUnavailableError` - Represents errors that occur when the FTP/SFTP service is temporarily unavailable. This is a transient error indicating the operation may succeed on retry. Common causes include server overload (FTP code 421), connection issues (425, 426), temporary file locks (450), or server-side processing errors (451). This error type is designed for use with retry and circuit breaker patterns.
192+
* `CircuitBreakerOpenError` - Error returned when the circuit breaker is in OPEN state. This indicates the FTP server is unavailable and requests are being blocked to prevent cascade failures. This is a subtype of `ServiceUnavailableError`.
230193
```ballerina
231-
public type ServiceUnavailableError distinct Error;
194+
public type CircuitBreakerOpenError distinct ServiceUnavailableError;
232195
```
233196

234197
All specific error types are subtypes of the base `Error` type, allowing for both specific and general error handling:
@@ -237,6 +200,10 @@ All specific error types are subtypes of the base `Error` type, allowing for bot
237200
ftp:Client|ftp:Error result = new(config);
238201
if result is ftp:ConnectionError {
239202
// Handle connection failures specifically
203+
} else if result is ftp:CircuitBreakerOpenError {
204+
// Circuit breaker is open - implement fallback logic
205+
} else if result is ftp:AllRetryAttemptsFailedError {
206+
// All retries exhausted - consider alerting
240207
} else if result is ftp:ServiceUnavailableError {
241208
// Transient error - retry the operation
242209
} else if result is ftp:Error {
@@ -284,6 +251,8 @@ public type ClientConfiguration record {|
284251
boolean laxDataBinding = false;
285252
# Retry configuration for read operations
286253
RetryConfig retryConfig?;
254+
# Circuit breaker configuration to prevent cascade failures
255+
CircuitBreakerConfig circuitBreaker?;
287256
|};
288257
```
289258
* InputContent record represents the configurations for the input given for `put` and `append` operations.
@@ -319,8 +288,84 @@ public enum FileWriteOption {
319288
APPEND
320289
}
321290
```
322-
### 3.2. Initialization
323-
#### 3.2.1. Insecure Client
291+
### 3.2. Circuit Breaker
292+
The circuit breaker pattern prevents cascade failures by temporarily blocking requests to an FTP server experiencing issues. When failures reach a threshold, the circuit "opens" and subsequent requests fail fast with `CircuitBreakerOpenError` without attempting the actual operation.
293+
294+
#### State Machine
295+
The circuit breaker operates in three states:
296+
- **CLOSED**: Normal operation. Requests are allowed and failures are tracked.
297+
- **OPEN**: Circuit is tripped. All requests are rejected immediately with `CircuitBreakerOpenError`.
298+
- **HALF_OPEN**: After the reset time elapses, one trial request is allowed. Success returns to CLOSED; failure returns to OPEN.
299+
300+
#### Configuration
301+
* CircuitBreakerConfig record contains the circuit breaker settings.
302+
```ballerina
303+
public type CircuitBreakerConfig record {|
304+
# Rolling window configuration for failure tracking
305+
RollingWindow rollingWindow = {};
306+
# Failure ratio threshold (0.0 to 1.0) that trips the circuit
307+
float failureThreshold = 0.5;
308+
# Time in seconds to wait before transitioning from OPEN to HALF_OPEN
309+
decimal resetTime = 30;
310+
# Categories of failures that count towards tripping the circuit
311+
FailureCategory[] failureCategories = [CONNECTION_ERROR, TRANSIENT_ERROR];
312+
|};
313+
```
314+
* RollingWindow record configures the sliding window for tracking failures.
315+
```ballerina
316+
public type RollingWindow record {|
317+
# Minimum number of requests in the window before the circuit can trip
318+
int requestVolumeThreshold = 10;
319+
# Time window in seconds for tracking failures
320+
decimal timeWindow = 60;
321+
# Size of each time bucket in seconds (timeWindow / bucketSize = number of buckets)
322+
decimal bucketSize = 10;
323+
|};
324+
```
325+
* FailureCategory enum specifies which types of failures count towards tripping the circuit.
326+
```ballerina
327+
public enum FailureCategory {
328+
# Connection failures (network issues, timeouts)
329+
CONNECTION_ERROR,
330+
# Authentication failures (invalid credentials)
331+
AUTHENTICATION_ERROR,
332+
# Server disconnection during operation
333+
TRANSIENT_ERROR,
334+
# All errors count as failures
335+
ALL_ERRORS
336+
}
337+
```
338+
#### Usage Example
339+
```ballerina
340+
ftp:ClientConfiguration ftpConfig = {
341+
protocol: ftp:FTP,
342+
host: "ftp.example.com",
343+
port: 21,
344+
auth: {
345+
credentials: {username: "user", password: "pass"}
346+
},
347+
circuitBreaker: {
348+
failureThreshold: 0.5,
349+
resetTime: 30,
350+
rollingWindow: {
351+
requestVolumeThreshold: 5,
352+
timeWindow: 60,
353+
bucketSize: 10
354+
},
355+
failureCategories: [ftp:CONNECTION_ERROR, ftp:TRANSIENT_ERROR]
356+
}
357+
};
358+
359+
ftp:Client ftpClient = check new(ftpConfig);
360+
361+
// Operations will fail fast with CircuitBreakerOpenError when circuit is open
362+
byte[]|ftp:Error content = ftpClient->getBytes("/file.txt");
363+
if content is ftp:CircuitBreakerOpenError {
364+
// Handle circuit breaker open state - server is unavailable
365+
}
366+
```
367+
### 3.3. Initialization
368+
#### 3.3.1. Insecure Client
324369
A simple insecure client can be initialized by providing `ftp:FTP` as the protocol and the host and optionally, the port
325370
to the `ftp:ClientConfiguration`.
326371
```ballerina
@@ -330,7 +375,7 @@ to the `ftp:ClientConfiguration`.
330375
# + return - `ftp:Error` in case of errors or `()` otherwise
331376
public isolated function init(ClientConfiguration clientConfig) returns Error?;
332377
```
333-
#### 3.2.2. Secure Client
378+
#### 3.3.2. Secure Client
334379
A secure client can be initialized by providing `ftp:SFTP` as the protocol and by providing `ftp:Credentials`
335380
and `ftp:PrivateKey` to `ftp:AuthConfiguration`.
336381
```ballerina
@@ -347,7 +392,7 @@ ftp:ClientConfiguration ftpConfig = {
347392
userDirIsRoot: true
348393
};
349394
```
350-
#### 3.2.3. Client with Retry Configuration
395+
#### 3.3.3. Client with Retry Configuration
351396
A client can be initialized with retry configuration to automatically retry failed read operations:
352397
```ballerina
353398
ftp:ClientConfiguration ftpConfig = {
@@ -367,7 +412,7 @@ ftp:Client ftpClient = check new(ftpConfig);
367412
// All read operations will automatically retry on failure
368413
byte[] bytes = check ftpClient->getBytes("/path/to/file.txt");
369414
```
370-
### 3.3. Functions
415+
### 3.4. Functions
371416
* FTP Client API can be used to put files on the FTP server. For this, the `put()` method can be used.
372417
```ballerina
373418
# Adds a file to FTP server.

0 commit comments

Comments
 (0)