From ab10f959116c9dda2191b657fd87f61b72605c76 Mon Sep 17 00:00:00 2001 From: "Fabrizio (Misto) Milo" Date: Tue, 6 Aug 2013 18:59:46 -0700 Subject: [PATCH 1/5] set messager as send only --- etcd.go | 2 +- store/store.go | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/etcd.go b/etcd.go index a5fc96772..cc75a8bea 100644 --- a/etcd.go +++ b/etcd.go @@ -216,7 +216,7 @@ func main() { if argInfo.WebPort != -1 { // start web - etcdStore.SetMessager(&storeMsg) + etcdStore.SetMessager(storeMsg) go webHelper() go web.Start(raftServer, argInfo.WebPort) } diff --git a/store/store.go b/store/store.go index 5ef4bc2d4..da7274eb3 100644 --- a/store/store.go +++ b/store/store.go @@ -35,7 +35,7 @@ type Store struct { // The string channel to send messages to the outside world // Now we use it to send changes to the hub of the web service - messager *chan string + messager chan<- string // A map to keep the recent response to the clients ResponseMap map[string]*Response @@ -141,7 +141,7 @@ func CreateStore(max int) *Store { } // Set the messager of the store -func (s *Store) SetMessager(messager *chan string) { +func (s *Store) SetMessager(messager chan<- string) { s.messager = messager } @@ -224,8 +224,7 @@ func (s *Store) internalSet(key string, value string, expireTime time.Time, inde // Send to the messager if s.messager != nil && err == nil { - - *s.messager <- string(msg) + s.messager <- string(msg) } s.addToResponseMap(index, &resp) @@ -257,8 +256,7 @@ func (s *Store) internalSet(key string, value string, expireTime time.Time, inde // Send to the messager if s.messager != nil && err == nil { - - *s.messager <- string(msg) + s.messager <- string(msg) } s.addToResponseMap(index, &resp) @@ -440,8 +438,7 @@ func (s *Store) internalDelete(key string, index uint64) ([]byte, error) { // notify the messager if s.messager != nil && err == nil { - - *s.messager <- string(msg) + s.messager <- string(msg) } s.addToResponseMap(index, &resp) @@ -526,8 +523,7 @@ func (s *Store) monitorExpiration(key string, update chan time.Time, expireTime // notify the messager if s.messager != nil && err == nil { - - *s.messager <- string(msg) + s.messager <- string(msg) } return From f5c2d19b90002ec569510572da7b8dc01382c77a Mon Sep 17 00:00:00 2001 From: "Fabrizio (Misto) Milo" Date: Tue, 6 Aug 2013 19:00:09 -0700 Subject: [PATCH 2/5] nip --- store/watcher_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/watcher_test.go b/store/watcher_test.go index 08e64d1ab..b5730ed93 100644 --- a/store/watcher_test.go +++ b/store/watcher_test.go @@ -54,7 +54,7 @@ func TestWatch(t *testing.T) { } } -// BenchmarkWatch creates 10K watchers watch at /foo/[paht] each time. +// BenchmarkWatch creates 10K watchers watch at /foo/[path] each time. // Path is randomly chosen with max depth 10. // It should take less than 15ms to wake up 10K watchers. func BenchmarkWatch(b *testing.B) { From 140bbfec381befdfdf61ebfc306dcff0b8a84523 Mon Sep 17 00:00:00 2001 From: "Fabrizio (Misto) Milo" Date: Tue, 6 Aug 2013 19:14:56 -0700 Subject: [PATCH 3/5] nip --- store/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/store.go b/store/store.go index da7274eb3..93a8e3dda 100644 --- a/store/store.go +++ b/store/store.go @@ -205,7 +205,7 @@ func (s *Store) internalSet(key string, value string, expireTime time.Time, inde } else { // If we want the permanent node to have expire time - // We need to create create a go routine with a channel + // We need to create a go routine with a channel if isExpire { node.update = make(chan time.Time) go s.monitorExpiration(key, node.update, expireTime) From f4fc4caf6f757efbb9a55c68ebedde24ce692aff Mon Sep 17 00:00:00 2001 From: "Fabrizio (Misto) Milo" Date: Tue, 6 Aug 2013 20:00:50 -0700 Subject: [PATCH 4/5] remove pointer to map --- store/store.go | 2 +- store/watcher.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/store/store.go b/store/store.go index 93a8e3dda..66a535aaa 100644 --- a/store/store.go +++ b/store/store.go @@ -483,7 +483,7 @@ func (s *Store) TestAndSet(key string, prevValue string, value string, expireTim // The watchHub will send response to the channel when any key under the prefix // changes [since the sinceIndex if given] func (s *Store) AddWatcher(prefix string, watcher *Watcher, sinceIndex uint64) error { - return s.watcher.addWatcher(prefix, watcher, sinceIndex, s.ResponseStartIndex, s.Index, &s.ResponseMap) + return s.watcher.addWatcher(prefix, watcher, sinceIndex, s.ResponseStartIndex, s.Index, s.ResponseMap) } // This function should be created as a go routine to delete the key-value pair diff --git a/store/watcher.go b/store/watcher.go index dfd91774a..57de318ba 100644 --- a/store/watcher.go +++ b/store/watcher.go @@ -36,14 +36,14 @@ func NewWatcher() *Watcher { // Add a watcher to the watcherHub func (w *WatcherHub) addWatcher(prefix string, watcher *Watcher, sinceIndex uint64, - responseStartIndex uint64, currentIndex uint64, resMap *map[string]*Response) error { + responseStartIndex uint64, currentIndex uint64, resMap map[string]*Response) error { prefix = path.Clean("/" + prefix) if sinceIndex != 0 && sinceIndex >= responseStartIndex { for i := sinceIndex; i <= currentIndex; i++ { if checkResponse(prefix, i, resMap) { - watcher.C <- (*resMap)[strconv.FormatUint(i, 10)] + watcher.C <- resMap[strconv.FormatUint(i, 10)] return nil } } @@ -65,9 +65,9 @@ func (w *WatcherHub) addWatcher(prefix string, watcher *Watcher, sinceIndex uint } // Check if the response has what we are watching -func checkResponse(prefix string, index uint64, resMap *map[string]*Response) bool { +func checkResponse(prefix string, index uint64, resMap map[string]*Response) bool { - resp, ok := (*resMap)[strconv.FormatUint(index, 10)] + resp, ok := resMap[strconv.FormatUint(index, 10)] if !ok { // not storage system command From 384d79d67184cbab86bad714936c65578cfc4540 Mon Sep 17 00:00:00 2001 From: "Fabrizio (Misto) Milo" Date: Tue, 6 Aug 2013 20:03:32 -0700 Subject: [PATCH 5/5] remove duplicate logic --- store/watcher.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/store/watcher.go b/store/watcher.go index 57de318ba..17de27b21 100644 --- a/store/watcher.go +++ b/store/watcher.go @@ -52,15 +52,11 @@ func (w *WatcherHub) addWatcher(prefix string, watcher *Watcher, sinceIndex uint _, ok := w.watchers[prefix] if !ok { - w.watchers[prefix] = make([]*Watcher, 0) - - w.watchers[prefix] = append(w.watchers[prefix], watcher) - } else { - - w.watchers[prefix] = append(w.watchers[prefix], watcher) } + w.watchers[prefix] = append(w.watchers[prefix], watcher) + return nil }