Skip to content

Commit

Permalink
update filesystem provider
Browse files Browse the repository at this point in the history
  • Loading branch information
hitzhangjie committed Jul 20, 2021
1 parent bb03f70 commit c97451a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
12 changes: 10 additions & 2 deletions config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ func (p *FilesystemProvider) Watch(ctx context.Context, fp string) (<-chan Event

ch := make(chan Event)
go func() {
defer watcher.Close()
defer func() {
watcher.Close()
close(ch)
}()

for {
select {
case ev, ok := <-watcher.Events:
if !ok {
fmt.Println("watch error: wather.Events closed")
return
}
if ev.Op&fsnotify.Write == 0 {
Expand All @@ -54,7 +59,10 @@ func (p *FilesystemProvider) Watch(ctx context.Context, fp string) (<-chan Event
return
}
fmt.Println("watch error:", err)
close(ch)
return
case <-ctx.Done():
fmt.Println("watch cancelled")
return
}
}
}()
Expand Down
18 changes: 16 additions & 2 deletions config/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
)

var fsProvider = &FilesystemProvider{}
Expand Down Expand Up @@ -32,16 +33,29 @@ func TestFilesystemProvider_Watch(t *testing.T) {

fp := filepath.Join(d, "testdata/change.ini")

ch, err := fsProvider.Watch(context.TODO(), fp)
ctx, cancel := context.WithCancel(context.TODO())
ch, err := fsProvider.Watch(ctx, fp)
if err != nil {
t.Fatalf("watch error: %v", err)
}

go func() {
defer cancel()

os.WriteFile(fp, []byte("helloworld0"), 0666)
time.Sleep(time.Second)
os.WriteFile(fp, []byte("helloworld1"), 0666)
time.Sleep(time.Second)
os.WriteFile(fp, []byte("helloworld2"), 0666)
time.Sleep(time.Second)
}()

LOOP:
for {
select {
case ev, ok := <-ch:
if !ok {
break
break LOOP
}
t.Logf("load ok: %s", ev.meta)
default:
Expand Down
22 changes: 1 addition & 21 deletions config/testdata/change.ini
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
# possible values : production, development
app_mode = development

[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
data = /home/git/grafana

[server]
# Protocol (http or https)
protocol = http

# The http port to use
http_port = 9999

# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
enforce_domain = true

hello = 1
hellox = 2
helloz = 4
helloworld2

0 comments on commit c97451a

Please sign in to comment.