内容简介:上一篇文章其内部使用的就是先安装:
简介
上一篇文章 Go 每日一库之 viper 中,我们介绍了 viper 可以监听文件修改进而自动重新加载。
其内部使用的就是 fsnotify 这个库,它是跨平台的。今天我们就来介绍一下它。
快速使用
先安装:
$ go get github.com/fsnotify/fsnotify
后使用:
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
}
fsnotify 的使用比较简单:
- 先调用
NewWatcher创建一个监听器; - 然后调用监听器的
Add增加监听的文件或目录; - 如果目录或文件有事件产生,监听器中的通道
Events可以取出事件。如果出现错误,监听器中的通道Errors可以取出错误信息。
上面示例中,我们在另一个 goroutine 中循环读取发生的事件及错误,然后输出它们。
编译、运行程序。在当前目录创建一个 新建文本文档.txt ,然后重命名为 file1.txt 文件,输入内容 some test text ,然后删除它。观察控制台输出:
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
其实,重命名时会产生两个事件,一个是原文件的 RENAME 事件,一个是新文件的 CREATE 事件。
注意, fsnotify 使用了操作系统接口,监听器中保存了系统资源的句柄,所以使用后需要关闭。
事件
上面示例中的事件是 fsnotify.Event 类型:
// fsnotify/fsnotify.go
type Event struct {
Name string
Op Op
}
事件只有两个字段, Name 表示发生变化的文件或目录名, Op 表示具体的变化。 Op 有 5 中取值:
// fsnotify/fsnotify.go type Op uint32 const ( Create Op = 1 << iota Write Remove Rename Chmod )
在中,我们已经演示了前 4 种事件。 Chmod 事件在文件或目录的属性发生变化时触发,在 Linux 系统中可以通过 chmod 命令改变文件或目录属性。
事件中的 Op 是按照位来存储的,可以存储多个,可以通过 & 操作判断对应事件是不是发生了。
if event.Op & fsnotify.Write != 0 {
fmt.Println("Op has Write")
}
我们在代码中不需要这样判断,因为 Op 的 String() 方法已经帮我们处理了这种情况了:
// 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()[1:] // Strip leading pipe
}
应用
fsnotify 的应用非常广泛,在 godoc 上,我们可以看到哪些库导入了 fsnotify 。只需要在 fsnotify 文档的 URL 后加上 ?imports 即可:
https://godoc.org/github.com/fsnotify/fsnotify?importers 。有兴趣打开看看,要 fq。
上一篇文章中,我们介绍了调用 viper.WatchConfig 就可以监听配置修改,自动重新加载。下面我们就来看看 WatchConfig 是怎么实现的:
// 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
}
其实流程是相似的:
NewWatcher v.getConfigFile() watcher.Add(configDir)
WatchConfig 不能阻塞主 goroutine,所以创建监听器也是新起 goroutine 进行的。代码中有两个 sync.WaitGroup 变量, initWG 是为了保证监听器初始化,
eventsWG 是在事件通道关闭,或配置被删除了,或遇到错误时退出事件处理循环。
然后就是核心事件循环:
v.ReadInConfig()
总结
fsnotify 的接口非常简单直接,所有系统相关的复杂性都被封装起来了。这也是我们平时设计模块和接口时可以参考的案例。
参考
我
欢迎关注我的微信公众号【GoUpUp】,共同学习,一起进步~
本文由博客一文多发平台 OpenWrite 发布!
以上所述就是小编给大家介绍的《Go 每日一库之 fsnotify》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
反应式设计模式
Roland Kuhn、Brian Hanafee、Jamie Allen / 何品、邱嘉和、王石冲、林炜翔审校 / 清华大学出版社 / 2019-1-1 / 98.00 元
《反应式设计模式》介绍反应式应用程序设计的原则、模式和经典实践,讲述如何用断路器模式将运行缓慢的组件与其他组件隔开、如何用事务序列(Saga)模式实现多阶段事务以及如何通过分片模式来划分数据集,分析如何保持源代码的可读性以及系统的可测试性(即使在存在许多潜在交互和失败点的情况下)。 主要内容 ? “反应式宣言”指南 ? 流量控制、有界一致性、容错等模式 ? 得之不易的关于“什么行不通”的经验 ? ......一起来看看 《反应式设计模式》 这本书的介绍吧!