Go 每日一庫之 fsnotify

簡介

上一篇文章Go 每日一庫之 viper中,咱們介紹了 viper 能夠監聽文件修改進而自動從新加載。
其內部使用的就是fsnotify這個庫,它是跨平臺的。今天咱們就來介紹一下它。git

快速使用

先安裝:github

$ go get github.com/fsnotify/fsnotify

後使用: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
}

fsnotify的使用比較簡單:segmentfault

  • 先調用NewWatcher建立一個監聽器;
  • 而後調用監聽器的Add增長監聽的文件或目錄;
  • 若是目錄或文件有事件產生,監聽器中的通道Events能夠取出事件。若是出現錯誤,監聽器中的通道Errors能夠取出錯誤信息。

上面示例中,咱們在另外一個 goroutine 中循環讀取發生的事件及錯誤,而後輸出它們。微信

編譯、運行程序。在當前目錄建立一個新建文本文檔.txt,而後重命名爲file1.txt文件,輸入內容some test text,而後刪除它。觀察控制檯輸出:oop

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使用了操做系統接口,監聽器中保存了系統資源的句柄,因此使用後須要關閉。ui

事件

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

// fsnotify/fsnotify.go
type Event struct {
  Name string
  Op   Op
}

事件只有兩個字段,Name表示發生變化的文件或目錄名,Op表示具體的變化。Op有 5 中取值:google

// 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")
}

咱們在代碼中不須要這樣判斷,由於OpString()方法已經幫咱們處理了這種狀況了:

// 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)監聽配置文件所在目錄,另起一個 goroutine 處理事件。

WatchConfig不能阻塞主 goroutine,因此建立監聽器也是新起 goroutine 進行的。代碼中有兩個sync.WaitGroup變量,initWG是爲了保證監聽器初始化,
eventsWG是在事件通道關閉,或配置被刪除了,或遇到錯誤時退出事件處理循環。

而後就是核心事件循環:

  • 有事件發生時,判斷變化的文件是不是在 viper 中設置的配置文件,發生的是不是建立或修改事件(只處理這兩個事件);
  • 若是配置文件爲符號連接,若符合連接的指向修改了,也須要從新加載配置;
  • 若是須要從新加載配置,調用v.ReadInConfig()讀取新的配置;
  • 若是註冊了事件回調,以發生的事件爲參數執行回調。

總結

fsnotify的接口很是簡單直接,全部系統相關的複雜性都被封裝起來了。這也是咱們平時設計模塊和接口時能夠參考的案例。

參考

  1. fsnotify API 設計
  2. fsnotify GitHub 倉庫

個人博客

歡迎關注個人微信公衆號【GoUpUp】,共同窗習,一塊兒進步~

本文由博客一文多發平臺 OpenWrite 發佈!
相關文章
相關標籤/搜索