2021-08-04 18:45:05 +08:00
|
|
|
package serverinterceptors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-01-04 15:51:32 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/breaker"
|
|
|
|
"github.com/zeromicro/go-zero/zrpc/internal/codes"
|
2021-08-04 18:45:05 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StreamBreakerInterceptor is an interceptor that acts as a circuit breaker.
|
|
|
|
func StreamBreakerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
|
|
|
|
handler grpc.StreamHandler) (err error) {
|
|
|
|
breakerName := info.FullMethod
|
|
|
|
return breaker.DoWithAcceptable(breakerName, func() error {
|
|
|
|
return handler(srv, stream)
|
|
|
|
}, codes.Acceptable)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnaryBreakerInterceptor is an interceptor that acts as a circuit breaker.
|
2021-10-03 20:53:50 +08:00
|
|
|
func UnaryBreakerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
|
|
breakerName := info.FullMethod
|
|
|
|
err = breaker.DoWithAcceptable(breakerName, func() error {
|
|
|
|
var err error
|
|
|
|
resp, err = handler(ctx, req)
|
|
|
|
return err
|
|
|
|
}, codes.Acceptable)
|
2021-08-04 18:45:05 +08:00
|
|
|
|
2021-10-03 20:53:50 +08:00
|
|
|
return resp, err
|
2021-08-04 18:45:05 +08:00
|
|
|
}
|