go-zero/rest/handler/breakerhandler.go

43 lines
1.2 KiB
Go
Raw Normal View History

2020-07-29 18:00:04 +08:00
package handler
2020-07-26 17:09:05 +08:00
import (
"fmt"
"net/http"
"strings"
"github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/internal/response"
2020-07-26 17:09:05 +08:00
)
const breakerSeparator = "://"
2021-03-01 19:15:35 +08:00
// BreakerHandler returns a break circuit middleware.
2020-07-26 17:09:05 +08:00
func BreakerHandler(method, path string, metrics *stat.Metrics) func(http.Handler) http.Handler {
brk := breaker.NewBreaker(breaker.WithName(strings.Join([]string{method, path}, breakerSeparator)))
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
promise, err := brk.Allow()
if err != nil {
metrics.AddDrop()
logx.Errorf("[http] dropped, %s - %s - %s",
2020-08-12 12:25:52 +08:00
r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent())
2020-07-26 17:09:05 +08:00
w.WriteHeader(http.StatusServiceUnavailable)
return
}
cw := &response.WithCodeResponseWriter{Writer: w}
2020-07-26 17:09:05 +08:00
defer func() {
if cw.Code < http.StatusInternalServerError {
promise.Accept()
} else {
promise.Reject(fmt.Sprintf("%d %s", cw.Code, http.StatusText(cw.Code)))
}
}()
next.ServeHTTP(cw, r)
})
}
}