@@ -12,7 +12,6 @@ import (
12
12
"io"
13
13
"os"
14
14
"path/filepath"
15
- "time"
16
15
17
16
"github.com/fsnotify/fsnotify"
18
17
@@ -60,12 +59,6 @@ func File(ctx context.Context, path string, onChange func()) error {
60
59
fw .hash = hash
61
60
62
61
go func () {
63
- const (
64
- initialBackoff = 100 * time .Millisecond
65
- maxBackoff = 15 * time .Second
66
- )
67
- var currentBackoff time.Duration
68
-
69
62
defer func () {
70
63
if err != nil {
71
64
log .WithError (err ).Error ("Stopping file watch" )
@@ -90,40 +83,23 @@ func File(ctx context.Context, path string, onChange func()) error {
90
83
continue
91
84
}
92
85
93
- currentHash , hashErr := hashConfig (path )
94
- if hashErr != nil {
95
- log .WithError (hashErr ).WithField ("event" , event .Name ).Warn ("Cannot check if config has changed, backing off" )
96
-
97
- if currentBackoff == 0 {
98
- currentBackoff = initialBackoff
99
- } else {
100
- currentBackoff *= 2
101
- if currentBackoff > maxBackoff {
102
- currentBackoff = maxBackoff
103
- }
104
- }
105
-
106
- select {
107
- case <- time .After (currentBackoff ):
108
- case <- ctx .Done ():
109
- log .Info ("Context cancelled during backoff sleep, stopping file watcher" )
110
- return
111
- }
112
- continue
86
+ currentHash , err := hashConfig (path )
87
+ if err != nil {
88
+ log .WithError (err ).WithField ("event" , event .Name ).Warn ("Cannot check if config has changed" )
89
+ return
113
90
}
114
91
115
- currentBackoff = 0
116
-
92
+ // no change
117
93
if currentHash == fw .hash {
118
- log .WithField ("path" , path ).Debug ("Config file changed but content hash is the same" )
119
94
continue
120
95
}
121
96
122
97
log .WithField ("path" , path ).Info ("reloading file after change" )
98
+
123
99
fw .hash = currentHash
124
100
fw .onChange ()
125
- case watchErr := <- watcher .Errors :
126
- log .WithError (watchErr ).Error ("Unexpected error watching event" )
101
+ case err := <- watcher .Errors :
102
+ log .WithError (err ).Error ("Unexpected error watching event" )
127
103
case <- ctx .Done ():
128
104
return
129
105
}
0 commit comments