Baoyuantop commented on code in PR #12765:
URL: https://github.com/apache/apisix/pull/12765#discussion_r2726898667
##########
apisix/plugins/api-breaker.lua:
##########
@@ -23,245 +23,648 @@ local error = error
local ipairs = ipairs
-local shared_buffer = ngx.shared["plugin-".. plugin_name]
+local shared_buffer = ngx.shared["plugin-" .. plugin_name]
if not shared_buffer then
- error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+ error("failed to get ngx.shared dict when load plugin " .. plugin_name)
end
+-- Circuit breaker states (only for ratio policy)
+local CLOSED = 0
+local OPEN = 1
+local HALF_OPEN = 2
local schema = {
- type = "object",
+ type = "object",
+ properties = {
+ break_response_code = {
+ type = "integer",
+ minimum = 200,
+ maximum = 599,
+ },
+ break_response_body = {
+ type = "string"
+ },
+ break_response_headers = {
+ type = "array",
+ items = {
+ type = "object",
+ properties = {
+ key = {
+ type = "string",
+ minLength = 1
+ },
+ value = {
+ type = "string",
+ minLength = 1
+ }
+ },
+ required = { "key", "value" },
+ }
+ },
+ max_breaker_sec = {
+ type = "integer",
+ minimum = 3,
+ default = 300,
+ description = "Circuit breaker duration in seconds (applies to both
count and ratio policies)"
+ },
+ policy = {
+ type = "string",
+ enum = { "unhealthy-count", "unhealthy-ratio" },
+ default = "unhealthy-count",
+ }
+ },
+ required = { "break_response_code" },
+ ["if"] = {
+ properties = {
+ policy = {
+ enum = { "unhealthy-count" },
+ },
+ },
+ },
+ ["then"] = {
properties = {
- break_response_code = {
+ unhealthy = {
+ type = "object",
+ properties = {
+ http_statuses = {
+ type = "array",
+ minItems = 1,
+ items = {
+ type = "integer",
+ minimum = 500,
+ maximum = 599,
+ },
+ uniqueItems = true,
+ default = { 500 }
+ },
+ failures = {
type = "integer",
- minimum = 200,
- maximum = 599,
+ minimum = 1,
+ default = 3,
+ }
},
- break_response_body = {
- type = "string"
- },
- break_response_headers = {
+ default = { http_statuses = { 500 }, failures = 3 }
+ },
+ healthy = {
+ type = "object",
+ properties = {
+ http_statuses = {
type = "array",
+ minItems = 1,
items = {
- type = "object",
- properties = {
- key = {
- type = "string",
- minLength = 1
- },
- value = {
- type = "string",
- minLength = 1
- }
- },
- required = {"key", "value"},
- }
- },
- max_breaker_sec = {
+ type = "integer",
+ minimum = 200,
+ maximum = 499,
+ },
+ uniqueItems = true,
+ default = { 200 }
+ },
+ successes = {
type = "integer",
- minimum = 3,
- default = 300,
+ minimum = 1,
+ default = 3,
+ }
+ },
+ default = { http_statuses = { 200 }, successes = 3 }
+ }
+ }
+ },
+ ["else"] = {
+ ["if"] = {
+ properties = {
+ policy = {
+ enum = { "unhealthy-ratio" },
},
+ },
+ },
+ ["then"] = {
+ properties = {
unhealthy = {
- type = "object",
- properties = {
- http_statuses = {
- type = "array",
- minItems = 1,
- items = {
- type = "integer",
- minimum = 500,
- maximum = 599,
- },
- uniqueItems = true,
- default = {500}
- },
- failures = {
- type = "integer",
- minimum = 1,
- default = 3,
- }
+ type = "object",
+ properties = {
+ http_statuses = {
+ type = "array",
+ minItems = 1,
+ items = {
+ type = "integer",
+ minimum = 500,
+ maximum = 599,
+ },
+ uniqueItems = true,
+ default = { 500 }
+ },
+ error_ratio = {
+ type = "number",
+ minimum = 0,
+ maximum = 1,
+ default = 0.5,
+ description = "Failure rate threshold to trigger circuit breaker"
+ },
+ min_request_threshold = {
+ type = "integer",
+ minimum = 1,
+ default = 10,
+ description = "Minimum number of calls before circuit breaker
can be triggered"
+ },
+ sliding_window_size = {
+ type = "integer",
+ minimum = 10,
+ maximum = 3600,
+ default = 300,
+ description = "Size of the sliding window in seconds"
},
- default = {http_statuses = {500}, failures = 3}
+ half_open_max_calls = {
+ type = "integer",
+ minimum = 1,
+ maximum = 20,
+ default = 3,
+ description = "Number of permitted calls when circuit breaker is
half-open"
+ }
+ },
+ default = {
+ http_statuses = { 500 },
+ error_ratio = 0.5,
+ min_request_threshold = 10,
+ sliding_window_size = 300,
+ half_open_max_calls = 3
+ }
},
healthy = {
- type = "object",
- properties = {
- http_statuses = {
- type = "array",
- minItems = 1,
- items = {
- type = "integer",
- minimum = 200,
- maximum = 499,
- },
- uniqueItems = true,
- default = {200}
- },
- successes = {
- type = "integer",
- minimum = 1,
- default = 3,
- }
+ type = "object",
+ properties = {
+ http_statuses = {
+ type = "array",
+ minItems = 1,
+ items = {
+ type = "integer",
+ minimum = 200,
+ maximum = 499,
+ },
+ uniqueItems = true,
+ default = { 200 }
},
- default = {http_statuses = {200}, successes = 3}
+ success_ratio = {
+ type = "number",
+ minimum = 0,
+ maximum = 1,
+ default = 0.6,
+ description = "Success rate threshold to close circuit breaker
from half-open state"
+ }
+ },
+ default = { http_statuses = { 200 }, success_ratio = 0.6 }
}
- },
- required = {"break_response_code"},
+ }
+ }
+ }
}
-
+-- Key generation functions (based on latest APISIX version)
local function gen_healthy_key(ctx)
- return "healthy-" .. core.request.get_host(ctx) .. ctx.var.uri
+ return "healthy-" .. core.request.get_host(ctx) .. ctx.var.uri
end
-
local function gen_unhealthy_key(ctx)
- return "unhealthy-" .. core.request.get_host(ctx) .. ctx.var.uri
+ return "unhealthy-" .. core.request.get_host(ctx) .. ctx.var.uri
end
-
local function gen_lasttime_key(ctx)
- return "unhealthy-lasttime" .. core.request.get_host(ctx) .. ctx.var.uri
+ return "unhealthy-lasttime" .. core.request.get_host(ctx) .. ctx.var.uri
+end
+
+-- New key generation functions for ratio policy
+local function gen_state_key(ctx)
+ return "cb-state-" .. core.request.get_host(ctx) .. ctx.var.uri
+end
+
+local function gen_total_requests_key(ctx)
+ return "cb-total-" .. core.request.get_host(ctx) .. ctx.var.uri
end
+local function gen_window_start_time_key(ctx)
+ return "cb-window-" .. core.request.get_host(ctx) .. ctx.var.uri
+end
+
+local function gen_last_state_change_key(ctx)
+ return "cb-last-change-" .. core.request.get_host(ctx) .. ctx.var.uri
+end
+
+local function gen_half_open_calls_key(ctx)
+ return "cb-half-open-calls-" .. core.request.get_host(ctx) .. ctx.var.uri
+end
+
+local function gen_half_open_success_key(ctx)
+ return "cb-half-open-success-" .. core.request.get_host(ctx) .. ctx.var.uri
+end
local _M = {
- version = 0.1,
- name = plugin_name,
- priority = 1005,
- schema = schema,
+ version = 0.1,
Review Comment:
Please change all indentation to 4 spaces.
##########
t/lib/server.lua:
##########
@@ -78,7 +78,6 @@ function _M.hello()
ngx.say(s)
end
-
Review Comment:
Please remove these irrelevant formatting changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]