Skip to content

Commit ae7fee6

Browse files
authored
Make optional to listen to pg wire protocol and param for the port (#3309)
# Description of Changes As the title says. Add `--pg-port NUM` to the `start` command # API and ABI breaking changes Before this, it was set to `5432` unconditionally. Docs updated at #3302. # Expected complexity level and risk 1 # Testing - [x] Run smoke tests
1 parent 351af50 commit ae7fee6

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
if: runner.os == 'Windows'
5656
run: |
5757
cargo build -p spacetimedb-cli -p spacetimedb-standalone -p spacetimedb-update
58-
Start-Process target/debug/spacetimedb-cli.exe start
58+
Start-Process target/debug/spacetimedb-cli.exe start --pg-port 5432
5959
cd modules
6060
# the sdk-manifests on windows-latest are messed up, so we need to update them
6161
dotnet workload config --update-mode manifests

crates/standalone/src/subcommands/start.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ pub fn cli() -> clap::Command {
7575
"The maximum size of the page pool in bytes. Should be a multiple of 64KiB. The default is 8GiB.",
7676
),
7777
)
78+
.arg(
79+
Arg::new("pg_port")
80+
.long("pg-port")
81+
.help("If specified, enables the built-in PostgreSQL wire protocol server on the given port.")
82+
.value_parser(clap::value_parser!(u16).range(1024..65535)),
83+
)
7884
// .after_help("Run `spacetime help start` for more detailed information.")
7985
}
8086

@@ -94,6 +100,7 @@ impl ConfigFile {
94100

95101
pub async fn exec(args: &ArgMatches, db_cores: JobCores) -> anyhow::Result<()> {
96102
let listen_addr = args.get_one::<String>("listen_addr").unwrap();
103+
let pg_port = args.get_one::<u16>("pg_port");
97104
let cert_dir = args.get_one::<spacetimedb_paths::cli::ConfigDir>("jwt_key_dir");
98105
let certs = Option::zip(
99106
args.get_one::<PubKeyPath>("jwt_pub_key_path").cloned(),
@@ -181,21 +188,32 @@ pub async fn exec(args: &ArgMatches, db_cores: JobCores) -> anyhow::Result<()> {
181188

182189
let tcp = TcpListener::bind(listen_addr).await?;
183190
socket2::SockRef::from(&tcp).set_nodelay(true)?;
184-
log::debug!("Starting SpacetimeDB listening on {}", tcp.local_addr()?);
185-
let pg_server_addr = format!("{}:5432", listen_addr.split(':').next().unwrap());
186-
let tcp_pg = TcpListener::bind(pg_server_addr).await?;
191+
log::info!("Starting SpacetimeDB listening on {}", tcp.local_addr()?);
192+
193+
if let Some(pg_port) = pg_port {
194+
let server_addr = listen_addr.split(':').next().unwrap();
195+
let tcp_pg = TcpListener::bind(format!("{server_addr}:{pg_port}")).await?;
187196

188-
let notify = Arc::new(tokio::sync::Notify::new());
189-
let shutdown_notify = notify.clone();
190-
tokio::select! {
191-
_ = pg_server::start_pg(notify.clone(), ctx, tcp_pg) => {},
192-
_ = axum::serve(tcp, service).with_graceful_shutdown(async move {
193-
shutdown_notify.notified().await;
194-
}) => {},
195-
_ = tokio::signal::ctrl_c() => {
196-
println!("Shutting down servers...");
197-
notify.notify_waiters(); // Notify all tasks
197+
let notify = Arc::new(tokio::sync::Notify::new());
198+
let shutdown_notify = notify.clone();
199+
tokio::select! {
200+
_ = pg_server::start_pg(notify.clone(), ctx, tcp_pg) => {},
201+
_ = axum::serve(tcp, service).with_graceful_shutdown(async move {
202+
shutdown_notify.notified().await;
203+
}) => {},
204+
_ = tokio::signal::ctrl_c() => {
205+
println!("Shutting down servers...");
206+
notify.notify_waiters(); // Notify all tasks
207+
}
198208
}
209+
} else {
210+
log::warn!("PostgreSQL wire protocol server disabled");
211+
axum::serve(tcp, service)
212+
.with_graceful_shutdown(async {
213+
tokio::signal::ctrl_c().await.expect("failed to install Ctrl+C handler");
214+
log::info!("Shutting down server...");
215+
})
216+
.await?;
199217
}
200218

201219
Ok(())

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ services:
2929
- "5432:5432"
3030
# Tracy
3131
- "8086:8086"
32-
entrypoint: cargo watch -i flamegraphs -i log.conf --why -C crates/standalone -x 'run start --data-dir=/stdb/data --jwt-pub-key-path=/etc/spacetimedb/id_ecdsa.pub --jwt-priv-key-path=/etc/spacetimedb/id_ecdsa'
32+
entrypoint: cargo watch -i flamegraphs -i log.conf --why -C crates/standalone -x 'run start --data-dir=/stdb/data --jwt-pub-key-path=/etc/spacetimedb/id_ecdsa.pub --jwt-priv-key-path=/etc/spacetimedb/id_ecdsa --pg-port 5432'
3333
privileged: true
3434
environment:
3535
SPACETIMEDB_FLAMEGRAPH_PATH: ../../../../flamegraphs/flamegraph.folded

run_standalone_temp.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ cargo run -p spacetimedb-standalone -- start \
2121
--data-dir ${STDB_PATH} \
2222
--jwt-pub-key-path "${STDB_PATH}/id_ecdsa.pub" \
2323
--jwt-priv-key-path "${STDB_PATH}/id_ecdsa" \
24+
--pg-port 5432 \
2425
-l 127.0.0.1:3000 --enable-tracy

0 commit comments

Comments
 (0)