Skip to content

Rate‐Limiting

ZILLEALI edited this page May 20, 2026 · 1 revision

Rate Limiting

Throttle RouterOS API calls to prevent router overload.


Overview

MikroTik routers have limited API processing capacity. High-frequency calls — especially from NOC dashboards or bulk operations — can spike router CPU and cause connection drops.

RateLimiter tracks calls per second and per minute, letting you throttle automatically.


Basic Usage

use ZillEAli\MikrotikLaravel\Support\RateLimiter;

$limiter = new RateLimiter(
    maxCallsPerSecond: 5,
    maxCallsPerMinute: 60
);

// Manual check
if ($limiter->isThrottled()) {
    usleep($limiter->getWaitMicroseconds());
}
$limiter->recordCall();
MikroTik::pppoe()->getActiveSessions();

// Auto-throttle — check + wait + record in one call
$limiter->throttle();
MikroTik::system()->getCpuLoad();

Recommended Limits

Use Case Per Second Per Minute
NOC Dashboard 5 60
Billing Sync 2 30
Bulk Operations 3 50
Monitoring 1 20
Production ISP 5 100

Stats and Monitoring

$stats = $limiter->getStats();
// [
//   'total_calls'       => 145,
//   'calls_this_second' => 3,
//   'calls_this_minute' => 45,
//   'max_per_second'    => 5,
//   'max_per_minute'    => 60,
//   'remaining_second'  => 2,
//   'remaining_minute'  => 15,
// ]

// Remaining capacity
$perSec = $limiter->getRemainingPerSecond();
$perMin = $limiter->getRemainingPerMinute();

// Total calls
$total = $limiter->getCallCount();

// Reset counters
$limiter->reset();

NexaLink Pattern — Bulk Operations

$limiter = new RateLimiter(
    maxCallsPerSecond: 3,
    maxCallsPerMinute: 50
);

// Disable 500 users with throttling
$users = PPPoEUser::whereDate('expiry', '<', today())->get();

foreach ($users as $user) {
    $limiter->throttle(); // auto-waits if needed
    MikroTik::pppoe()->disableSecret($user->username);
}

echo "Done. Total API calls: {$limiter->getCallCount()}";

Combined with Caching

$limiter = new RateLimiter(maxCallsPerSecond: 5);
$cached  = MikroTik::withCache(MikroTik::pppoe(), ttl: 30);

// Cache reduces actual API calls
// RateLimiter protects when cache misses
$limiter->throttle();
$sessions = $cached->getActiveSessions(); // cached — no router call

Caching | Changelog →


📝 Found an error or missing info?
Edit this page or open an issue to suggest improvements.

Clone this wiki locally