getNextProxy method

  1. @override
Proxy? getNextProxy()
override

Gets the next proxy from the internal list

Implementation

@override
Proxy? getNextProxy() {
  if (_proxies.isEmpty) {
    return null;
  }

  // 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;
}