replace map with list

This commit is contained in:
Allenxuxu 2019-12-05 19:29:48 +08:00
parent 26b5d1994a
commit b55b7d6b20

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,
} }