MCPcopy Create free account
hub / github.com/foxcpp/maddy / reload

Method reload

internal/table/file.go:151–194  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

149}
150
151func (f *File) reload() {
152 info, err := os.Stat(f.file)
153 if err != nil {
154 if os.IsNotExist(err) {
155 f.mLck.Lock()
156 f.m = map[string][]string{}
157 f.mLck.Unlock()
158 return
159 }
160 f.log.Error("os stat", err)
161 }
162 if info.ModTime().Before(f.mStamp) || time.Since(info.ModTime()) < (reloadInterval/2) {
163 return // reload not necessary
164 }
165
166 f.log.Debugf("reloading")
167
168 newm := make(map[string][]string, len(f.m)+5)
169 if err := readFile(f.file, newm); err != nil {
170 if os.IsNotExist(err) {
171 f.log.Printf("ignoring non-existent file: %s", f.file)
172 return
173 }
174
175 f.log.Println(err)
176 return
177 }
178 // after reading we need to check whether file has changed in between
179 info2, err := os.Stat(f.file)
180 if err != nil {
181 f.log.Println(err)
182 return
183 }
184
185 if !info2.ModTime().Equal(info.ModTime()) {
186 // file has changed in the meantime
187 return
188 }
189
190 f.mLck.Lock()
191 f.m = newm
192 f.mStamp = info.ModTime()
193 f.mLck.Unlock()
194}
195
196func readFile(path string, out map[string][]string) error {
197 f, err := os.Open(path)

Callers 1

reloaderMethod · 0.95

Calls 5

readFileFunction · 0.85
DebugfMethod · 0.80
PrintlnMethod · 0.80
ErrorMethod · 0.45
PrintfMethod · 0.45

Tested by

no test coverage detected