interceptor

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: Apache-2.0 Imports: 3 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithRPCInterceptor

func WithRPCInterceptor(ctx context.Context, interceptor RPCInterceptor) context.Context

WithRPCInterceptor is a helper function used to bind RPCInterceptor with ctx.

Types

type MockInterceptorManager

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

MockInterceptorManager can be used to create Interceptor and record the number of executions of the created Interceptor.

func NewMockInterceptorManager

func NewMockInterceptorManager() *MockInterceptorManager

NewMockInterceptorManager creates an empty MockInterceptorManager.

func (*MockInterceptorManager) BeginCount

func (m *MockInterceptorManager) BeginCount() int

BeginCount gets how many times the previously created Interceptor has been executed.

func (*MockInterceptorManager) CreateMockInterceptor

func (m *MockInterceptorManager) CreateMockInterceptor(name string) RPCInterceptor

CreateMockInterceptor creates an RPCInterceptor for testing.

func (*MockInterceptorManager) EndCount

func (m *MockInterceptorManager) EndCount() int

EndCount gets how many times the previously created Interceptor has been returned.

func (*MockInterceptorManager) ExecLog

func (m *MockInterceptorManager) ExecLog() []string

ExecLog gets execution log of all interceptors.

func (*MockInterceptorManager) Reset

func (m *MockInterceptorManager) Reset()

Reset clear all counters.

type RPCInterceptor

type RPCInterceptor func(next RPCInterceptorFunc) RPCInterceptorFunc

RPCInterceptor is used to decorate the RPC requests to TiKV.

The definition of an interceptor is: Given an RPCInterceptorFunc, we will get the decorated RPCInterceptorFunc with additional logic before and after the execution of the given RPCInterceptorFunc.

The decorated RPCInterceptorFunc will be executed before and after the real RPC request is initiated to TiKV.

We can implement an RPCInterceptor like this: ```

func LogInterceptor(next InterceptorFunc) RPCInterceptorFunc {
    return func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
        log.Println("before")
        resp, err := next(target, req)
        log.Println("after")
        return resp, err
    }
}

txn.SetRPCInterceptor(LogInterceptor) ```

Or you want to inject some dependent modules: ```

func GetLogInterceptor(lg *log.Logger) RPCInterceptor {
    return func(next RPCInterceptorFunc) RPCInterceptorFunc {
        return func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
            lg.Println("before")
            resp, err := next(target, req)
            lg.Println("after")
            return resp, err
        }
    }
}

txn.SetRPCInterceptor(GetLogInterceptor()) ```

NOTE: Interceptor calls may not correspond one-to-one with the underlying gRPC requests. This is because there may be some exceptions, such as: request batched, no valid connection etc. If you have questions about the execution location of RPCInterceptor, please refer to:

tikv/kv.go#NewKVStore()
internal/client/client_interceptor.go#SendRequest.

func ChainRPCInterceptors

func ChainRPCInterceptors(its ...RPCInterceptor) RPCInterceptor

ChainRPCInterceptors chains multiple RPCInterceptors into one. Multiple RPCInterceptors will be executed in the order of their parameters. See RPCInterceptorChain for more information.

func GetRPCInterceptorFromCtx

func GetRPCInterceptorFromCtx(ctx context.Context) RPCInterceptor

GetRPCInterceptorFromCtx gets the RPCInterceptor bound by the previous call to WithRPCInterceptor, and returns nil if there is none.

type RPCInterceptorChain

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

RPCInterceptorChain is used to combine multiple interceptors into one. Multiple interceptors will be executed in the order of link time, but are more similar to the onion model: The earlier the interceptor is executed, the later it will return.

We can use RPCInterceptorChain like this: ```

func Interceptor1(next InterceptorFunc) RPCInterceptorFunc {
    return func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
        fmt.Println("begin-interceptor-1")
        defer fmt.Println("end-interceptor-1")
        return next(target, req)
    }
}
func Interceptor2(next InterceptorFunc) RPCInterceptorFunc {
    return func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
        fmt.Println("begin-interceptor-2")
        defer fmt.Println("end-interceptor-2")
        return next(target, req)
    }
}

txn.SetRPCInterceptor(NewRPCInterceptorChain().Link(Interceptor1).Link(Interceptor2).Build()) ```

Then every time an RPC request is initiated, the following text will be printed: ``` begin-interceptor-1 begin-interceptor-2 /* do request & respond here */ end-interceptor-2 end-interceptor-1 ```

func NewRPCInterceptorChain

func NewRPCInterceptorChain() *RPCInterceptorChain

NewRPCInterceptorChain creates an empty RPCInterceptorChain.

func (*RPCInterceptorChain) Build

Build merges the previously linked interceptors into one.

Link is used to link the next RPCInterceptor. Multiple interceptors will be executed in the order of link time.

type RPCInterceptorFunc

type RPCInterceptorFunc func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error)

RPCInterceptorFunc is a callable function used to initiate a request to TiKV. It is mainly used as the parameter and return value of RPCInterceptor.

Jump to

Keyboard shortcuts

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