etcd/store/watcher.go

123 lines
2.4 KiB
Go
Raw Normal View History

2013-09-29 03:26:19 +04:00
package store
2013-09-03 22:30:42 +04:00
import (
"container/list"
"path"
2013-09-03 22:30:42 +04:00
"strings"
2013-09-29 03:26:19 +04:00
"sync/atomic"
2013-09-03 22:30:42 +04:00
)
type watcherHub struct {
watchers map[string]*list.List
2013-09-29 03:26:19 +04:00
count int64 // current number of watchers.
2013-09-03 22:30:42 +04:00
EventHistory *EventHistory
}
2013-09-07 06:05:11 +04:00
type watcher struct {
2013-09-16 17:16:22 +04:00
eventChan chan *Event
recursive bool
sinceIndex uint64
2013-09-07 06:05:11 +04:00
}
2013-09-29 03:26:19 +04:00
func newWatchHub(capacity int) *watcherHub {
2013-09-03 22:30:42 +04:00
return &watcherHub{
watchers: make(map[string]*list.List),
EventHistory: newEventHistory(capacity),
}
}
2013-09-07 06:05:11 +04:00
// watch function returns an Event channel.
// If recursive is true, the first change after index under prefix will be sent to the event channel.
// If recursive is false, the first change after index at prefix will be sent to the event channel.
// If index is zero, watch will start from the current index + 1.
2013-09-07 09:05:11 +04:00
func (wh *watcherHub) watch(prefix string, recursive bool, index uint64) (<-chan *Event, error) {
2013-09-03 22:30:42 +04:00
eventChan := make(chan *Event, 1)
e, err := wh.EventHistory.scan(prefix, index)
if err != nil {
2013-09-07 09:05:11 +04:00
return nil, err
2013-09-03 22:30:42 +04:00
}
if e != nil {
eventChan <- e
2013-09-07 09:05:11 +04:00
return eventChan, nil
2013-09-03 22:30:42 +04:00
}
2013-09-07 06:05:11 +04:00
w := &watcher{
2013-09-16 17:16:22 +04:00
eventChan: eventChan,
recursive: recursive,
sinceIndex: index,
2013-09-07 06:05:11 +04:00
}
2013-09-03 22:30:42 +04:00
l, ok := wh.watchers[prefix]
2013-09-07 06:05:11 +04:00
if ok { // add the new watcher to the back of the list
l.PushBack(w)
} else { // create a new list and add the new watcher
2013-09-03 22:30:42 +04:00
l := list.New()
2013-09-07 06:05:11 +04:00
l.PushBack(w)
2013-09-03 22:30:42 +04:00
wh.watchers[prefix] = l
}
2013-09-29 03:26:19 +04:00
atomic.AddInt64(&wh.count, 1)
2013-09-07 09:05:11 +04:00
return eventChan, nil
2013-09-03 22:30:42 +04:00
}
2013-09-07 09:05:11 +04:00
func (wh *watcherHub) notifyWithPath(e *Event, path string, force bool) {
l, ok := wh.watchers[path]
2013-09-03 22:30:42 +04:00
2013-09-07 09:05:11 +04:00
if ok {
curr := l.Front()
notifiedAll := true
2013-09-03 22:30:42 +04:00
2013-09-07 09:05:11 +04:00
for {
2013-09-03 22:30:42 +04:00
2013-09-07 09:05:11 +04:00
if curr == nil { // we have reached the end of the list
2013-09-03 22:30:42 +04:00
2013-09-07 09:05:11 +04:00
if notifiedAll {
// if we have notified all watcher in the list
// we can delete the list
delete(wh.watchers, path)
}
break
}
2013-09-07 06:05:11 +04:00
2013-09-07 09:05:11 +04:00
next := curr.Next() // save the next
2013-09-07 06:05:11 +04:00
2013-09-07 09:05:11 +04:00
w, _ := curr.Value.(*watcher)
2013-09-07 06:05:11 +04:00
2013-09-16 17:16:22 +04:00
if (w.recursive || force || e.Key == path) && e.Index >= w.sinceIndex {
2013-09-07 09:05:11 +04:00
w.eventChan <- e
l.Remove(curr)
2013-09-29 03:26:19 +04:00
atomic.AddInt64(&wh.count, -1)
2013-09-07 09:05:11 +04:00
} else {
notifiedAll = false
}
2013-09-07 06:05:11 +04:00
2013-09-07 09:05:11 +04:00
curr = next // go to the next one
2013-09-07 06:05:11 +04:00
2013-09-07 09:05:11 +04:00
}
}
}
2013-09-07 06:05:11 +04:00
2013-09-07 09:05:11 +04:00
func (wh *watcherHub) notify(e *Event) {
segments := strings.Split(e.Key, "/")
2013-09-07 06:05:11 +04:00
2013-09-07 09:05:11 +04:00
currPath := "/"
2013-09-03 22:30:42 +04:00
2013-09-07 09:05:11 +04:00
// walk through all the paths
for _, segment := range segments {
currPath = path.Join(currPath, segment)
wh.notifyWithPath(e, currPath, false)
2013-09-03 22:30:42 +04:00
}
2013-09-14 01:10:40 +04:00
wh.EventHistory.addEvent(e)
2013-09-03 22:30:42 +04:00
}
2013-09-29 03:58:57 +04:00
func (wh *watcherHub) clone() *watcherHub {
return &watcherHub{}
}