nanjiek commented on code in PR #876:
URL: https://github.com/apache/dubbo-go-pixiu/pull/876#discussion_r2772993870
##########
pkg/filter/sentinel/circuitbreaker/circuit_breaker.go:
##########
@@ -101,7 +107,40 @@ func (f *Filter) Decode(ctx *http.HttpContext)
filter.FilterStatus {
ctx.SendLocalReply(errResp.Status, errResp.ToJSON())
return filter.Stop
}
+
+ // Store entry in context for later use in Encode phase
+ if ctx.Params == nil {
+ ctx.Params = make(map[string]any)
+ }
+ ctx.Params[ContextKeySentinelEntry] = entry
+
+ return filter.Continue
+}
+
+// Encode processes the response and reports statistics to Sentinel
+func (f *Filter) Encode(ctx *http.HttpContext) filter.FilterStatus {
+ entryVal, ok := ctx.Params[ContextKeySentinelEntry]
+ if !ok {
+ // No entry in context, skip
+ return filter.Continue
+ }
+
+ entry, ok := entryVal.(*base.SentinelEntry)
+ if !ok || entry == nil {
+ logger.Warnf("Invalid sentinel entry type in context")
+ return filter.Continue
+ }
+
+ // Ensure entry.Exit() is called
defer entry.Exit()
+
+ // Report error to Sentinel if response indicates failure
+ // Consider 5xx status codes as errors for circuit breaker
+ statusCode := ctx.GetStatusCode()
+ if statusCode >= 500 && statusCode < 600 {
+ entry.SetError(errors.New("backend service error"))
Review Comment:
错误处理机制请使用pkg\context\http里定义的错误码替换500,配合SendLocalReply函数完成。
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]