Overview
The watcher monitors configuration and authentication files for changes and triggers automatic reloads without restarting the service. This enables:- Hot-reload of
config.yamlwhen 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 monitorsconfig.yaml for changes:
internal/watcher/config_reload.go
- Detect file system event (Write, Create, or Rename)
- Wait 150ms debounce period (handles rapid successive writes)
- Read file and compute SHA256 hash
- Compare with previous hash
- If changed, reload configuration and trigger callback
- Persist changes if persistence is configured
Authentication File Watching
The watcher monitors all.json files in the auth directory:
internal/watcher/events.go
- Detect
.jsonfile event in auth directory - Compute SHA256 hash of file contents
- Compare with cached hash
- If unchanged, skip processing
- If changed, parse auth file and generate update events
- Emit
AuthUpdateevents to registered consumers - 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
Auth File Debouncing
internal/watcher/events.go
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
- 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
- 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:- Verify file paths are absolute
- Check file permissions (read access required)
- Ensure file system supports inotify/FSEvents
- Check for file system mount options (some NFS/network mounts don’t support events)
- 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:- Configure editor to write atomically
- Use single-write operations
- Check if backup files (
.swp,.bak) are triggering events
Missing Auth Updates
Problem: Auth file changes not reflected Solutions:- Verify files have
.jsonextension (only.jsonfiles watched) - Check file is in configured auth directory
- Ensure JSON is valid (parse errors skip update)
- Check SHA256 hash (unchanged files are skipped)
High CPU Usage
Problem: Watcher consuming excessive CPU Solutions:- Reduce number of files in auth directory
- Avoid watching directories with frequent changes
- Check for file system loops (symlinks)
- 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