Private
Public Access
1
0

feat: implement bandwidth enforcement mechanism in Proxmox server component to throttle servers exceeding usage limits, enhancing resource management

This commit is contained in:
2025-12-04 08:24:10 -05:00
parent 4b0f3e0621
commit da1f74365a
3 changed files with 208 additions and 1 deletions

View File

@@ -12,6 +12,9 @@ class Server extends Component
public function mount(ServerModel $server): void
{
parent::mount($server);
// Check and enforce bandwidth throttling if exceeded
$this->checkAndEnforceBandwidthLimit();
}
public function getMonthlyTrafficProperty()
@@ -97,6 +100,68 @@ class Server extends Component
});
}
protected function checkAndEnforceBandwidthLimit()
{
$monthlyTraffic = $this->monthlyTraffic;
$threshold = $this->bandwidthThreshold95;
// Check if bandwidth is exceeded
if ($monthlyTraffic > $threshold) {
// Throttle the server to 1 MiB/s
$this->throttleServerBandwidth();
}
}
protected function throttleServerBandwidth()
{
try {
$proxmox = new \Paymenter\Extensions\Servers\Proxmox\Proxmox;
// Get current VM config
$config = $proxmox->request('/nodes/' . $this->server->node->name . '/qemu/' . $this->server->vm_id . '/config')->json()['data'];
$updates = [];
// Update all network interfaces to rate limit of 1 MiB/s
foreach ($config as $key => $value) {
if (preg_match('/^net\d+$/', $key)) {
// Parse existing network config
$parts = explode(',', $value);
$newParts = [];
$hasRate = false;
foreach ($parts as $part) {
if (strpos($part, 'rate=') === 0) {
// Replace existing rate
$newParts[] = 'rate=1';
$hasRate = true;
} else {
$newParts[] = $part;
}
}
// Add rate if not present
if (!$hasRate) {
$newParts[] = 'rate=1';
}
$updates[$key] = implode(',', $newParts);
}
}
// Apply the updates if any network interfaces were found
if (!empty($updates)) {
$proxmox->request('/nodes/' . $this->server->node->name . '/qemu/' . $this->server->vm_id . '/config', 'put', $updates);
}
} catch (\Throwable $e) {
// Log the error but don't fail the page load
\Log::error('Failed to throttle bandwidth for server ' . $this->server->id, [
'error' => $e->getMessage(),
'server_id' => $this->server->id,
]);
}
}
public function render()
{
return view('proxmox::server');