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 1 commit
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
3 changes: 2 additions & 1 deletion crates/corro-agent/src/agent/run_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async fn run(agent: Agent, opts: AgentOptions, pconf: PerfConfig) -> eyre::Resul
gossip_server_endpoint,
transport,
api_listener,
extra_api_listeners,
tripwire,
lock_registry,
rx_bcast,
Expand Down Expand Up @@ -96,7 +97,7 @@ async fn run(agent: Agent, opts: AgentOptions, pconf: PerfConfig) -> eyre::Resul
&tripwire,
subs_bcast_cache,
&subs_manager,
api_listener,
(api_listener, extra_api_listeners),
)
.await?;

Expand Down
7 changes: 7 additions & 0 deletions crates/corro-agent/src/agent/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct AgentOptions {
pub gossip_server_endpoint: quinn::Endpoint,
pub transport: Transport,
pub api_listener: TcpListener,
pub extra_api_listeners: Vec<TcpListener>,
pub rx_bcast: CorroReceiver<BroadcastInput>,
pub rx_apply: CorroReceiver<(ActorId, Version)>,
pub rx_clear_buf: CorroReceiver<(ActorId, RangeInclusive<Version>)>,
Expand Down Expand Up @@ -135,6 +136,11 @@ pub async fn setup(conf: Config, tripwire: Tripwire) -> eyre::Result<(Agent, Age
let api_listener = TcpListener::bind(conf.api.bind_addr).await?;
let api_addr = api_listener.local_addr()?;

let mut extra_api_listeners = vec![];
for addr in &conf.api.extra_bind_addrs {
extra_api_listeners.push(TcpListener::bind(addr).await?);
}

let clock = Arc::new(
uhlc::HLCBuilder::default()
.with_id(actor_id.try_into().unwrap())
Expand Down Expand Up @@ -167,6 +173,7 @@ pub async fn setup(conf: Config, tripwire: Tripwire) -> eyre::Result<(Agent, Age
gossip_server_endpoint,
transport,
api_listener,
extra_api_listeners,
lock_registry,
rx_bcast,
rx_apply,
Expand Down
44 changes: 29 additions & 15 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 @@ -384,7 +383,7 @@ pub async fn setup_http_api_handler(
tripwire: &Tripwire,
subs_bcast_cache: BcastCache,
subs_manager: &SubsManager,
api_listener: TcpListener,
(api_listener, extra_listeners): (TcpListener, Vec<TcpListener>),
) -> eyre::Result<()> {
let api = Router::new()
// transactions
Expand Down Expand Up @@ -485,9 +484,24 @@ pub async fn setup_http_api_handler(
.layer(DefaultBodyLimit::disable())
.layer(TraceLayer::new_for_http());

spawn_server_on_bind(api_listener, api.clone(), &tripwire)?;

for extra_addr in extra_listeners {
spawn_server_on_bind(extra_addr, api.clone(), &tripwire)?;
}

Ok(())
}

fn spawn_server_on_bind(
api_listener: TcpListener,
api: Router,
tripwire: &Tripwire,
) -> eyre::Result<()> {
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 +798,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 +814,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 +1403,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
9 changes: 9 additions & 0 deletions crates/corro-types/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ impl DbConfig {
pub struct ApiConfig {
#[serde(alias = "addr")]
pub bind_addr: SocketAddr,
#[serde(alias = "extra_addrs")]
pub extra_bind_addrs: Vec<SocketAddr>,
spacekookie marked this conversation as resolved.
Show resolved Hide resolved
#[serde(alias = "authz", default)]
pub authorization: Option<AuthzConfig>,
#[serde(default)]
Expand Down Expand Up @@ -281,6 +283,7 @@ pub struct ConfigBuilder {
pub db_path: Option<Utf8PathBuf>,
gossip_addr: Option<SocketAddr>,
api_addr: Option<SocketAddr>,
extra_api_addrs: Vec<SocketAddr>,
external_addr: Option<SocketAddr>,
admin_path: Option<Utf8PathBuf>,
prometheus_addr: Option<SocketAddr>,
Expand Down Expand Up @@ -309,6 +312,11 @@ impl ConfigBuilder {
self
}

pub fn extra_api_addr(mut self, addr: SocketAddr) -> Self {
self.extra_api_addrs.push(addr);
self
}

pub fn external_addr(mut self, addr: SocketAddr) -> Self {
self.external_addr = Some(addr);
self
Expand Down Expand Up @@ -373,6 +381,7 @@ impl ConfigBuilder {
},
api: ApiConfig {
bind_addr: self.api_addr.ok_or(ConfigBuilderError::ApiAddrRequired)?,
extra_bind_addrs: self.extra_api_addrs,
authorization: None,
pg: None,
},
Expand Down
Loading