返回

Fsnotify Introduction

Go跨平台文件系统通知之第三方库fsnotify介绍。

Viper 可以监听文件修改进而自动重新加载。 其内部使用的就是fsnotify这个库,它是跨平台的。fs是filesystem的缩写,翻译过来就是文件系统通知。能够监听文件的修改,进而发送通知。今天我们就来介绍一下fsnotify


快速使用

先安装:

1
$ go get -u github.com/fsnotify/fsnotify

后使用:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main

import (
	"log"

	"github.com/fsnotify/fsnotify"
)

func main() {
	// Create new watcher.
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer func(watcher *fsnotify.Watcher) {
		err := watcher.Close()
		if err != nil {
			log.Fatal(err)
		}
	}(watcher)

	done := make(chan bool)

	// Start listening for events.
	go func() {
		defer close(done)
		for {
			select {
			case event, ok := <-watcher.Events:
				if !ok {
					return
				}
				log.Printf("%s %s\n", event.Name, event.Op)
				/*if event.Has(fsnotify.Write) {
					log.Println("modified file:", event.Name)
				}*/
			case err, ok := <-watcher.Errors:
				if !ok {
					return
				}
				log.Println("error:", err)
			}
		}
	}()

	// Add a path.
	err = watcher.Add(".")
	if err != nil {
		log.Fatal(err)
	}

	<-done
}

fsnotify的使用比较简单:

  • 先调用NewWatcher创建一个监听器;
  • 然后调用监听器的Add增加监听的文件或目录;
  • 如果目录或文件有事件产生,监听器中的通道Events可以取出事件。如果出现错误,监听器中的通道Errors可以取出错误信息。

上面示例中,我们在另一个 goroutine 中循环读取发生的事件及错误,然后输出它们。

编译、运行程序。在当前目录创建一个新建文本文档.txt,然后重命名为abc.txt文件,输入内容some test text,然后删除它。观察控制台输出:

1
2
3
4
5
6
$ go run main.go
2024/05/08 16:29:52 新建文本文档.txt CREATE
2024/05/08 16:30:03 新建文本文档.txt RENAME
2024/05/08 16:30:03 abc.txt CREATE
2024/05/08 16:30:15 abc.txt WRITE
2024/05/08 16:30:26 abc.txt REMOVE

其实,重命名时会产生两个事件,一个是原文件的RENAME事件,一个是新文件的CREATE事件。

注意:

  1. fsnotify使用了操作系统接口,监听器中保存了系统资源的句柄,所以使用后需要关闭。
  2. 修改文件操作建议不要在IDE中操作,如GlLand。IDE的缓存和自动保存会响应输出结果。建议直接在系统文件管理器中操作,才会出现上面结果。

事件

上面示例中的事件是fsnotify.Event类型:

1
2
3
4
5
// fsnotify/fsnotify.go
type Event struct {
  Name string
  Op   Op
}

事件只有两个字段,Name表示发生变化的文件或目录名,Op表示具体的变化。Op有 5 种取值:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// fsnotify/fsnotify.go
type Op uint32

const (
  Create Op = 1 << iota
  Write
  Remove
  Rename
  Chmod
)

快速使用中,我们已经演示了前 4 种事件。Chmod事件在文件或目录的属性发生变化时触发,在 Linux 系统中可以通过chmod命令改变文件或目录属性。

事件中的Op是按照左移位运算来存储的,可以存储多个,可以通过&操作判断对应事件是不是发生了。

1
2
3
if event.Op & fsnotify.Write != 0 {
  fmt.Println("Op has Write")
}

补充:

​ 与运算&:同为1为1。

​ 左移运算«:向做做移动指定位,低位用0补齐

​ 或运算:有1就为1。用在组合Op——>|

​ 事件中的Op是通过左移运算之后,结果为1,2,4,6,8十进制来存储的,它们的二进制位都只有一个1,当进行与运算时,就就能判断是否包含指定Op

判断事件中是否存在某个Op,封装到了事件对象的Has方法下:

1
2
3
4
// fsnotify/fsnotify.go
func (e Event) Has(op Op) bool { return e.Op.Has(op) }

func (o Op) Has(h Op) bool { return o&h != 0 }  // event.Op & fsnotify.Write != 0同理

当我们直接输出Op时,会自动调用String()方法,这个方法帮我们将对应的Op(uint类型)转换成定义的具体操作,代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
func (o Op) String() string {
    var b strings.Builder
    if o.Has(Create) {
       b.WriteString("|CREATE")
    }
    if o.Has(Remove) {
       b.WriteString("|REMOVE")
    }
    if o.Has(Write) {
       b.WriteString("|WRITE")
    }
    if o.Has(Rename) {
       b.WriteString("|RENAME")
    }
    if o.Has(Chmod) {
       b.WriteString("|CHMOD")
    }
    if b.Len() == 0 {
       return "[no events]"
    }
    return b.String()[1:]
}

应用

fsnotify的应用非常广泛,在 godoc 上,我们可以看到哪些库导入了fsnotify。只需进入fsnotify godoc点击Imported by 9,171,就能查看。有兴趣的可以打开看看。

《Go配置管理之第三方库viper》文章中,我们介绍了调用viper.WatchConfig就可以监听配置修改,自动重新加载。下面我们就来看看WatchConfig是怎么实现的:

首先演示viper自动更新配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main
// mian.go
import (
    "fmt"
    "log"
    "time"

    "github.com/fsnotify/fsnotify"
    "github.com/spf13/viper"
)

func main() {
    viper.SetConfigFile("./cfg.yaml")

    err := viper.ReadInConfig()
    if err != nil {
       log.Fatal("ReadInConfig error:", err)
    }

    viper.OnConfigChange(func(in fsnotify.Event) {
       log.Printf("%s %s username:%s\n", in.Name, in.Op, viper.Get("username"))
    })

    fmt.Println("修改前的username:", viper.Get("username"))
    viper.WatchConfig()
    time.Sleep(10 * time.Second) // 修改文件
    fmt.Println("修改后的username:", viper.Get("username"))
}
1
2
# ./cfg.yaml
username: arlettebrook

运行之后,手动修改cfg.yamlusername(注意不要在IDE中修改,会影响输出效果),在后面分别追加1/2/3,并别保存,输出:

1
2
3
4
5
6
$ go run main.go 
修改前的username: arlettebrook
2024/05/08 18:01:32 cfg.yaml WRITE username:arlettebrook1
2024/05/08 18:01:36 cfg.yaml WRITE username:arlettebrook12
2024/05/08 18:01:39 cfg.yaml WRITE username:arlettebrook123
修改后的username: arlettebrook123

具体实现:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// viper/viper.go
// WatchConfig starts watching a config file for changes.
func WatchConfig() { v.WatchConfig() }

// WatchConfig starts watching a config file for changes.
func (v *Viper) WatchConfig() {
	initWG := sync.WaitGroup{}
	initWG.Add(1)
	go func() {
		watcher, err := fsnotify.NewWatcher()
		if err != nil {
			v.logger.Error(fmt.Sprintf("failed to create watcher: %s", err))
			os.Exit(1)
		}
		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 {
			v.logger.Error(fmt.Sprintf("get config file: %s", 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)
					if (filepath.Clean(event.Name) == configFile &&
						(event.Has(fsnotify.Write) || event.Has(fsnotify.Create))) ||
						(currentConfigFile != "" && currentConfigFile != realConfigFile) {
						realConfigFile = currentConfigFile
						err := v.ReadInConfig()
						if err != nil {
							v.logger.Error(fmt.Sprintf("read config file: %s", err))
						}
						if v.onConfigChange != nil {
							v.onConfigChange(event)
						}
					} else if filepath.Clean(event.Name) == configFile && event.Has(fsnotify.Remove) {
						eventsWG.Done()
						return
					}

				case err, ok := <-watcher.Errors:
					if ok { // 'Errors' channel is not closed
						v.logger.Error(fmt.Sprintf("watcher error: %s", 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)监听配置文件所在目录,另起一个 goroutine 处理事件。

WatchConfig不能阻塞主 goroutine,所以创建监听器也是新起 goroutine 进行的。代码中有两个sync.WaitGroup变量,initWG是为了保证监听器初始化, eventsWG是在事件通道关闭,或配置被删除了,或遇到错误时退出事件处理循环。

然后就是核心事件循环:

  • 有事件发生时,判断变化的文件是否是在 viper 中设置的配置文件,发生的是否是创建或修改事件(只处理这两个事件);
  • 如果配置文件为符号链接,若符合链接的指向修改了,也需要重新加载配置;
  • 如果需要重新加载配置,调用v.ReadInConfig()读取新的配置;
  • 如果注册了事件回调,以发生的事件为参数执行回调。

总结

fsnotify的接口非常简单直接,所有系统相关的复杂性都被封装起来了。这也是我们平时设计模块和接口时可以参考的案例。


参考

  1. fsnotify API 设计
  2. fsnotify GitHub 仓库
  3. 原文:Go 每日一库之 fsnotify