Skip to content

Error Handling

Retry, fallback, and circuit breaker patterns.

3 modules

ModuleDescription
Circuit BreakerBảo vệ chống lại lỗi dây chuyền với mẫu circuit breaker
Dự phòngCung cấp giá trị dự phòng khi hoạt động thất bại
Thử lạiBao bọc các hoạt động với logic thử lại có thể cấu hình

Modules

Circuit Breaker

error.circuit_breaker

Bảo vệ chống lại lỗi dây chuyền với mẫu circuit breaker

Parameters:

NameTypeRequiredDefaultDescription
actionobjectYes-Hành động được bảo vệ với circuit breaker
circuit_idstringYes-Hành động được bảo vệ với circuit breaker
failure_thresholdnumberNo5Định danh duy nhất cho circuit này (để theo dõi trạng thái)
failure_window_msnumberNo60000Cửa sổ thời gian để đếm thất bại
recovery_timeout_msnumberNo30000Thời gian trước khi thử khôi phục (trạng thái nửa mở)
success_thresholdnumberNo3Số yêu cầu thành công cần thiết trong trạng thái nửa mở để đóng circuit
fallbackobjectNo-Hành động thay thế khi circuit mở
fallback_valueanyNo-Hành động thay thế khi circuit mở
track_errorsarrayNo[]Giá trị tĩnh để trả về khi circuit mở

Output:

FieldTypeDescription
__event__stringChỉ đếm các mã lỗi này vào ngưỡng (để trống = tất cả)
resultanySự kiện để định tuyến (thành công/circuit mở/dự phòng)
circuit_statestringKết quả từ hành động hoặc dự phòng
failure_countnumberTrạng thái hiện tại của circuit (đóng/mở/nửa mở)
last_failure_timestringSố lần thất bại hiện tại trong cửa sổ
circuit_opened_atstringDấu thời gian của lần thất bại cuối

Example: Example

yaml
action: {"module": "http.post", "params": {"url": "https://api.example.com/submit"}}
circuit_id: example-api
failure_threshold: 5
failure_window_ms: 60000
recovery_timeout_ms: 30000

Example: Example

yaml
action: {"module": "http.get", "params": {"url": "https://api.example.com/data"}}
circuit_id: data-api
failure_threshold: 3
fallback: {"module": "cache.get", "params": {"key": "data_cache"}}

Example: Example

yaml
action: {"module": "database.query", "params": {"query": "SELECT * FROM users"}}
circuit_id: database
failure_threshold: 3
fallback_value: {"users": [], "from_cache": false}

Dự phòng

error.fallback

Cung cấp giá trị dự phòng khi hoạt động thất bại

Parameters:

NameTypeRequiredDefaultDescription
operationobjectNo-Hoạt động chính để thử
fallback_valueanyNo-Hoạt động chính để thử
fallback_operationobjectNo-Giá trị tĩnh để trả về khi thất bại
fallback_onarrayNo[]Hoạt động thay thế để thực hiện khi thất bại
include_error_infobooleanNoTrueMã lỗi kích hoạt dự phòng (để trống = tất cả lỗi)
log_fallbackbooleanNoTrueBao gồm thông tin lỗi gốc trong đầu ra

Output:

FieldTypeDescription
resultanyGhi lại khi sử dụng dự phòng
used_fallbackbooleanKết quả từ hoạt động chính hoặc dự phòng
sourcestringCó sử dụng dự phòng hay không
original_errorobjectNguồn của kết quả (chính/dự phòng giá trị/dự phòng hoạt động)

Example: Example

yaml
operation: {"module": "http.get", "params": {"url": "https://api.example.com/items"}}
fallback_value: []

Example: Example

yaml
operation: {"module": "http.get", "params": {"url": "https://api.example.com/config"}}
fallback_operation: {"module": "cache.get", "params": {"key": "config_cache"}}

Example: Example

yaml
operation: {"module": "api.call", "params": {"endpoint": "/data"}}
fallback_value: {"status": "unavailable"}
fallback_on: ["NETWORK_ERROR", "TIMEOUT_ERROR"]

Thử lại

error.retry

Bao bọc các hoạt động với logic thử lại có thể cấu hình

Parameters:

NameTypeRequiredDefaultDescription
operationobjectYes-Hoạt động cần thử lại (ID module và tham số)
max_retriesnumberNo3Hoạt động cần thử lại (ID module và tham số)
initial_delay_msnumberNo1000Số lần thử lại tối đa
max_delay_msnumberNo30000Độ trễ ban đầu trước lần thử lại đầu tiên
backoff_multipliernumberNo2.0Hệ số nhân cho backoff lũy thừa (ví dụ: 2 sẽ tăng gấp đôi độ trễ mỗi lần thử lại)
jitterbooleanNoTrueThêm độ trễ ngẫu nhiên để tránh hiện tượng bầy đàn
retry_onarrayNo[]Thêm độ trễ ngẫu nhiên để tránh hiện tượng bầy đàn
timeout_per_attempt_msnumberNo0Danh sách mã lỗi cần thử lại (để trống = thử lại tất cả)

Output:

FieldTypeDescription
__event__stringThời gian chờ cho mỗi lần thử (0 để không có thời gian chờ)
resultanySự kiện để định tuyến (thành công/hết lần thử)
attemptsnumberSự kiện để định tuyến (thành công/hết lần thử)
total_delay_msnumberKết quả từ lần thử thành công
errorsarraySố lần thử đã thực hiện

Example: Example

yaml
operation: {"module": "http.get", "params": {"url": "https://api.example.com/data"}}
max_retries: 3

Example: Example

yaml
operation: {"module": "database.query", "params": {"query": "SELECT * FROM users"}}
max_retries: 5
initial_delay_ms: 2000
backoff_multiplier: 2.0
jitter: true

Example: Example

yaml
operation: {"module": "api.call", "params": {"endpoint": "/submit"}}
max_retries: 3
retry_on: ["NETWORK_ERROR", "TIMEOUT_ERROR", "SERVICE_UNAVAILABLE"]

Released under the Apache 2.0 License.