fix(contrib/registry/file/v2): fix the panic caused by channel closing after resolver closed (#3691)

This commit is contained in:
Luoyy 2024-08-14 20:44:05 +08:00 committed by GitHub
parent 9143b26182
commit dc8bb809a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View File

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

View File

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