def _ContractAperture(self, force=False):
    """Attempt to contract the aperture.  By calling this it's assume the aperture
    needs to be contracted.

    The aperture can be contracted if it's current size is larger than the
    min size.
    """
    if self._pending_endpoints and not force:
      return

    num_healthy = len([c for c in self._heap[1:] if c.channel.is_open])
    if num_healthy > self._min_size:
      least_loaded_endpoint = None
      # Scan the heap for any closed endpoints.
      for n in self._heap[1:]:
        if n.channel.is_closed and n.endpoint not in self._pending_endpoints:
          least_loaded_endpoint = n.endpoint
          break
      if not least_loaded_endpoint:
        # Scan the heap for the least-loaded node.  This isn't exactly in-order,
        # but "close enough"
        for n in self._heap[1:]:
          if n.endpoint not in self._pending_endpoints:
            least_loaded_endpoint = n.endpoint
            break

      if least_loaded_endpoint:
        self._idle_endpoints.add(least_loaded_endpoint)
        super(ApertureBalancerSink, self)._RemoveSink(least_loaded_endpoint)
        self._log.debug('Contracting aperture to remove %s' % str(least_loaded_endpoint))
        self._UpdateSizeVarz()
dataset/ETHPy150Open steveniemitz/scales/scales/loadbalancer/aperture.py/ApertureBalancerSink._ContractAperture/original