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 weights for each proxy
  final weights = <double>[];
  double totalWeight = 0;

  for (final proxy in _proxies) {
    final weight = _calculateWeight(proxy);
    weights.add(weight);
    totalWeight += weight;
  }

  // If all weights are zero, use equal weights
  if (totalWeight <= 0) {
    final equalWeight = 1.0 / _proxies.length;
    for (int i = 0; i < weights.length; i++) {
      weights[i] = equalWeight;
    }
    totalWeight = 1.0;
  }

  // Select a proxy based on its weight
  double randomValue = _random.nextDouble() * totalWeight;
  double cumulativeWeight = 0;

  for (int i = 0; i < _proxies.length; i++) {
    cumulativeWeight += weights[i];
    if (randomValue <= cumulativeWeight) {
      return _proxies[i];
    }
  }

  // Fallback to the last proxy
  return _proxies.last;
}