Skip to content

Commit

Permalink
Add client_interceptor
Browse files Browse the repository at this point in the history
Signed-off-by: mornyx <mornyx.z@gmail.com>
  • Loading branch information
mornyx committed Dec 17, 2021
1 parent 7197af6 commit 0011200
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
10 changes: 0 additions & 10 deletions internal/client/client_collapse.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (

"github.com/pkg/errors"
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/tikvrpc/interceptor"
"golang.org/x/sync/singleflight"
)

Expand All @@ -66,15 +65,6 @@ func (r reqCollapse) Close() error {
}

func (r reqCollapse) SendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
if it := interceptor.GetRPCInterceptorFromCtx(ctx); it != nil {
return it(func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
return r.sendRequest(ctx, target, req, timeout)
})(addr, req)
}
return r.sendRequest(ctx, addr, req, timeout)
}

func (r reqCollapse) sendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
if r.Client == nil {
panic("client should not be nil")
}
Expand Down
43 changes: 43 additions & 0 deletions internal/client/client_interceptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2021 TiKV Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"context"
"time"

"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/tikvrpc/interceptor"
)

var _ Client = interceptedClient{}

type interceptedClient struct {
Client
}

// NewInterceptedClient creates a Client which can execute interceptor.
func NewInterceptedClient(client Client) Client {
return interceptedClient{client}
}

func (r interceptedClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
if it := interceptor.GetRPCInterceptorFromCtx(ctx); it != nil {
return it(func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
return r.Client.SendRequest(ctx, target, req, timeout)
})(addr, req)
}
return r.Client.SendRequest(ctx, addr, req, timeout)
}
2 changes: 1 addition & 1 deletion tikv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func NewKVStore(uuid string, pdClient pd.Client, spkv SafePointKV, tikvclient Cl
ctx: ctx,
cancel: cancel,
}
store.clientMu.client = client.NewReqCollapse(tikvclient)
store.clientMu.client = client.NewReqCollapse(client.NewInterceptedClient(tikvclient))
store.lockResolver = txnlock.NewLockResolver(store)

store.wg.Add(2)
Expand Down
6 changes: 3 additions & 3 deletions tikvrpc/interceptor/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ import (
// ```
//
// 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 collapsed, request batched,
// no valid connection etc. If you have questions about the execution location of RPCInterceptor,
// please refer to: internal/client/client_collapse.go#SendRequest.
// 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: internal/client/client_collapse.go#SendRequest.
type RPCInterceptor func(next RPCInterceptorFunc) RPCInterceptorFunc

// RPCInterceptorFunc is a callable function used to initiate a request to TiKV.
Expand Down

0 comments on commit 0011200

Please sign in to comment.