@@ -269,60 +269,60 @@ end
269269
270270-- Circuit breaker state management functions
271271local function get_circuit_breaker_state (ctx )
272- local state_key = gen_state_key (ctx )
273- local state , err = shared_buffer :get (state_key )
274- if err then
275- core .log .warn (" failed to get circuit breaker state: " , err )
276- return CLOSED
277- end
278- return state or CLOSED
272+ local state_key = gen_state_key (ctx )
273+ local state , err = shared_buffer :get (state_key )
274+ if err then
275+ core .log .warn (" failed to get circuit breaker state: " , err )
276+ return CLOSED
277+ end
278+ return state or CLOSED
279279end
280280
281281local function set_circuit_breaker_state (ctx , state )
282- local state_key = gen_state_key (ctx )
283- local last_change_key = gen_last_state_change_key (ctx )
284- local current_time = ngx .time ()
282+ local state_key = gen_state_key (ctx )
283+ local last_change_key = gen_last_state_change_key (ctx )
284+ local current_time = ngx .time ()
285285
286- shared_buffer :set (state_key , state )
287- shared_buffer :set (last_change_key , current_time )
286+ shared_buffer :set (state_key , state )
287+ shared_buffer :set (last_change_key , current_time )
288288
289- core .log .info (" Circuit breaker state changed to: " , state , " at: " , current_time )
289+ core .log .info (" Circuit breaker state changed to: " , state , " at: " , current_time )
290290end
291291
292292-- Sliding window management
293293local function reset_sliding_window (ctx , current_time , window_size )
294- local window_start_key = gen_window_start_time_key (ctx )
295- local total_requests_key = gen_total_requests_key (ctx )
296- local unhealthy_key = gen_unhealthy_key (ctx )
294+ local window_start_key = gen_window_start_time_key (ctx )
295+ local total_requests_key = gen_total_requests_key (ctx )
296+ local unhealthy_key = gen_unhealthy_key (ctx )
297297
298- shared_buffer :set (window_start_key , current_time )
299- shared_buffer :set (total_requests_key , 0 )
300- shared_buffer :set (unhealthy_key , 0 )
298+ shared_buffer :set (window_start_key , current_time )
299+ shared_buffer :set (total_requests_key , 0 )
300+ shared_buffer :set (unhealthy_key , 0 )
301301
302- -- Reset circuit breaker state to CLOSED when sliding window resets
303- shared_buffer :delete (gen_state_key (ctx ))
304- shared_buffer :delete (gen_last_state_change_key (ctx ))
305- shared_buffer :delete (gen_half_open_calls_key (ctx ))
306- shared_buffer :delete (gen_half_open_success_key (ctx ))
302+ -- Reset circuit breaker state to CLOSED when sliding window resets
303+ shared_buffer :delete (gen_state_key (ctx ))
304+ shared_buffer :delete (gen_last_state_change_key (ctx ))
305+ shared_buffer :delete (gen_half_open_calls_key (ctx ))
306+ shared_buffer :delete (gen_half_open_success_key (ctx ))
307307
308- core .log .info (" Sliding window reset at: " , current_time , " window size: " , window_size , " s" )
308+ core .log .info (" Sliding window reset at: " , current_time , " window size: " , window_size , " s" )
309309end
310310
311311local function check_and_reset_window (ctx , conf )
312- local current_time = ngx .time ()
313- local window_start_key = gen_window_start_time_key (ctx )
314- local window_start_time , err = shared_buffer :get (window_start_key )
312+ local current_time = ngx .time ()
313+ local window_start_key = gen_window_start_time_key (ctx )
314+ local window_start_time , err = shared_buffer :get (window_start_key )
315315
316- if err then
317- core .log .warn (" failed to get window start time: " , err )
318- return
319- end
316+ if err then
317+ core .log .warn (" failed to get window start time: " , err )
318+ return
319+ end
320320
321- local window_size = conf .unhealthy .sliding_window_size or 300
321+ local window_size = conf .unhealthy .sliding_window_size or 300
322322
323- if not window_start_time or (current_time - window_start_time ) >= window_size then
324- reset_sliding_window (ctx , current_time , window_size )
325- end
323+ if not window_start_time or (current_time - window_start_time ) >= window_size then
324+ reset_sliding_window (ctx , current_time , window_size )
325+ end
326326end
327327
328328-- Count-based circuit breaker (based on latest APISIX version)
@@ -487,12 +487,12 @@ local function ratio_based_access(conf, ctx)
487487end
488488
489489function _M .access (conf , ctx )
490- if conf .policy == " unhealthy-ratio" then
491- return ratio_based_access (conf , ctx )
492- else
493- -- Default to count-based (unhealthy-count)
494- return count_based_access (conf , ctx )
495- end
490+ if conf .policy == " unhealthy-ratio" then
491+ return ratio_based_access (conf , ctx )
492+ else
493+ -- Default to count-based (unhealthy-count)
494+ return count_based_access (conf , ctx )
495+ end
496496end
497497
498498-- Count-based logging (based on latest APISIX version)
@@ -549,7 +549,7 @@ local function count_based_log(conf, ctx)
549549 local healthy_count , err = shared_buffer :incr (healthy_key , 1 , 0 )
550550 if err then
551551 core .log .warn (" failed to `incr` healthy_key: " , healthy_key ,
552- " err: " , err )
552+ " err: " , err )
553553 end
554554
555555 -- clear related status
@@ -659,12 +659,12 @@ local function ratio_based_log(conf, ctx)
659659end
660660
661661function _M .log (conf , ctx )
662- if conf .policy == " unhealthy-ratio" then
663- ratio_based_log (conf , ctx )
664- else
665- -- Default to count-based (unhealthy-count)
666- count_based_log (conf , ctx )
667- end
662+ if conf .policy == " unhealthy-ratio" then
663+ ratio_based_log (conf , ctx )
664+ else
665+ -- Default to count-based (unhealthy-count)
666+ count_based_log (conf , ctx )
667+ end
668668end
669669
670670return _M
0 commit comments