breakerinterceptor.go 600 B

12345678910111213141516171819
  1. package clientinterceptors
  2. import (
  3. "context"
  4. "path"
  5. "git.i2edu.net/i2/go-zero/core/breaker"
  6. "git.i2edu.net/i2/go-zero/zrpc/internal/codes"
  7. "google.golang.org/grpc"
  8. )
  9. // BreakerInterceptor is an interceptor that acts as a circuit breaker.
  10. func BreakerInterceptor(ctx context.Context, method string, req, reply interface{},
  11. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  12. breakerName := path.Join(cc.Target(), method)
  13. return breaker.DoWithAcceptable(breakerName, func() error {
  14. return invoker(ctx, method, req, reply, cc, opts...)
  15. }, codes.Acceptable)
  16. }