1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

Merge pull request #1020 from Allenxuxu/master

Avoid excessive memory usage
This commit is contained in:
Asim Aslam 2019-12-05 11:56:44 +00:00 committed by GitHub
commit 18cf025056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package memory
import ( import (
"bytes" "bytes"
"container/list"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@ -28,8 +29,7 @@ type memory struct {
// all the sources // all the sources
sources []source.Source sources []source.Source
idx int watchers *list.List
watchers map[int]*watcher
} }
type watcher struct { type watcher struct {
@ -153,11 +153,11 @@ func (m *memory) reload() error {
} }
func (m *memory) update() { func (m *memory) update() {
watchers := make([]*watcher, 0, len(m.watchers)) watchers := make([]*watcher, 0, m.watchers.Len())
m.RLock() m.RLock()
for _, w := range m.watchers { for e := m.watchers.Front(); e != nil; e = e.Next() {
watchers = append(watchers, w) watchers = append(watchers, e.Value.(*watcher))
} }
m.RUnlock() m.RUnlock()
@ -336,16 +336,14 @@ func (m *memory) Watch(path ...string) (loader.Watcher, error) {
updates: make(chan reader.Value, 1), updates: make(chan reader.Value, 1),
} }
id := m.idx e := m.watchers.PushBack(w)
m.watchers[id] = w
m.idx++
m.Unlock() m.Unlock()
go func() { go func() {
<-w.exit <-w.exit
m.Lock() m.Lock()
delete(m.watchers, id) m.watchers.Remove(e)
m.Unlock() m.Unlock()
}() }()
@ -404,7 +402,7 @@ func NewLoader(opts ...loader.Option) loader.Loader {
m := &memory{ m := &memory{
exit: make(chan bool), exit: make(chan bool),
opts: options, opts: options,
watchers: make(map[int]*watcher), watchers: list.New(),
sources: options.Source, sources: options.Source,
} }