Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 解决json配置文件在热更新时,因系统触发两次文件写入事件,第一次读取到的配置内容为空,导致json解析失败报错的问题 #3431

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

flytutu-susu
Copy link

@flytutu-susu flytutu-susu commented Sep 28, 2024

Description (what this PR does / why we need it):

image
此为对空[]byte进行json.Unmarshal时的错误。

触发Write时,可能导致读取的内容为空
image

通过添加延时一毫秒去抖,能每次都读取到文件内容
image
2024/09/29 10:27:42 event: WRITE 2024/09/29 10:27:42 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:42 event: WRITE 2024/09/29 10:27:42 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:42 event: WRITE 2024/09/29 10:27:42 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:42 event: WRITE 2024/09/29 10:27:42 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:42 event: WRITE 2024/09/29 10:27:42 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:42 event: WRITE 2024/09/29 10:27:42 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:43 event: WRITE 2024/09/29 10:27:43 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:44 event: WRITE 2024/09/29 10:27:44 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:44 event: WRITE 2024/09/29 10:27:44 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:44 event: WRITE 2024/09/29 10:27:44 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:44 event: WRITE 2024/09/29 10:27:44 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:44 event: WRITE 2024/09/29 10:27:44 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:44 event: WRITE 2024/09/29 10:27:44 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:45 event: WRITE 2024/09/29 10:27:45 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:45 event: WRITE 2024/09/29 10:27:45 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:45 event: WRITE 2024/09/29 10:27:45 modified file: tmp/a.txt [49 50 51] 2024/09/29 10:27:45 event: WRITE 2024/09/29 10:27:45 modified file: tmp/a.txt [49 50 51]
每次触发事件均能读取成功

Which issue(s) this PR fixes (resolves / be part of):

Other special notes for the reviewers:

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Sep 28, 2024
@@ -52,6 +53,7 @@ func (w *watcher) Next() ([]*config.KeyValue, error) {
if fi.IsDir() {
path = filepath.Join(w.f.path, filepath.Base(event.Name))
}
time.Sleep(time.Millisecond)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can sleep here solve the problem?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

未添加Sleep前,每次保存配置触发的两次写事件时,第一次读取到的内容均为空。添加Sleep后,进行100次连续保存操作,每次触发的两次写事件,均能读取到文件内容。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants