Go 每日一库之 fsnotify
<h2 id="简介">简介</h2><p>上一篇文章Go 每日一库之 viper中,我们介绍了 viper 可以监听文件修改进而自动重新加载。<br>
其内部使用的就是<code>fsnotify</code>这个库,它是跨平台的。今天我们就来介绍一下它。</p>
<h2 id="快速使用">快速使用</h2>
<p>先安装:</p>
<pre><code>$ go get github.com/fsnotify/fsnotify
</code></pre>
<p>后使用:</p>
<pre><code class="language-golang">package main
import (
"log"
"github.com/fsnotify/fsnotify"
)
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("NewWatcher failed: ", err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
defer close(done)
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Printf("%s %s\n", event.Name, event.Op)
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
err = watcher.Add("./")
if err != nil {
log.Fatal("Add failed:", err)
}
<-done
}
</code></pre>
<p><code>fsnotify</code>的使用比较简单:</p>
<ul>
<li>先调用<code>NewWatcher</code>创建一个监听器;</li>
<li>然后调用监听器的<code>Add</code>增加监听的文件或目录;</li>
<li>如果目录或文件有事件产生,监听器中的通道<code>Events</code>可以取出事件。如果出现错误,监听器中的通道<code>Errors</code>可以取出错误信息。</li>
</ul>
<p>上面示例中,我们在另一个 goroutine 中循环读取发生的事件及错误,然后输出它们。</p>
<p>编译、运行程序。在当前目录创建一个<code>新建文本文档.txt</code>,然后重命名为<code>file1.txt</code>文件,输入内容<code>some test text</code>,然后删除它。观察控制台输出:</p>
<pre><code>2020/01/20 08:41:17 新建文本文档.txt CREATE
2020/01/20 08:41:25 新建文本文档.txt RENAME
2020/01/20 08:41:25 file1.txt CREATE
2020/01/20 08:42:28 file1.txt REMOVE
</code></pre>
<p><strong>其实,重命名时会产生两个事件,一个是原文件的<code>RENAME</code>事件,一个是新文件的<code>CREATE</code>事件。</strong></p>
<p>注意,<code>fsnotify</code>使用了操作系统接口,监听器中保存了系统资源的句柄,所以使用后需要关闭。</p>
<h2 id="事件">事件</h2>
<p>上面示例中的事件是<code>fsnotify.Event</code>类型:</p>
<pre><code class="language-golang">// fsnotify/fsnotify.go
type Event struct {
Name string
Op Op
}
</code></pre>
<p>事件只有两个字段,<code>Name</code>表示发生变化的文件或目录名,<code>Op</code>表示具体的变化。<code>Op</code>有 5 中取值:</p>
<pre><code class="language-golang">// fsnotify/fsnotify.go
type Op uint32
const (
Create Op = 1 << iota
Write
Remove
Rename
Chmod
)
</code></pre>
<p>在快速使用中,我们已经演示了前 4 种事件。<code>Chmod</code>事件在文件或目录的属性发生变化时触发,在 Linux 系统中可以通过<code>chmod</code>命令改变文件或目录属性。</p>
<p>事件中的<code>Op</code>是按照位来存储的,可以存储多个,可以通过<code>&</code>操作判断对应事件是不是发生了。</p>
<pre><code class="language-golang">if event.Op & fsnotify.Write != 0 {
fmt.Println("Op has Write")
}
</code></pre>
<p>我们在代码中不需要这样判断,因为<code>Op</code>的<code>String()</code>方法已经帮我们处理了这种情况了:</p>
<pre><code class="language-golang">// fsnotify.go
func (op Op) String() string {
// Use a buffer for efficient string concatenation
var buffer bytes.Buffer
if op&Create == Create {
buffer.WriteString("|CREATE")
}
if op&Remove == Remove {
buffer.WriteString("|REMOVE")
}
if op&Write == Write {
buffer.WriteString("|WRITE")
}
if op&Rename == Rename {
buffer.WriteString("|RENAME")
}
if op&Chmod == Chmod {
buffer.WriteString("|CHMOD")
}
if buffer.Len() == 0 {
return ""
}
return buffer.String() // Strip leading pipe
}
</code></pre>
<h2 id="应用">应用</h2>
<p><code>fsnotify</code>的应用非常广泛,在 godoc 上,我们可以看到哪些库导入了<code>fsnotify</code>。只需要在<code>fsnotify</code>文档的 URL 后加上<code>?imports</code>即可:</p>
<p>https://godoc.org/github.com/fsnotify/fsnotify?importers。有兴趣打开看看,要 fq。</p>
<p>上一篇文章中,我们介绍了调用<code>viper.WatchConfig</code>就可以监听配置修改,自动重新加载。下面我们就来看看<code>WatchConfig</code>是怎么实现的:</p>
<pre><code class="language-golang">// viper/viper.go
func WatchConfig() { v.WatchConfig() }
func (v *Viper) WatchConfig() {
initWG := sync.WaitGroup{}
initWG.Add(1)
go func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
filename, err := v.getConfigFile()
if err != nil {
log.Printf("error: %v\n", err)
initWG.Done()
return
}
configFile := filepath.Clean(filename)
configDir, _ := filepath.Split(configFile)
realConfigFile, _ := filepath.EvalSymlinks(filename)
eventsWG := sync.WaitGroup{}
eventsWG.Add(1)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok { // 'Events' channel is closed
eventsWG.Done()
return
}
currentConfigFile, _ := filepath.EvalSymlinks(filename)
// we only care about the config file with the following cases:
// 1 - if the config file was modified or created
// 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement)
const writeOrCreateMask = fsnotify.Write | fsnotify.Create
if (filepath.Clean(event.Name) == configFile &&
event.Op&writeOrCreateMask != 0) ||
(currentConfigFile != "" && currentConfigFile != realConfigFile) {
realConfigFile = currentConfigFile
err := v.ReadInConfig()
if err != nil {
log.Printf("error reading config file: %v\n", err)
}
if v.onConfigChange != nil {
v.onConfigChange(event)
}
} else if filepath.Clean(event.Name) == configFile &&
event.Op&fsnotify.Remove&fsnotify.Remove != 0 {
eventsWG.Done()
return
}
case err, ok := <-watcher.Errors:
if ok { // 'Errors' channel is not closed
log.Printf("watcher error: %v\n", err)
}
eventsWG.Done()
return
}
}
}()
watcher.Add(configDir)
initWG.Done() // done initializing the watch in this go routine, so the parent routine can move on...
eventsWG.Wait() // now, wait for event loop to end in this go-routine...
}()
initWG.Wait() // make sure that the go routine above fully ended before returning
}
</code></pre>
<p>其实流程是相似的:</p>
<ul>
<li>首先,调用<code>NewWatcher</code>创建一个监听器;</li>
<li>调用<code>v.getConfigFile()</code>获取配置文件路径,抽出文件名、目录,配置文件如果是一个符号链接,获得链接指向的路径;</li>
<li>调用<code>watcher.Add(configDir)</code>监听配置文件所在目录,另起一个 goroutine 处理事件。</li>
</ul>
<p><code>WatchConfig</code>不能阻塞主 goroutine,所以创建监听器也是新起 goroutine 进行的。代码中有两个<code>sync.WaitGroup</code>变量,<code>initWG</code>是为了保证监听器初始化,<br>
<code>eventsWG</code>是在事件通道关闭,或配置被删除了,或遇到错误时退出事件处理循环。</p>
<p>然后就是核心事件循环:</p>
<ul>
<li>有事件发生时,判断变化的文件是否是在 viper 中设置的配置文件,发生的是否是创建或修改事件(只处理这两个事件);</li>
<li>如果配置文件为符号链接,若符合链接的指向修改了,也需要重新加载配置;</li>
<li>如果需要重新加载配置,调用<code>v.ReadInConfig()</code>读取新的配置;</li>
<li>如果注册了事件回调,以发生的事件为参数执行回调。</li>
</ul>
<h2 id="总结">总结</h2>
<p><code>fsnotify</code>的接口非常简单直接,所有系统相关的复杂性都被封装起来了。这也是我们平时设计模块和接口时可以参考的案例。</p>
<h2 id="参考">参考</h2>
<ol>
<li>fsnotify API 设计</li>
<li>fsnotify GitHub 仓库</li>
</ol>
<h2 id="我">我</h2>
<p>我的博客</p>
<p>欢迎关注我的微信公众号【GoUpUp】,共同学习,一起进步~</p>
<p><img src="https://img2018.cnblogs.com/blog/1919725/202001/1919725-20200121064250325-1446418159.jpg"></p>
<blockquote>
<p>本文由博客一文多发平台 OpenWrite 发布!</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/darjun/p/12220546.html
頁:
[1]