diff --git a/contrib/registry/file/file_discovery.go b/contrib/registry/file/file_discovery.go index 36065cc11..5fd21a741 100644 --- a/contrib/registry/file/file_discovery.go +++ b/contrib/registry/file/file_discovery.go @@ -10,6 +10,7 @@ import ( "context" "github.com/gogf/gf/v2/container/gmap" + "github.com/gogf/gf/v2/container/gtype" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gsvc" "github.com/gogf/gf/v2/os/gfile" @@ -55,8 +56,12 @@ func (r *Registry) Watch(ctx context.Context, key string) (watcher gsvc.Watcher, prefix: key, discovery: r, ch: make(chan gsvc.Service, 100), + closed: gtype.NewBool(false), } _, err = gfsnotify.Add(r.path, func(event *gfsnotify.Event) { + if fileWatcher.closed.Val() { + return + } if event.IsChmod() { return } diff --git a/contrib/registry/file/file_watcher.go b/contrib/registry/file/file_watcher.go index fa060ef0a..b10e74304 100644 --- a/contrib/registry/file/file_watcher.go +++ b/contrib/registry/file/file_watcher.go @@ -9,6 +9,8 @@ package file import ( "context" + "github.com/gogf/gf/v2/container/gtype" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/net/gsvc" ) @@ -17,11 +19,15 @@ type Watcher struct { prefix string // Watched prefix key, not file name prefix. discovery gsvc.Discovery // Service discovery. ch chan gsvc.Service // Changes that caused by inotify. + closed *gtype.Bool // Whether the channel has been closed } // Proceed proceeds watch in blocking way. // It returns all complete services that watched by `key` if any change. func (w *Watcher) Proceed() (services []gsvc.Service, err error) { + if w.closed.Val() { + return nil, gerror.New("discovery service was closed") + } <-w.ch return w.discovery.Search(context.Background(), gsvc.SearchInput{ Prefix: w.prefix, @@ -30,6 +36,8 @@ func (w *Watcher) Proceed() (services []gsvc.Service, err error) { // Close closes the watcher. func (w *Watcher) Close() error { - close(w.ch) + if w.closed.Cas(false, true) { + close(w.ch) + } return nil }