From 37ce1d0654ea7b69242097068ae4a75f40b3a2b2 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 28 Feb 2019 04:16:04 +0100 Subject: manager: wire up tunnels changed notifier --- conf/storewatcher.go | 34 ++++++++++------------------------ conf/storewatcher_windows.go | 4 ++-- 2 files changed, 12 insertions(+), 26 deletions(-) (limited to 'conf') diff --git a/conf/storewatcher.go b/conf/storewatcher.go index 4f9c2ef7..7a5ab387 100644 --- a/conf/storewatcher.go +++ b/conf/storewatcher.go @@ -5,34 +5,20 @@ package conf -import "reflect" - -type StoreCallback func() +type storeCallback struct { + cb func() +} -var storeCallbacks []StoreCallback +var storeCallbacks = make(map[*storeCallback]bool) -func RegisterStoreChangeCallback(cb StoreCallback) { +func RegisterStoreChangeCallback(cb func()) *storeCallback { startWatchingConfigDir() cb() - storeCallbacks = append(storeCallbacks, cb) + s := &storeCallback{cb} + storeCallbacks[s] = true + return s } -func UnregisterStoreChangeCallback(cb StoreCallback) { - //TODO: this function is ridiculous, doing slow iteration like this and reflection too. - - index := -1 - for i, e := range storeCallbacks { - if reflect.ValueOf(e).Pointer() == reflect.ValueOf(cb).Pointer() { - index = i - break - } - } - if index == -1 { - return - } - newList := storeCallbacks[0:index] - if index < len(storeCallbacks)-1 { - newList = append(newList, storeCallbacks[index+1:]...) - } - storeCallbacks = newList +func UnregisterStoreChangeCallback(cb *storeCallback) { + delete(storeCallbacks, cb) } diff --git a/conf/storewatcher_windows.go b/conf/storewatcher_windows.go index f3f38fef..ddfc8b92 100644 --- a/conf/storewatcher_windows.go +++ b/conf/storewatcher_windows.go @@ -46,8 +46,8 @@ func startWatchingConfigDir() { log.Fatalf("Unable to wait on config directory watcher: %v", err) } - for _, cb := range storeCallbacks { - cb() + for cb := range storeCallbacks { + cb.cb() } err = findNextChangeNotification(h) -- cgit v1.2.3-59-g8ed1b