Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: fix goroutine leak risk #3091

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion server/config/persist_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
type PersistOptions struct {
// configuration -> ttl value
ttl map[string]*cache.TTLString
ttlCancel map[string]context.CancelFunc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why create cache.TTLString for each key, instead of use only 1 cache.TTLString for all keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For each configuration, they could have different ttl.

schedule atomic.Value
replication atomic.Value
pdServerConfig atomic.Value
Expand All @@ -53,6 +54,7 @@ func NewPersistOptions(cfg *Config) *PersistOptions {
o.labelProperty.Store(cfg.LabelProperty)
o.SetClusterVersion(&cfg.ClusterVersion)
o.ttl = make(map[string]*cache.TTLString, 6)
o.ttlCancel = make(map[string]context.CancelFunc, 6)
return o
}

Expand Down Expand Up @@ -597,12 +599,15 @@ func (o *PersistOptions) CheckLabelProperty(typ string, labels []*metapb.StoreLa
}

// SetTTLData set temporary configuration
func (o *PersistOptions) SetTTLData(ctx context.Context, key string, value interface{}, ttl time.Duration) {
func (o *PersistOptions) SetTTLData(parCtx context.Context, key string, value interface{}, ttl time.Duration) {
if data, ok := o.ttl[key]; ok {
data.Clear()
o.ttlCancel[key]()
}
ctx, cancel := context.WithCancel(parCtx)
o.ttl[key] = cache.NewStringTTL(ctx, 5*time.Second, ttl)
o.ttl[key].Put(key, value)
o.ttlCancel[key] = cancel
}

func (o *PersistOptions) getTTLData(key string) (interface{}, bool) {
Expand Down