// Bootstrap-server dialing: rotate over the configured entries so repeated
// connects (and reconnects) spread across the listed brokers, and keep
// trying until one accepts.

///|
/// Rotating view over parsed bootstrap server addresses.
#derive(@debug.Debug)
pub struct BootstrapServers {
  addrs : Array[HostPort]
  mut cursor : Int32
}

///|
pub fn BootstrapServers::new(addrs : Array[HostPort]) -> BootstrapServers raise {
  if addrs.is_empty() {
    raise ProtocolError::ProtocolError(
      "bootstrap server list must not be empty",
    )
  }
  { addrs, cursor: 0, }
}

///|
/// Next address in round-robin order.
pub fn BootstrapServers::next(self : BootstrapServers) -> HostPort {
  let addr = self.addrs[self.cursor % self.addrs.length()]
  self.cursor += 1
  addr
}

///|
/// Dial the bootstrap servers in rotating order; the first connection that
/// succeeds wins. Raises the last failure if every entry refuses.
pub async fn connect_bootstrap(
  servers : BootstrapServers,
  client_id : String,
  timeout_ms~ : Int32 = 10000,
  sasl~ : SaslConfig? = None,
  use_tls~ : Bool = false,
  tls~ : TlsClientOptions? = None,
) -> BrokerConnection {
  let n = servers.addrs.length()
  let mut last_error : Error = ProtocolError::ProtocolError(
    "no bootstrap servers",
  )
  for _ in 0.. {
        last_error = e
        continue
      }
    }
    // SASL runs before the connection is handed out; an authentication
    // failure is NOT a dial failure, so it propagates without failover.
    match sasl {
      Some(sasl) => conn.authenticate(sasl, timeout_ms~)
      None => ()
    }
    // Every connection negotiates API versions, like the Java client:
    // later requests on this connection pick versions from the ranges.
    conn.negotiate_api_versions(timeout_ms~)
    return conn
  }
  raise TransportError::ConnectionClosed(
    "all \{n} bootstrap servers failed; last error: \{last_error}",
  )
}

///|
async fn dial(
  addr : HostPort,
  client_id : String,
  timeout_ms : Int32,
  use_tls : Bool,
  tls : TlsClientOptions?,
) -> BrokerConnection {
  // Without explicit TLS options, a TLS protocol still gets defaults whose
  // server name is the dialed host.
  let tls_opts = match tls {
    Some(opts) => Some(opts)
    None => if use_tls { Some(TlsClientOptions::new(addr.host)) } else { None }
  }
  @async.with_timeout(timeout_ms, () => {
    BrokerConnection::connect(
      addr.host,
      addr.port,
      client_id~,
      timeout_ms~,
      tls=tls_opts,
    )
  })
}