watch

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package watch polls a single file and reports content changes via a callback.

It is an operator convenience that lets the gateway pick up an in-place edit to its configuration file without an explicit SIGHUP. The package is a leaf of the import graph: it imports only the standard library and knows nothing about configuration or the gateway. It detects that a file changed and invokes a caller-supplied callback, leaving the meaning of a reload to the caller. Change is judged by the file's modification time and size, which covers both in-place edits and the atomic write-temp-then-rename-over pattern (the replacement file carries a fresh modification time).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Watcher

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

Watcher polls a file and calls onChange whenever the file's modification time or size changes after Run begins.

func New

func New(path string, interval time.Duration, onChange func()) *Watcher

New returns a Watcher that polls path every interval and calls onChange after each detected change. interval must be positive; the caller (cmd) validates it before constructing the Watcher.

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context)

Run polls the file until ctx is cancelled, then returns. It captures a baseline at start so the first poll never fires a spurious change; thereafter it fires onChange once per detected change. A transient not-exist is ignored (the baseline is held until the file reappears); any other stat error is logged once per error episode and never fires onChange or stops the loop.

onChange runs synchronously on the polling goroutine, so it must not block: a slow callback stalls polling and delays observing ctx cancellation. Callers are expected to hand off (e.g. a non-blocking send) rather than do work inline.

Jump to

Keyboard shortcuts

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