feat: add option example

This commit is contained in:
zhuyasen 2025-07-27 15:48:13 +08:00
parent 08d1d00b9e
commit af791196cf
3 changed files with 50 additions and 10 deletions

View File

@ -60,12 +60,28 @@ func NewRouter() *gin.Engine {
// limit middleware
if config.Get().App.EnableLimit {
r.Use(middleware.RateLimit())
r.Use(middleware.RateLimit(
//middleware.WithWindow(time.Second*5), // default 10s
//middleware.WithBucket(200), // default 100
//middleware.WithCPUThreshold(900), // default 800
))
}
// circuit breaker middleware
if config.Get().App.EnableCircuitBreaker {
r.Use(middleware.CircuitBreaker())
r.Use(middleware.CircuitBreaker(
//middleware.WithBreakerOption(
//circuitbreaker.WithSuccess(75), // default 60
//circuitbreaker.WithRequest(100), // default 100
//circuitbreaker.WithBucket(20), // default 10
//circuitbreaker.WithWindow(time.Second*3), // default 3s
//),
//middleware.WithDegradeHandler(handler), // Add degradation processing
middleware.WithValidCode( // Add error codes to trigger circuit breaking
errcode.InternalServerError.Code(),
errcode.ServiceUnavailable.Code(),
),
))
}
// trace middleware

View File

@ -60,15 +60,27 @@ func NewRouter_pbExample() *gin.Engine { //nolint
// limit middleware
if config.Get().App.EnableLimit {
r.Use(middleware.RateLimit())
r.Use(middleware.RateLimit(
//middleware.WithWindow(time.Second*5), // default 10s
//middleware.WithBucket(200), // default 100
//middleware.WithCPUThreshold(900), // default 800
))
}
// circuit breaker middleware
if config.Get().App.EnableCircuitBreaker {
r.Use(middleware.CircuitBreaker(
// set http code for circuit breaker, default already includes 500 and 503
middleware.WithValidCode(errcode.InternalServerError.Code()),
middleware.WithValidCode(errcode.ServiceUnavailable.Code()),
//middleware.WithBreakerOption(
//circuitbreaker.WithSuccess(75), // default 60
//circuitbreaker.WithRequest(100), // default 100
//circuitbreaker.WithBucket(20), // default 10
//circuitbreaker.WithWindow(time.Second*3), // default 3s
//),
//middleware.WithDegradeHandler(handler), // Add degradation processing
middleware.WithValidCode( // Add error codes to trigger circuit breaking
errcode.InternalServerError.Code(),
errcode.ServiceUnavailable.Code(),
),
))
}

View File

@ -186,15 +186,27 @@ func (s *grpcServer) unaryServerOptions() grpc.ServerOption {
// limit interceptor
if config.Get().App.EnableLimit {
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerRateLimit())
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerRateLimit(
//interceptor.WithWindow(time.Second*5), // default 10s
//interceptor.WithBucket(200), // default 100
//interceptor.WithCPUThreshold(900), // default 800
))
}
// circuit breaker interceptor
if config.Get().App.EnableCircuitBreaker {
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerCircuitBreaker(
// set rpc code for circuit breaker, default already includes codes.Internal and codes.Unavailable
interceptor.WithValidCode(ecode.StatusInternalServerError.Code()),
interceptor.WithValidCode(ecode.StatusServiceUnavailable.Code()),
//interceptor.WithBreakerOption(
//circuitbreaker.WithSuccess(75), // default 60
//circuitbreaker.WithRequest(100), // default 100
//circuitbreaker.WithBucket(10), // default 10
//circuitbreaker.WithWindow(time.Second*3), // default 3s
//),
//interceptor.WithUnaryServerDegradeHandler(handler), // Add degradation processing
interceptor.WithValidCode( // Add timeout error codes can also trigger circuit breaking
ecode.StatusInternalServerError.Code(),
ecode.StatusServiceUnavailable.Code(),
),
))
}