selectProxy method
Selects the next proxy from the given list
Implementation
@override
Proxy selectProxy(List<Proxy> proxies) {
if (proxies.isEmpty) {
throw ArgumentError('Proxy list cannot be empty');
}
// Calculate total weight
double totalWeight = 0;
final weights = <double>[];
for (final proxy in proxies) {
// Default weight is 1.0 if no score is available
double weight = 1.0;
if (proxy is ProxyModel && proxy.score != null) {
weight = proxy.score!.calculateScore();
}
weights.add(weight);
totalWeight += weight;
}
// Generate a random value between 0 and totalWeight
final random = _random.nextDouble();
final target = random * totalWeight;
// Find the proxy that corresponds to the random value
double cumulativeWeight = 0;
for (int i = 0; i < proxies.length; i++) {
cumulativeWeight += weights[i];
if (cumulativeWeight >= target) {
return proxies[i];
}
}
// Fallback to the last proxy (should not happen)
return proxies.last;
}