Skip to content

Commit

Permalink
fix: memory leak when etcd watch failed (#2920)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauerwu authored Feb 19, 2024
1 parent 397c0cb commit 4133aa1
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion api/internal/core/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type GenericStore struct {

cancel context.CancelFunc
closing bool
inited bool //is inited
}

type GenericStoreOption struct {
Expand Down Expand Up @@ -109,12 +110,20 @@ func ReInit() error {
return err
}
}

//clear storeNeedReInit
storeNeedReInit = storeNeedReInit[:0]
return nil
}

func (s *GenericStore) Init() error {
s.initLock.Lock()
defer s.initLock.Unlock()
//return if inited
if s.IsInited() {
return nil
}

return s.listAndWatch()
}

Expand Down Expand Up @@ -351,6 +360,7 @@ func (s *GenericStore) listAndWatch() error {

// start watch
s.cancel = s.watch()
s.SetIsInited(true)

return nil
}
Expand All @@ -362,7 +372,11 @@ func (s *GenericStore) watch() context.CancelFunc {
defer func() {
if !s.closing {
log.Errorf("etcd watch exception closed, restarting: resource: %s", s.Type())
storeNeedReInit = append(storeNeedReInit, s)
//only add to storeNeedReInit when from inited to uninited
if s.IsInited() {
s.SetIsInited(false)
storeNeedReInit = append(storeNeedReInit, s)
}
}
}()
defer runtime.HandlePanic()
Expand Down Expand Up @@ -421,3 +435,11 @@ func (s *GenericStore) GetObjStorageKey(obj interface{}) string {
func (s *GenericStore) GetStorageKey(key string) string {
return fmt.Sprintf("%s/%s", s.opt.BasePath, key)
}

func (s *GenericStore) IsInited() bool {
return s.inited
}

func (s *GenericStore) SetIsInited(inited bool) {
s.inited = inited
}

0 comments on commit 4133aa1

Please sign in to comment.