resolveFreePort function

  1. @override
Future<int> resolveFreePort(
  1. int port
)

Implementation

@override
Future<int> resolveFreePort(int port) {
  var startPort = port - 100;
  var endPort = port + 100;

  var initPort = _randomPort(startPort, endPort);
  var initPortAttempt = 0;

  while (++initPortAttempt < 200 && _initFreePorts.contains(initPort)) {
    initPort = _randomPort(startPort, endPort);
  }

  _initFreePorts.add(initPort);

  return getFreeListenPort(
          ports: [initPort], startPort: startPort, endPort: endPort)
      .then((freePort) => freePort ?? port);
}