Skip to content

Commit

Permalink
cherry pick tikv#3091 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
Yisaer authored and ti-srebot committed Oct 22, 2020
1 parent a698d06 commit 0902b46
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions server/config/persist_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ import (
// PersistOptions wraps all configurations that need to persist to storage and
// allows to access them safely.
type PersistOptions struct {
<<<<<<< HEAD
=======
// configuration -> ttl value
ttl map[string]*cache.TTLString
ttlCancel map[string]context.CancelFunc
>>>>>>> 60775507... config: fix goroutine leak risk (#3091)
schedule atomic.Value
replication atomic.Value
pdServerConfig atomic.Value
Expand All @@ -50,6 +56,11 @@ func NewPersistOptions(cfg *Config) *PersistOptions {
o.replicationMode.Store(&cfg.ReplicationMode)
o.labelProperty.Store(cfg.LabelProperty)
o.SetClusterVersion(&cfg.ClusterVersion)
<<<<<<< HEAD
=======
o.ttl = make(map[string]*cache.TTLString, 6)
o.ttlCancel = make(map[string]context.CancelFunc, 6)
>>>>>>> 60775507... config: fix goroutine leak risk (#3091)
return o
}

Expand Down Expand Up @@ -532,3 +543,35 @@ func (o *PersistOptions) CheckLabelProperty(typ string, labels []*metapb.StoreLa
}
return false
}
<<<<<<< HEAD
=======

// SetTTLData set temporary configuration
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) {
if data, ok := o.ttl[key]; ok {
return data.Get(key)
}
return nil, false
}

// SetAllStoresLimitTTL sets all store limit for a given type and rate with ttl.
func (o *PersistOptions) SetAllStoresLimitTTL(ctx context.Context, typ storelimit.Type, ratePerMin float64, ttl time.Duration) {
switch typ {
case storelimit.AddPeer:
o.SetTTLData(ctx, "default-add-peer", ratePerMin, ttl)
case storelimit.RemovePeer:
o.SetTTLData(ctx, "default-remove-peer", ratePerMin, ttl)
}
}
>>>>>>> 60775507... config: fix goroutine leak risk (#3091)

0 comments on commit 0902b46

Please sign in to comment.