From 4ac6d60d026db372500245049f1ed37bcf09d74f Mon Sep 17 00:00:00 2001 From: bigbong420 <207055395+bigbong420@users.noreply.github.com> Date: Sun, 29 Mar 2026 14:17:00 -0400 Subject: [PATCH] Autoscale: use php8.5, support a-z then aa/ab/etc --- etc/systemd/system/hosting-autoscale.service | 2 +- var/www/autoscale.php | 32 +++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/etc/systemd/system/hosting-autoscale.service b/etc/systemd/system/hosting-autoscale.service index 27296fc..0842260 100644 --- a/etc/systemd/system/hosting-autoscale.service +++ b/etc/systemd/system/hosting-autoscale.service @@ -3,4 +3,4 @@ Description=Auto-scale Tor hosting instances based on account count [Service] Type=oneshot -ExecStart=/usr/bin/php8.2 /var/www/autoscale.php +ExecStart=/usr/bin/php8.5 /var/www/autoscale.php diff --git a/var/www/autoscale.php b/var/www/autoscale.php index 420f074..c75e630 100644 --- a/var/www/autoscale.php +++ b/var/www/autoscale.php @@ -3,8 +3,23 @@ require(__DIR__ . '/common.php'); $db = get_db_instance(); $accounts_per_instance = 250; -$max_instances = 8; -$instance_chars = 'abcdefgh'; + +function next_instance_id(array $existing): string { + // a-z, then aa, ab, ... az, ba, ... + if(empty($existing)){ + return 'a'; + } + $last = end($existing); + $len = strlen($last); + for($i = $len - 1; $i >= 0; $i--){ + if($last[$i] !== 'z'){ + $last[$i] = chr(ord($last[$i]) + 1); + return $last; + } + $last[$i] = 'a'; + } + return str_repeat('a', $len + 1); +} $stmt = $db->query('SELECT instance, COUNT(*) as cnt FROM onions WHERE enabled IN (1, -2) GROUP BY instance;'); $loads = []; @@ -15,19 +30,15 @@ while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ $current = SERVICE_INSTANCES; $total_accounts = array_sum($loads); $needed = max(1, (int)ceil($total_accounts / $accounts_per_instance)); -$needed = min($needed, $max_instances); if($needed <= count($current)){ - echo "OK: $total_accounts accounts across " . count($current) . " instance(s), no scaling needed\n"; + echo "OK: $total_accounts accounts across " . count($current) . " instance(s)\n"; exit(0); } $new_instances = $current; -for($i = count($current); $i < $needed; $i++){ - $char = $instance_chars[$i]; - if(!in_array($char, $new_instances)){ - $new_instances[] = $char; - } +while(count($new_instances) < $needed){ + $new_instances[] = next_instance_id($new_instances); } $old_str = "const SERVICE_INSTANCES=['" . implode("','", $current) . "']"; @@ -40,7 +51,6 @@ if(strpos($common, $old_str) === false){ } file_put_contents('/var/www/common.php', str_replace($old_str, $new_str, $common)); -echo "SCALED: $total_accounts accounts, " . count($current) . " -> " . count($new_instances) . " instances ($new_str)\n"; +echo "SCALED: $total_accounts accounts, " . count($current) . " -> " . count($new_instances) . " instances\n"; -echo "Running setup.php to create new instances...\n"; require(__DIR__ . '/setup.php');