Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions beacon/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ use std::sync::Arc;
use std::sync::atomic::{self, AtomicBool};
use std::thread;

pub const SERVER_ADDRESS: &str = "127.0.0.1:9167";

#[derive(Debug, Clone)]
pub struct Client {
sender: mpsc::Sender<Action>,
Expand Down Expand Up @@ -224,7 +222,8 @@ async fn run(

async fn _connect() -> Result<net::TcpStream, io::Error> {
log::debug!("Attempting to connect to server...");
let stream = net::TcpStream::connect(SERVER_ADDRESS).await?;
let addr = crate::server_address_from_env();
let stream = net::TcpStream::connect(&addr).await?;

stream.set_nodelay(true)?;
stream.writable().await?;
Expand Down
13 changes: 11 additions & 2 deletions beacon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ use tokio::net;
use tokio::sync::mpsc;
use tokio::task;

/// Defines a helper to retrieve the connection address from an envvar and
/// fallback to default otherwise.
pub fn server_address_from_env() -> String {
const SERVER_ADDRESS_DEFAULT: &str = "127.0.0.1:9167";

std::env::var("ICED_BEACON_SERVER_ADDRESS")
.unwrap_or_else(|_| String::from(SERVER_ADDRESS_DEFAULT))
}

#[derive(Debug, Clone)]
pub struct Connection {
commands: mpsc::Sender<client::Command>,
Expand Down Expand Up @@ -91,15 +100,15 @@ impl Event {
}

pub fn is_running() -> bool {
std::net::TcpListener::bind(client::SERVER_ADDRESS).is_err()
std::net::TcpListener::bind(server_address_from_env()).is_err()
}

pub fn run() -> impl Stream<Item = Event> {
stream::channel(|mut output| async move {
let mut buffer = Vec::new();

let server = loop {
match net::TcpListener::bind(client::SERVER_ADDRESS).await {
match net::TcpListener::bind(server_address_from_env()).await {
Ok(server) => break server,
Err(error) => {
if error.kind() == io::ErrorKind::AddrInUse {
Expand Down