From 1f67ff2ad4d91d1adc80985a6b413dde54e2640c Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 22 Oct 2020 18:09:52 +0800 Subject: [PATCH] cherry pick #3091 to release-4.0 (#3094) Signed-off-by: ti-srebot Co-authored-by: Song Gao --- server/config/persist_options.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/config/persist_options.go b/server/config/persist_options.go index 9998a7499ce..8f6e2210ecf 100644 --- a/server/config/persist_options.go +++ b/server/config/persist_options.go @@ -37,6 +37,7 @@ import ( type PersistOptions struct { // configuration -> ttl value ttl map[string]*cache.TTLString + ttlCancel map[string]context.CancelFunc schedule atomic.Value replication atomic.Value pdServerConfig atomic.Value @@ -55,6 +56,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 } @@ -613,12 +615,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) {