mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-19 07:43:54 +01:00
v0.5-rc2: added reload cooldown, fixed auto reload, updated API
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"path"
|
||||
|
||||
"github.com/yusing/go-proxy/common"
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
)
|
||||
|
||||
@@ -22,4 +23,4 @@ func (f *fileWatcher) Events(ctx context.Context) (<-chan Event, <-chan E.Nested
|
||||
return fwHelper.Add(ctx, f)
|
||||
}
|
||||
|
||||
var fwHelper = newFileWatcherHelper()
|
||||
var fwHelper = newFileWatcherHelper(common.ConfigBasePath)
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/yusing/go-proxy/common"
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
)
|
||||
|
||||
@@ -26,14 +25,12 @@ type fileWatcherStream struct {
|
||||
errCh chan E.NestedError
|
||||
}
|
||||
|
||||
func newFileWatcherHelper() *fileWatcherHelper {
|
||||
func newFileWatcherHelper(dirPath string) *fileWatcherHelper {
|
||||
w, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
logrus.Panicf("unable to create fs watcher: %s", err)
|
||||
}
|
||||
// watch config path for all changes
|
||||
err = w.Add(common.ConfigBasePath)
|
||||
if err != nil {
|
||||
if err = w.Add(dirPath); err != nil {
|
||||
logrus.Panicf("unable to create fs watcher: %s", err)
|
||||
}
|
||||
helper := &fileWatcherHelper{
|
||||
@@ -60,26 +57,24 @@ func (h *fileWatcherHelper) Add(ctx context.Context, w *fileWatcher) (<-chan Eve
|
||||
errCh: make(chan E.NestedError),
|
||||
}
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
h.Remove(w)
|
||||
return
|
||||
case <-s.stopped:
|
||||
return
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
s.stopped <- struct{}{}
|
||||
case <-s.stopped:
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
close(s.eventCh)
|
||||
close(s.errCh)
|
||||
delete(h.m, w.filename)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
h.m[w.filename] = s
|
||||
return s.eventCh, s.errCh
|
||||
}
|
||||
|
||||
func (h *fileWatcherHelper) Remove(w *fileWatcher) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
h.m[w.filename].stopped <- struct{}{}
|
||||
delete(h.m, w.filename)
|
||||
}
|
||||
|
||||
func (h *fileWatcherHelper) start() {
|
||||
defer h.wg.Done()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user