You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/ngin/handler/sheddinghandler.go

64 lines
1.3 KiB
Go

4 years ago
package handler
4 years ago
import (
"net/http"
"sync"
"zero/core/load"
"zero/core/logx"
"zero/core/stat"
4 years ago
"zero/ngin/internal"
"zero/ngin/internal/security"
4 years ago
)
const serviceType = "api"
var (
sheddingStat *load.SheddingStat
lock sync.Mutex
)
func SheddingHandler(shedder load.Shedder, metrics *stat.Metrics) func(http.Handler) http.Handler {
if shedder == nil {
return func(next http.Handler) http.Handler {
return next
}
}
ensureSheddingStat()
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sheddingStat.IncrementTotal()
promise, err := shedder.Allow()
if err != nil {
metrics.AddDrop()
sheddingStat.IncrementDrop()
logx.Errorf("[http] dropped, %s - %s - %s",
4 years ago
r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent())
4 years ago
w.WriteHeader(http.StatusServiceUnavailable)
return
}
4 years ago
cw := &security.WithCodeResponseWriter{Writer: w}
4 years ago
defer func() {
if cw.Code == http.StatusServiceUnavailable {
promise.Fail()
} else {
sheddingStat.IncrementPass()
promise.Pass()
}
}()
next.ServeHTTP(cw, r)
})
}
}
func ensureSheddingStat() {
lock.Lock()
if sheddingStat == nil {
sheddingStat = load.NewSheddingStat(serviceType)
}
lock.Unlock()
}