Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow API listener to bind to multiple addresses #195

Merged
merged 2 commits into from
Apr 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion crates/corro-agent/src/agent/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub async fn setup(conf: Config, tripwire: Tripwire) -> eyre::Result<(Agent, Age

let transport = Transport::new(&conf.gossip, rtt_tx).await?;

let api_listener = TcpListener::bind(conf.api.bind_addr).await?;
let api_listener = TcpListener::bind(conf.api.bind_addr.as_slice()).await?;
let api_addr = api_listener.local_addr()?;

let clock = Arc::new(
Expand Down
28 changes: 14 additions & 14 deletions crates/corro-agent/src/agent/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
//! a similar API facade, handle the same kinds of data, etc) should
//! be pulled out of this file in future.

use std::{
cmp,
collections::{BTreeMap, BTreeSet, HashSet},
convert::Infallible,
net::SocketAddr,
ops::RangeInclusive,
sync::{atomic::AtomicI64, Arc},
time::{Duration, Instant},
};

use crate::{
agent::{handlers, CountedExecutor, MAX_SYNC_BACKOFF, TO_CLEAR_COUNT},
api::public::{
Expand All @@ -34,6 +24,15 @@ use corro_types::{
config::AuthzConfig,
pubsub::SubsManager,
};
use std::{
cmp,
collections::{BTreeMap, BTreeSet, HashSet},
convert::Infallible,
net::SocketAddr,
ops::RangeInclusive,
sync::{atomic::AtomicI64, Arc},
time::{Duration, Instant},
};

use axum::{
error_handling::HandleErrorLayer,
Expand Down Expand Up @@ -486,8 +485,9 @@ pub async fn setup_http_api_handler(
.layer(TraceLayer::new_for_http());

let api_addr = api_listener.local_addr()?;
info!("Starting public API server on tcp/{api_addr}");
info!("Starting API listener on tcp/{api_addr}");
let mut incoming = AddrIncoming::from_listener(api_listener)?;

incoming.set_nodelay(true);
spawn_counted(
axum::Server::builder(incoming)
Expand Down Expand Up @@ -784,7 +784,7 @@ pub fn store_empty_changeset(
let deleted: Vec<RangeInclusive<Version>> = conn
.prepare_cached(
"
DELETE FROM __corro_bookkeeping
DELETE FROM __corro_bookkeeping
WHERE
actor_id = :actor_id AND
start_version >= COALESCE((
Expand All @@ -800,7 +800,7 @@ pub fn store_empty_changeset(
(
-- start_version is between start and end of range AND no end_version
( start_version BETWEEN :start AND :end AND end_version IS NULL ) OR

-- start_version and end_version are within the range
( start_version >= :start AND end_version <= :end ) OR

Expand Down Expand Up @@ -1389,7 +1389,7 @@ pub fn process_incomplete_version(
"
DELETE FROM __corro_seq_bookkeeping
WHERE site_id = :actor_id AND version = :version AND
(
(
-- start_seq and end_seq are within the range
( start_seq >= :start AND end_seq <= :end ) OR

Expand Down
20 changes: 11 additions & 9 deletions crates/corro-types/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::net::{Ipv6Addr, SocketAddr, SocketAddrV6};

use camino::Utf8PathBuf;
use serde::{Deserialize, Serialize};
use serde_with::{formats::PreferOne, serde_as, OneOrMany};

pub const DEFAULT_GOSSIP_PORT: u16 = 4001;
const DEFAULT_GOSSIP_IDLE_TIMEOUT: u32 = 30;
Expand Down Expand Up @@ -112,10 +113,12 @@ impl DbConfig {
}
}

#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiConfig {
#[serde(alias = "addr")]
pub bind_addr: SocketAddr,
#[serde_as(deserialize_as = "OneOrMany<_, PreferOne>")]
pub bind_addr: Vec<SocketAddr>,
#[serde(alias = "authz", default)]
pub authorization: Option<AuthzConfig>,
#[serde(default)]
Expand Down Expand Up @@ -280,7 +283,7 @@ impl Config {
pub struct ConfigBuilder {
pub db_path: Option<Utf8PathBuf>,
gossip_addr: Option<SocketAddr>,
api_addr: Option<SocketAddr>,
api_addr: Vec<SocketAddr>,
external_addr: Option<SocketAddr>,
admin_path: Option<Utf8PathBuf>,
prometheus_addr: Option<SocketAddr>,
Expand All @@ -305,12 +308,7 @@ impl ConfigBuilder {
}

pub fn api_addr(mut self, addr: SocketAddr) -> Self {
self.api_addr = Some(addr);
self
}

pub fn external_addr(mut self, addr: SocketAddr) -> Self {
self.external_addr = Some(addr);
self.api_addr.push(addr);
self
}

Expand Down Expand Up @@ -364,6 +362,10 @@ impl ConfigBuilder {
open_telemetry: None,
};

if self.api_addr.is_empty() {
return Err(ConfigBuilderError::ApiAddrRequired);
}

Ok(Config {
db: DbConfig {
path: db_path,
Expand All @@ -372,7 +374,7 @@ impl ConfigBuilder {
clear_overwritten_secs: None,
},
api: ApiConfig {
bind_addr: self.api_addr.ok_or(ConfigBuilderError::ApiAddrRequired)?,
bind_addr: self.api_addr,
authorization: None,
pg: None,
},
Expand Down
3 changes: 2 additions & 1 deletion crates/corrosion/src/command/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ pub async fn run(config: Config, config_path: &Utf8PathBuf) -> eyre::Result<()>
)?;

if !config.db.schema_paths.is_empty() {
let client = corro_client::CorrosionApiClient::new(config.api.bind_addr);
let client =
corro_client::CorrosionApiClient::new(config.api.bind_addr.first().unwrap().clone());
match client
.schema_from_paths(config.db.schema_paths.as_slice())
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/corrosion/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ impl Cli {
Ok(if let Some(api_addr) = self.api_addr {
api_addr
} else {
self.config()?.api.bind_addr
self.config()?.api.bind_addr.first().unwrap().clone()
})
}

Expand Down
Loading