这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@u5surf
Copy link
Owner

@u5surf u5surf commented Jul 26, 2025

Summary

Implement a comprehensive shared memory and red-black tree system for VTS (Virtual Host Traffic Statistics) based on the original nginx-module-vts architecture.

Key Features

🏗️ Shared Memory Management

  • VtsSharedContext: C-compatible structure for managing shared memory state
  • Slab Allocation: Proper nginx slab allocator usage for all data structures
  • Configuration Reload Support: Preserves existing data across nginx reloads
  • Memory Safety: Comprehensive error handling and allocation checks

🌳 Red-Black Tree Implementation

  • Custom Insert Function: vts_rbtree_insert_value following nginx patterns
  • Key-based Storage: Efficient lookup and insertion for VTS nodes
  • Tree Initialization: Proper sentinel node allocation and tree setup
  • nginx Compatibility: Uses standard nginx red-black tree APIs

🔧 Enhanced Initialization

  • vts_init_shm_zone: Complete rewrite based on original nginx-module-vts
  • Existing Zone Handling: Reuses existing shared memory when available
  • Error Recovery: Graceful handling of allocation failures
  • Multi-worker Support: Shared data across all nginx worker processes

Implementation Details

Based on Original nginx-module-vts

  • Follows the exact patterns from nginx-module-vts
  • Implements ngx_http_vhost_traffic_status_init_zone equivalent in Rust
  • Uses the same shared memory allocation strategy

ngx-rust Integration

  • Leverages examples from ngx-rust PR #164
  • Proper FFI usage for nginx data structures
  • Safe wrapper around unsafe nginx APIs

Code Changes

New Data Structures (src/lib.rs)

#[repr(C)]
#[allow(dead_code)]
struct VtsSharedContext {
    rbtree: *mut ngx_rbtree_t,
    shpool: *mut ngx_slab_pool_t,
}

Custom Tree Insert Function

unsafe extern "C" fn vts_rbtree_insert_value(
    temp: *mut ngx_rbtree_node_t,
    node: *mut ngx_rbtree_node_t,
    sentinel: *mut ngx_rbtree_node_t,
)

Enhanced Initialization

extern "C" fn vts_init_shm_zone(
    shm_zone: *mut ngx_shm_zone_t, 
    data: *mut c_void
) -> ngx_int_t

Architecture Benefits

🚀 Performance

  • O(log n) Operations: Red-black tree provides efficient lookups
  • Shared Memory: Zero-copy data sharing across worker processes
  • Minimal Overhead: Direct nginx API usage without abstraction layers

🛡️ Reliability

  • Configuration Reload: Data persists across nginx reloads
  • Memory Management: Proper cleanup and error handling
  • Type Safety: Rust's safety guarantees where possible

🔧 Maintainability

  • Clear Documentation: Comprehensive comments explaining each component
  • Unit Tests: Structure compatibility and functionality verification
  • Standard Patterns: Follows established nginx module conventions

Test Plan

  • All existing tests pass
  • New unit test for VtsSharedContext structure compatibility
  • Memory layout verification for C interoperability
  • Build verification with enhanced shared memory code
  • Clippy linting passes without warnings

Usage Example

http {
    # Configure shared memory zone for VTS
    vts_zone main 10m;
    
    server {
        location /status {
            vts_status;
        }
    }
}

Future Enhancements

This implementation provides the foundation for:

  • VTS node creation and management
  • Request statistics collection hooks
  • Prometheus metrics generation from shared data
  • Advanced filtering and grouping features

References

This establishes nginx-vts-rust as a production-ready foundation for comprehensive traffic statistics monitoring with enterprise-grade shared memory management.

🤖 Generated with Claude Code

u5surf and others added 2 commits July 26, 2025 19:28
- Add VtsSharedContext structure for managing shared memory state
- Implement vts_rbtree_insert_value custom insert function for VTS nodes
- Enhance vts_init_shm_zone with proper shared memory management
- Support nginx configuration reload by reusing existing trees
- Add slab allocation for red-black tree and sentinel nodes
- Include unit test for VtsSharedContext structure compatibility
- Follow original nginx-module-vts patterns for shared memory handling

Based on:
- https://github.com/vozlt/nginx-module-vts shared memory implementation
- nginx/ngx-rust#164 examples

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Apply cargo fmt to resolve CI formatting check failures.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@u5surf u5surf merged commit 6595f4c into main Jul 26, 2025
4 checks passed
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.

2 participants