Skip to main content

Overview

The watcher monitors configuration and authentication files for changes and triggers automatic reloads without restarting the service. This enables:
  • Hot-reload of config.yaml when modified
  • Automatic detection of auth file changes in the auth directory
  • Incremental updates for individual auth files
  • Debounced reload to handle rapid successive changes
  • Cross-platform file system event handling

Watcher Interface

The watcher is implemented in the internal package but exposed through the service builder:
internal/watcher/watcher.go

Authentication Updates

Auth file changes trigger incremental updates through a structured event system:
internal/watcher/watcher.go

File Watching Behavior

Configuration File Watching

The watcher monitors config.yaml for changes:
internal/watcher/config_reload.go
Reload process:
  1. Detect file system event (Write, Create, or Rename)
  2. Wait 150ms debounce period (handles rapid successive writes)
  3. Read file and compute SHA256 hash
  4. Compare with previous hash
  5. If changed, reload configuration and trigger callback
  6. Persist changes if persistence is configured

Authentication File Watching

The watcher monitors all .json files in the auth directory:
internal/watcher/events.go
Incremental update process:
  1. Detect .json file event in auth directory
  2. Compute SHA256 hash of file contents
  3. Compare with cached hash
  4. If unchanged, skip processing
  5. If changed, parse auth file and generate update events
  6. Emit AuthUpdate events to registered consumers
  7. Persist changes if persistence is configured

Integration with Service Builder

The watcher is automatically configured when building a service:

Custom Watcher Factory

Override the default watcher with a custom implementation:

Configuration Reload Events

The watcher detects and logs configuration changes:
internal/watcher/config_reload.go

Example Log Output

Authentication File Events

The watcher emits detailed logs for auth changes:
internal/watcher/clients.go

Example Log Output

Debouncing and Deduplication

The watcher uses multiple strategies to handle noisy file systems:

Configuration Debouncing

internal/watcher/config_reload.go
Example timeline:

Auth File Debouncing

internal/watcher/events.go
Atomic replacement detection:

Hash-Based Deduplication

internal/watcher/events.go

Cross-Platform Path Handling

The watcher normalizes paths for consistent behavior across operating systems:
internal/watcher/events.go
Platform-specific behaviors:
  • Linux: Case-sensitive paths, direct inotify events
  • macOS: Case-insensitive by default, FSEvents-based
  • Windows: Case-insensitive, long path prefix handling

Runtime Authentication Updates

External systems can inject auth updates through the watcher:
internal/watcher/watcher.go

Example: WebSocket Auth Provider

Persistence Integration

The watcher supports optional persistence for synchronized storage:
internal/watcher/clients.go

Server Update Coordination

The watcher coordinates server updates with debouncing:
internal/watcher/clients.go

Configuration Example

Minimal configuration for file watching:
config.yaml

Monitoring Watcher Activity

Enable debug logging to observe watcher behavior:
config.yaml
Debug logs include:
  • File system events received
  • Hash comparison results
  • Field-level change detection
  • Debounce and deduplication decisions
  • Reload timing and performance

Example Debug Output

Best Practices

1. Use Absolute Paths

2. Graceful Shutdown

3. Monitor Watcher Errors

4. Test Configuration Changes

Troubleshooting

Watcher Not Detecting Changes

Problem: File changes not triggering reloads Solutions:
  1. Verify file paths are absolute
  2. Check file permissions (read access required)
  3. Ensure file system supports inotify/FSEvents
  4. Check for file system mount options (some NFS/network mounts don’t support events)
  5. Enable debug logging to see events

Rapid Successive Reloads

Problem: Too many reloads happening Cause: Editor or tool making multiple rapid writes Solution: Debouncing is automatic, but you can:
  1. Configure editor to write atomically
  2. Use single-write operations
  3. Check if backup files (.swp, .bak) are triggering events

Missing Auth Updates

Problem: Auth file changes not reflected Solutions:
  1. Verify files have .json extension (only .json files watched)
  2. Check file is in configured auth directory
  3. Ensure JSON is valid (parse errors skip update)
  4. Check SHA256 hash (unchanged files are skipped)

High CPU Usage

Problem: Watcher consuming excessive CPU Solutions:
  1. Reduce number of files in auth directory
  2. Avoid watching directories with frequent changes
  3. Check for file system loops (symlinks)
  4. Monitor for “hot” files being written continuously

Next Steps

Service Builder

Configure and build the proxy service

Access Providers

Implement custom authentication

Advanced Features

Custom executors and translators

Getting Started

Build your first integration