notifier

package
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package notifier provides a grouped event notification mechanism that accumulates filesystem events and errors and triggers user-defined callbacks at controlled intervals.

The Notifier is designed to avoid flooding handlers when many filesystem events happen in quick succession. It supports:

  • Event batching with a configurable delay.
  • Rate limiting using golang.org/x/time/rate.
  • Custom callbacks for events and errors.
  • Safe concurrent access and automatic recovery from panics in handlers.

Example usage:

// Create a new notifier with a delay of 200ms and up to 5 events per delay
notifier, err := notifier.Create(paths,
	notifier.OnPaths("/tmp/watchdir"),
	notifier.WithEventHandler(func(path string, events []fsnotify.Event) {
		fmt.Println("Received events:", events)
	}),
	notifier.WithLimitDelay(200*time.Millisecond),
	notifier.WithBurstNumber(5),
)
if err != nil {
	log.Fatal(err)
}

// Start watching
err = notifier.Start()
if err != nil {
	log.Fatal(err)
}
defer notifier.Close()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event added in v1.4.5

type Event struct {
	Path   string
	Events []fsnotify.Event
}

type Notifier

type Notifier struct {
	// contains filtered or unexported fields
}

Notifier accumulates filesystem events and errors and triggers user-defined callbacks in a rate-limited and batched manner.

It is safe for concurrent use.

func Create

func Create(opts ...Option) (*Notifier, error)

Create creates a new Notifier for the specified filesystem locations.

The notifier will accumulate events and errors from the given paths and trigger configured callbacks according to the delay and event-per-delay settings.

func (*Notifier) AddPath

func (n *Notifier) AddPath(path string) error

AddPath adds a new path to the notifier. It must be called before Start. The path must exist on the filesystem, otherwise an error is returned.

func (*Notifier) Close

func (n *Notifier) Close() error

Close stops the watcher and releases all associated resources. It is safe to call multiple times.

func (*Notifier) IsStarted

func (n *Notifier) IsStarted() bool

func (*Notifier) Start

func (n *Notifier) Start() error

Start starts the underlying filesystem watcher.

type Option

type Option func(*Notifier) error

Option is a function type for configuring Notifier.

func ForOperations

func ForOperations(ops ...fsnotify.Op) Option

ForOperations returns an Option that configures a Notifier to only consider specific filesystem operations for triggering events.

The provided operations (ops) are combined into a set. Only events matching one of these operations will be processed by the Notifier.

Example:

notifier, err := Create(
    "/tmp/watchdir",
    ForOperations(fsnotify.Create, fsnotify.Write, fsnotify.Remove),
)
if err != nil {
    log.Fatal(err)
}

Supported operations are defined in fsnotify.Op:

fsnotify.Create, fsnotify.Write, fsnotify.Remove, fsnotify.Rename, fsnotify.Chmod

This Option can be passed to a Notifier during creation to filter events according to your requirements.

func OnPath

func OnPath(path string) Option

OnPath configures the notifier to observe a single path.

func OnPaths

func OnPaths(paths ...string) Option

OnPaths configures the notifier to observe multiple paths at once.

func WatchSubfolders

func WatchSubfolders(enabled bool) Option

WatchSubfolders enables or disables recursive monitoring of subfolders.

By default, fsnotify does not watch subfolders of a directory automatically. When enabled, this option ensures that all nested directories are included in the watch scope, so events such as file creation, modification, renaming, or deletion inside subdirectories will also be detected. Parameters:

  • enabled: set to true to include subfolders in the watch, false to restrict watching only to the top-level directory.

Returns:

  • Option: a configuration function applied when creating the watcher.

func WithBurstNumber

func WithBurstNumber(burst uint) Option

WithBurstNumber sets the maximum number of events allowed per delay window.

func WithEventChainAsHandler added in v1.4.5

func WithEventChainAsHandler(ch chan<- Event) Option

WithEventChainAsHandler sets up an event handler that delivers batched fsnotify events into the provided channel instead of invoking a direct callback.

This option is useful when you prefer to decouple event handling logic from the watcher by processing events asynchronously through a channel. Each emitted Event groups together all fsnotify events for a given path during a batch.

func WithEventHandler

func WithEventHandler(handler func(string, []fsnotify.Event)) Option

WithEventHandler sets the event handler callback that receives batched fsnotify events.

func WithSimpleHandler

func WithSimpleHandler(handler func()) Option

WithSimpleHandler sets a simple callback invoked when events are accumulated, without event details.

func WithThrottleInterval

func WithThrottleInterval(interval time.Duration) Option

WithThrottleInterval sets the throttling interval used to configure the rate limiter. It represents the minimum time between allowed callback invocations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL