SHL-004
Rotate shortlink providers by weight
Spread traffic across providers so one outage or one bad rate does not take the whole earning page down, with a weight you can tune per provider.
Relying on a single provider means their outage is your outage, their rate cut is your economy change, and their decision to drop your account is a site with no earning.
Weighted selection spreads it. Each provider carries a weight, traffic is distributed in proportion, and tuning one number moves traffic toward whoever is paying best this month.
Weight also expresses trust: a new provider can start at a low weight until the payouts actually arrive.
function pick_provider(PDO $pdo): ?array
{
$providers = $pdo->query(
'SELECT * FROM shortlink_providers WHERE is_active = 1 AND weight > 0 ORDER BY id'
)->fetchAll(PDO::FETCH_ASSOC);
if ($providers === []) {
return null;
}
$total = array_sum(array_map(static fn ($p) => (int) $p['weight'], $providers));
if ($total <= 0) {
return $providers[0];
}
$roll = random_int(1, $total);
$seen = 0;
foreach ($providers as $p) {
$seen += (int) $p['weight'];
if ($roll <= $seen) {
return $p;
}
}
return $providers[0]; // unreachable; never return null here
}
/** Drop a provider's weight automatically when it stops answering. */
function note_provider_failure(PDO $pdo, int $providerId): void
{
$pdo->prepare(
'UPDATE shortlink_providers
SET fails = fails + 1,
is_active = IF(fails + 1 >= 20, 0, is_active)
WHERE id = ?'
)->execute([$providerId]);
}
Using it
Record sent and completed counts per provider. The completion rate is what tells you which ones are actually paying.
Re-enable a disabled provider by hand rather than automatically. An outage that lasted a day is worth looking at before traffic returns.
Keep at least two active at all times, so the auto-disable can never leave you with none.
What bites people
random_int, not rand, if the choice influences earnings — predictability here is a small arbitrage.
Weights are relative, not percentages. Adding a provider at weight 100 next to two at weight 10 sends it 83% of traffic, which is rarely what anyone intends.
Reset the failure counter on a success, or a provider slowly accumulates fails over months and disables itself for no reason.