mirror of
https://github.com/go-kratos/kratos.git
synced 2025-02-05 13:15:11 +02:00
fix: middleware uses sentinel error (#1463)
This commit is contained in:
parent
558070f4d9
commit
6e6526efd9
@ -11,6 +11,9 @@ import (
|
|||||||
"github.com/go-kratos/kratos/v2/transport"
|
"github.com/go-kratos/kratos/v2/transport"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrNotAllowed is request failed due to circuit breaker triggered.
|
||||||
|
var ErrNotAllowed = errors.New(503, "CIRCUITBREAKER", "request failed due to circuit breaker triggered")
|
||||||
|
|
||||||
// Option is circuit breaker option.
|
// Option is circuit breaker option.
|
||||||
type Option func(*options)
|
type Option func(*options)
|
||||||
|
|
||||||
@ -46,7 +49,7 @@ func Client(opts ...Option) middleware.Middleware {
|
|||||||
// NOTE: when client reject requets locally,
|
// NOTE: when client reject requets locally,
|
||||||
// continue add counter let the drop ratio higher.
|
// continue add counter let the drop ratio higher.
|
||||||
breaker.MarkFailed()
|
breaker.MarkFailed()
|
||||||
return nil, errors.New(503, "CIRCUITBREAKER", "request failed due to circuit breaker triggered")
|
return nil, ErrNotAllowed
|
||||||
}
|
}
|
||||||
// allowed
|
// allowed
|
||||||
reply, err := handler(ctx, req)
|
reply, err := handler(ctx, req)
|
||||||
|
@ -9,6 +9,9 @@ import (
|
|||||||
"github.com/go-kratos/kratos/v2/middleware"
|
"github.com/go-kratos/kratos/v2/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrLimitExceed is service unavailable due to rate limit exceeded.
|
||||||
|
var ErrLimitExceed = errors.New(429, "RATELIMIT", "service unavailable due to rate limit exceeded")
|
||||||
|
|
||||||
// Option is ratelimit option.
|
// Option is ratelimit option.
|
||||||
type Option func(*options)
|
type Option func(*options)
|
||||||
|
|
||||||
@ -37,7 +40,7 @@ func Server(opts ...Option) middleware.Middleware {
|
|||||||
done, e := options.limiter.Allow()
|
done, e := options.limiter.Allow()
|
||||||
if e != nil {
|
if e != nil {
|
||||||
// rejected
|
// rejected
|
||||||
return nil, errors.New(429, "RATELIMIT", "service unavailable due to rate limit exceeded")
|
return nil, ErrLimitExceed
|
||||||
}
|
}
|
||||||
// allowed
|
// allowed
|
||||||
reply, err = handler(ctx, req)
|
reply, err = handler(ctx, req)
|
@ -2,7 +2,6 @@ package recovery
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/go-kratos/kratos/v2/errors"
|
"github.com/go-kratos/kratos/v2/errors"
|
||||||
@ -10,6 +9,9 @@ import (
|
|||||||
"github.com/go-kratos/kratos/v2/middleware"
|
"github.com/go-kratos/kratos/v2/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrUnknownRequest is unknown request error.
|
||||||
|
var ErrUnknownRequest = errors.InternalServer("UNKNOWN", "unknown request error")
|
||||||
|
|
||||||
// HandlerFunc is recovery handler func.
|
// HandlerFunc is recovery handler func.
|
||||||
type HandlerFunc func(ctx context.Context, req, err interface{}) error
|
type HandlerFunc func(ctx context.Context, req, err interface{}) error
|
||||||
|
|
||||||
@ -40,7 +42,7 @@ func Recovery(opts ...Option) middleware.Middleware {
|
|||||||
op := options{
|
op := options{
|
||||||
logger: log.DefaultLogger,
|
logger: log.DefaultLogger,
|
||||||
handler: func(ctx context.Context, req, err interface{}) error {
|
handler: func(ctx context.Context, req, err interface{}) error {
|
||||||
return errors.InternalServer("RECOVERY", fmt.Sprintf("panic triggered: %v", err))
|
return ErrUnknownRequest
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user