Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jul 4, 2023
1 parent c0cf773 commit a79f7e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
7 changes: 5 additions & 2 deletions internal/client/client_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ func buildResourceControlInterceptor(
return nil, err
}
req.GetResourceControlContext().Penalty = penalty
ruRuntimeStats.Update(consumption)
// Background requests don't need to update the runtime stats will record in TiKV.
if !util.IsBackgroundRequest(util.RequestSourceFromCtx(ctx)) {
ruRuntimeStats.Update(consumption)
}
resp, err := next(target, req)
if resp != nil {
if resp != nil && !util.IsBackgroundRequest(util.RequestSourceFromCtx(ctx)) {
respInfo := resourcecontrol.MakeResponseInfo(resp)
consumption, err = resourceControlInterceptor.OnResponse(resourceGroupName, reqInfo, respInfo)
if err != nil {
Expand Down
36 changes: 29 additions & 7 deletions util/request_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const (

const (
// InternalRequest is the scope of internal queries
InternalRequest = "internal_"
InternalRequest = "internal"
// ExternalRequest is the scope of external queries
ExternalRequest = "external_"
ExternalRequest = "external"
// SourceUnknown keeps same with the default value(empty string)
SourceUnknown = "unknown"
)
Expand All @@ -40,6 +40,9 @@ const (
type RequestSource struct {
RequestSourceInternal bool
RequestSourceType string
// ExplicitRequestSourceType is set from the session variable `explicit_request_source_type`. it's a complement of RequestSourceType.
// The value maybe "lightning", "br", "dumpling" etc.
ExplicitRequestSourceType string
}

// SetRequestSourceInternal sets the scope of the request source.
Expand Down Expand Up @@ -76,10 +79,24 @@ func (r *RequestSource) GetRequestSource() string {
if r == nil || r.RequestSourceType == "" {
return SourceUnknown
}
appendType := func(list []string) []string {
if len(r.ExplicitRequestSourceType) > 0 {
list = append(list, r.ExplicitRequestSourceType)
return list
}
return list
}
if r.RequestSourceInternal {
return InternalRequest + r.RequestSourceType
internalLabels := []string{InternalRequest, r.RequestSourceType}
internalLabels = appendType(internalLabels)
if len(r.ExplicitRequestSourceType) > 0 {
internalLabels = append(internalLabels, r.ExplicitRequestSourceType)
}
return strings.Join(internalLabels, "_")
}
return ExternalRequest + r.RequestSourceType
internalLabels := []string{ExternalRequest, r.RequestSourceType}
internalLabels = appendType(internalLabels)
return strings.Join(internalLabels, "_")
}

// RequestSourceFromCtx extract source from passed context.
Expand All @@ -96,15 +113,20 @@ func IsInternalRequest(source string) bool {
return strings.HasPrefix(source, InternalRequest)
}

// IsBackgroundRequest returns the type of the request source.
func IsBackgroundRequest(source string) bool {
return strings.Contains(source, "br") || strings.Contains(source, "lightning")
}

// ResourceGroupNameKeyType is the context key type of resource group name.
type resourceGroupNameKeyType struct{}

// ResourceGroupNameKey is used as the key of request source type in context.
var resourceGroupNameKey = resourceGroupNameKeyType{}

// WithResouceGroupName return a copy of the given context with a associated
// reosurce group name.
func WithResouceGroupName(ctx context.Context, groupName string) context.Context {
// WithResourceGroupName return a copy of the given context with a associated
// resource group name.
func WithResourceGroupName(ctx context.Context, groupName string) context.Context {
return context.WithValue(ctx, resourceGroupNameKey, groupName)
}

Expand Down

0 comments on commit a79f7e8

Please sign in to comment.