vane 0.9.2

A flow-based reverse proxy with multi-layer routing and programmable pipelines.
---
title: Resource Monitoring
description: Adaptive memory management and system resource tracking in Vane.
icon: Activity
---

import { Steps, Step } from 'fumadocs-ui/components/steps';

Vane implements an adaptive resource monitoring system to ensure high performance while preventing system-wide resource exhaustion, particularly during heavy Layer 7 (Application) processing where request and response bodies are buffered in memory.

The core of this system resides in `src/bootstrap/monitor.rs` and the L7 container memory accounting.

## L7 Adaptive Memory Management

Vane's most critical monitor is the L7 memory limit controller. Unlike traditional proxies with static buffer limits, Vane can dynamically adjust its global memory quota based on the actual real-time availability of system RAM.

### Operational Modes

The monitor operates in one of two modes based on the environment configuration:

1.  **Adaptive Mode (Default)**: Dynamically calculates the global buffer limit every second based on system free memory.
2.  **Fixed Mode**: Uses a static byte limit (`L7_GLOBAL_BUFFER_LIMIT`). This is used if `L7_ADAPTIVE_MEMORY_LIMIT` is set to `false` or if the host platform is not supported.

### The Calculation Logic

In adaptive mode, the monitor performs the following calculation every second:

```rust
let calculated_limit = (free_mem * ratio / 100) + used_by_vane;
```

- **`free_mem`**: Actual available physical RAM detected from the OS.
- **`ratio`**: The target percentage of free memory Vane is allowed to claim (default is 85%, configurable via `L7_ADAPTIVE_MEMORY_RATIO`).
- **`used_by_vane`**: The current total bytes tracked in `GLOBAL_L7_BUFFERED_BYTES`.

By adding the current usage to a percentage of the _remaining_ free memory, Vane maintains a "moving ceiling" that shrinks as other system processes consume RAM and expands when resources are released.

## Implementation Deep Dive

<Steps>
<Step>

### Platform Detection

The `src/common/sys/system.rs` module provides cross-platform abstractions for memory detection.

<Mermaid
	chart="
graph LR
    OS[Operating System] --> Check{Check Platform}
    Check -- Linux --> Proc[Parse /proc/meminfo]
    Check -- macOS/BSD --> Sysctl[sysctl vm.page_free_count]
    Proc & Sysctl --> Bytes[Available Bytes]
"
/>

</Step>
<Step>

### Background Orchestration

The monitor is spawned as a detached `tokio` task. It continuously polls the system and updates the global limit atomically.

<Mermaid
	chart="
graph LR
    Timer[Interval 1s] --> Detect[Get Free Memory]
    Detect --> Calc[Calculate Limit]
    Calc --> Atomic[Update CURRENT_MEMORY_LIMIT]
    Atomic --> Timer
"
/>

</Step>
<Step>

### Global Accounting

The L7 layer uses an atomic counter, `GLOBAL_L7_BUFFERED_BYTES`. Every plugin or middleware that buffers a payload must increment this counter through a `BufferGuard`.

<Mermaid
	chart="
graph LR
    Req[Buffer Request] --> Check{Check Limit}
    Check -- Exceeded --> Deny[Return Error]
    Check -- OK --> Inc[Atomic Add]
    Inc --> Guard[Create BufferGuard]
    Guard --> Drop[Drop Guard]
    Drop --> Dec[Atomic Sub]
"
/>

</Step>
</Steps>

## Environment Configuration

The monitoring behavior is controlled via several environment variables:

| Variable                   | Default     | Description                                     |
| :------------------------- | :---------- | :---------------------------------------------- |
| `L7_ADAPTIVE_MEMORY_LIMIT` | `true`      | Enables or disables adaptive memory management. |
| `L7_ADAPTIVE_MEMORY_RATIO` | `85`        | The % of available memory Vane aims to utilize. |
| `L7_GLOBAL_BUFFER_LIMIT`   | `536870912` | 512MB fallback limit if adaptive mode is off.   |

<Callout type="info" title="Safety Margin">
	The adaptive ratio is capped at 95% internally to ensure that the operating system always has a
	small buffer of memory available for critical kernel operations, even under extreme Vane load.
</Callout>