1
0
mirror of https://github.com/go-kratos/kratos.git synced 2026-05-22 10:15:24 +02:00

fix:config groutine(watch) leak (#1327)

* fix:config goroutine(watch) Leak
This commit is contained in:
月墨夕
2021-08-13 14:09:53 +08:00
committed by GitHub
parent 55cf83062f
commit fdce5f0746
3 changed files with 32 additions and 4 deletions
+16 -3
View File
@@ -1,24 +1,37 @@
package env
import (
"context"
"github.com/go-kratos/kratos/v2/config"
)
type watcher struct {
exit chan struct{}
ctx context.Context
cancel context.CancelFunc
}
var _ config.Watcher = (*watcher)(nil)
func NewWatcher() (config.Watcher, error) {
return &watcher{exit: make(chan struct{})}, nil
ctx, cancel := context.WithCancel(context.Background())
return &watcher{exit: make(chan struct{}), ctx: ctx, cancel: cancel}, nil
}
// Next will be blocked until the Stop method is called
func (w *watcher) Next() ([]*config.KeyValue, error) {
<-w.exit
return nil, nil
select {
case <-w.ctx.Done():
return nil, w.ctx.Err()
case <-w.exit:
return nil, nil
}
}
func (w *watcher) Stop() error {
close(w.exit)
w.cancel()
return nil
}