Skip to content

Commit

Permalink
Simplify the code
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Jan 20, 2023
1 parent f24b731 commit 313152f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
33 changes: 10 additions & 23 deletions internal/client/client_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
)

func init() {
resourceControlSwitch.Store(false)
resourceControlInterceptor = nil
ResourceControlSwitch.Store(false)
ResourceControlInterceptor = nil
}

var _ Client = interceptedClient{}
Expand Down Expand Up @@ -62,33 +62,20 @@ func (r interceptedClient) SendRequest(ctx context.Context, addr string, req *ti
}

var (
resourceControlSwitch atomic.Value
resourceControlInterceptor resourceControlClient.ResourceGroupKVInterceptor
// ResourceControlSwitch is used to control whether to enable the resource control.
ResourceControlSwitch atomic.Value
// ResourceControlInterceptor is used to build the resource control interceptor.
ResourceControlInterceptor resourceControlClient.ResourceGroupKVInterceptor
)

// EnableResourceControl enables the resource control.
func EnableResourceControl() {
resourceControlSwitch.Store(true)
}

// DisableResourceControl disables the resource control.
func DisableResourceControl() {
resourceControlSwitch.Store(false)
}

// SetResourceControlInterceptor sets the interceptor for resource control.
func SetResourceControlInterceptor(interceptor resourceControlClient.ResourceGroupKVInterceptor) {
resourceControlInterceptor = interceptor
}

// buildResourceControlInterceptor builds a resource control interceptor with
// the given resource group name.
func buildResourceControlInterceptor(
ctx context.Context,
req *tikvrpc.Request,
resourceGroupName string,
) interceptor.RPCInterceptor {
if !resourceControlSwitch.Load().(bool) {
if !ResourceControlSwitch.Load().(bool) {
return nil
}
// When the group name is empty or "default", we don't need to
Expand All @@ -97,22 +84,22 @@ func buildResourceControlInterceptor(
return nil
}
// No resource group interceptor is set.
if resourceControlInterceptor == nil {
if ResourceControlInterceptor == nil {
return nil
}
// Make the request info.
reqInfo := resourcecontrol.MakeRequestInfo(req)
// Build the interceptor.
return func(next interceptor.RPCInterceptorFunc) interceptor.RPCInterceptorFunc {
return func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
err := resourceControlInterceptor.OnRequestWait(ctx, resourceGroupName, reqInfo)
err := ResourceControlInterceptor.OnRequestWait(ctx, resourceGroupName, reqInfo)
if err != nil {
return nil, err
}
resp, err := next(target, req)
if resp != nil {
respInfo := resourcecontrol.MakeResponseInfo(resp)
resourceControlInterceptor.OnResponse(ctx, resourceGroupName, reqInfo, respInfo)
ResourceControlInterceptor.OnResponse(ctx, resourceGroupName, reqInfo, respInfo)
}
return resp, err
}
Expand Down
8 changes: 4 additions & 4 deletions tikv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,22 +585,22 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {

// EnableResourceControl enables the resource control.
func EnableResourceControl() {
client.EnableResourceControl()
client.ResourceControlSwitch.Store(true)
}

// DisableResourceControl disables the resource control.
func DisableResourceControl() {
client.DisableResourceControl()
client.ResourceControlSwitch.Store(false)
}

// SetResourceControlInterceptor sets the interceptor for resource control.
func SetResourceControlInterceptor(interceptor resourceControlClient.ResourceGroupKVInterceptor) {
client.SetResourceControlInterceptor(interceptor)
client.ResourceControlInterceptor = interceptor
}

// UnsetResourceControlInterceptor un-sets the interceptor for resource control.
func UnsetResourceControlInterceptor() {
client.SetResourceControlInterceptor(nil)
client.ResourceControlInterceptor = nil
}

// Variables defines the variables used by TiKV storage.
Expand Down

0 comments on commit 313152f

Please sign in to comment.