grpc_interceptor

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 2 Imported by: 0

README

grpc_interceptor

grpc interceptor

usage

server
func ServerDemo1(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, 
handler grpc.UnaryHandler) (interface{}, error) {

    log.Printf("before handling. Info: %+v", info)
    resp, err := handler(ctx, req)
    log.Printf("after handling. resp: %+v", resp)
    return rsp, err
}

func ServerDemo2(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo,
handler grpc.StreamHandler) error {

    log.Printf("before handling. Info: %+v", info)
    err := handler(srv, ss)
    log.Printf("after handling. err: %v", err)
    return err
}

...



// Load interceptor ServerDemo1, ignore on /package.Service/Do1 method
// Same set of interceptors using a common white list
unaryMd := &grpc_interceptor.UnaryServerInterceptors{}
unaryMd.UseGlobal([]grpc.UnaryServerInterceptor{
    ServerDemo1,
    ...
}, "/package.Service/Do1")

// Load interceptor ServerDemo2, ignore on /package.Service/Do2 method
streamMd := &grpc_interceptor.StreamServerInterceptors{}
streamMd.UseGlobal([]grpc.StreamServerInterceptor{ServerDemo2}, "/package.Service/Do2")


s := grpc.NewServer(
        grpc.StreamInterceptor(streamMd.StreamServerInterceptor()),
        grpc.UnaryInterceptor(unaryMd.UnaryServerInterceptor()),
)
client
func clientDemo1(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {

	log.Printf("before invoker. method: %+v, request:%+v", method, req)
	err := invoker(ctx, method, req, reply, cc, opts...)
	log.Printf("after invoker. reply: %+v", reply)
	return err
}

func clientDemo2(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, 
streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {

	log.Printf("before invoker. method: %+v, StreamDesc:%+v", method, desc)
	clientStream, err := streamer(ctx, desc, cc, method, opts...)
	log.Printf("before invoker. method: %+v", method)
	return clientStream, err
}

...


unaryMd := &grpc_interceptor.UnaryClientInterceptors{}
unaryMd.UseGlobal([]grpc.UnaryClientInterceptor{
    clientDemo1,
    ...
})

// Load the interceptor clientDemo2 for /package.Service/Do2 methods only
streamMd := &grpc_interceptor.StreamClientInterceptors{}
streamMd.UseMethod("/package.Service/Do2", clientDemo2)

grpc.Dial(*address, grpc.WithInsecure(), 
    grpc.WithUnaryInterceptor(unaryMd.UnaryClientInterceptor()),
	grpc.WithStreamInterceptor(streamMd.StreamClientInterceptor()),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StreamClientInterceptors

type StreamClientInterceptors struct {
	// contains filtered or unexported fields
}

func (*StreamClientInterceptors) StreamClientInterceptor

func (sci *StreamClientInterceptors) StreamClientInterceptor() grpc.StreamClientInterceptor

func (*StreamClientInterceptors) UseGlobal

func (sci *StreamClientInterceptors) UseGlobal(interceptors []grpc.StreamClientInterceptor, skipMethods ...string)

func (*StreamClientInterceptors) UseMethod

func (sci *StreamClientInterceptors) UseMethod(method string, interceptors ...grpc.StreamClientInterceptor)

type StreamServerInterceptors

type StreamServerInterceptors struct {
	// contains filtered or unexported fields
}

func (*StreamServerInterceptors) StreamServerInterceptor

func (ssi *StreamServerInterceptors) StreamServerInterceptor() grpc.StreamServerInterceptor

func (*StreamServerInterceptors) UseGlobal

func (ssi *StreamServerInterceptors) UseGlobal(interceptors []grpc.StreamServerInterceptor, skipMethods ...string)

func (*StreamServerInterceptors) UseMethod

func (ssi *StreamServerInterceptors) UseMethod(method string, interceptors ...grpc.StreamServerInterceptor)

type UnaryClientInterceptors

type UnaryClientInterceptors struct {
	// contains filtered or unexported fields
}

func (*UnaryClientInterceptors) UnaryClientInterceptor

func (uci *UnaryClientInterceptors) UnaryClientInterceptor() grpc.UnaryClientInterceptor

func (*UnaryClientInterceptors) UseGlobal

func (uci *UnaryClientInterceptors) UseGlobal(interceptors []grpc.UnaryClientInterceptor, skipMethods ...string)

func (*UnaryClientInterceptors) UseMethod

func (uci *UnaryClientInterceptors) UseMethod(method string, interceptors ...grpc.UnaryClientInterceptor)

type UnaryServerInterceptors

type UnaryServerInterceptors struct {
	// contains filtered or unexported fields
}

func (*UnaryServerInterceptors) UnaryServerInterceptor

func (usi *UnaryServerInterceptors) UnaryServerInterceptor() grpc.UnaryServerInterceptor

func (*UnaryServerInterceptors) UseGlobal

func (usi *UnaryServerInterceptors) UseGlobal(interceptors []grpc.UnaryServerInterceptor, skipMethods ...string)

func (*UnaryServerInterceptors) UseMethod

func (usi *UnaryServerInterceptors) UseMethod(method string, interceptors ...grpc.UnaryServerInterceptor)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL