Skip to content

Conversation

@ammar-agent
Copy link
Contributor

Closes #88

Overview

Implements automatic reloading of JavaScript rules when using --js-file. The file is checked for modifications on each request, and if changed, the code is validated and atomically reloaded.

Implementation Approach

On-demand mtime checking (not background polling or inotify):

  • File modification time checked on each request in evaluate() method
  • When mtime changes: read file, validate JS, atomically update code
  • No background tasks - simpler, testable, zero overhead when idle

Key Design Decisions:

  1. Lock-free reads with ArcSwap - ArcSwap<(String, Option<SystemTime>)> for hot path performance
  2. Singleflight pattern - Mutex-based double-check locking prevents concurrent reloads
  3. Validation before reload - Invalid JS rejected, existing rules kept on error
  4. Atomic updates - Code and mtime always consistent

Code Refactoring

The evaluate() method was decomposed from 98 lines into focused helper methods:

  • check_and_reload_file() - File monitoring and reload logic (48 lines)
  • load_js_code() - Lock-free code loading (4 lines)
  • execute_js_blocking() - V8 execution in blocking task (32 lines)
  • build_evaluation_result() - Result construction (15 lines)

Also added comprehensive module and struct documentation.

Testing

  • ✅ 3 new integration tests in tests/js_file_reload.rs
  • ✅ All 44 existing unit tests pass
  • ✅ Clippy clean, properly formatted

Documentation

Updated:

  • README.md - File reload feature
  • docs/guide/quick-start.md - Usage examples
  • docs/guide/configuration.md - --js-file flag documentation
  • docs/guide/rule-engines/javascript.md - Detailed behavior

Dependencies

Added arc-swap = "1.7" to Cargo.toml for lock-free atomic updates.

This commit adds automatic reloading of JavaScript rules when using --js-file.

Key features:
- On-demand mtime checking (checked on each request)
- Lock-free reads with ArcSwap for hot path performance
- Singleflight pattern to prevent concurrent reloads
- Validation before reload (keeps existing rules on error)
- Comprehensive tests and documentation

Implementation details:
- Refactored evaluate() method into focused helper methods:
  - check_and_reload_file(): File monitoring and reload logic
  - load_js_code(): Lock-free code loading
  - execute_js_blocking(): V8 execution in blocking task
  - build_evaluation_result(): Result construction
- Added module and struct documentation
- Removed redundant create_and_execute() method
- All tests pass (44 unit tests + 3 reload tests)

Resolves #88
@ammario ammario merged commit 45ac4fe into main Nov 11, 2025
6 checks passed
@ammario ammario deleted the js-file-reload branch November 11, 2025 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect javascript rule file change

2 participants