Skip to content

Commit 84c6a40

Browse files
authored
feat(pyth-lazer-agent): liveness and readiness endpoints
2 parents cef03e2 + 0ca1363 commit 84c6a40

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

.github/CODEOWNERS

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apps/api-reference @pyth-network/web-team
22
apps/entropy-debugger @pyth-network/web-team
33
apps/insights @pyth-network/web-team
44
apps/staking @pyth-network/web-team
5+
apps/pyth-lazer-agent @merolish
56
packages/component-library @pyth-network/web-team
67
packages/known-publishers @pyth-network/web-team
78
Dockerfile.node @pyth-network/web-team
@@ -16,11 +17,11 @@ turbo.json @pyth-network/web-team
1617
.github/workflows/ci-turbo-build.yml @pyth-network/web-team
1718
.github/workflows/ci-turbo-test.yml @pyth-network/web-team
1819

19-
/lazer/contracts/aptos @Riateche @ali-bahjati
20-
/lazer/contracts/evm @Riateche @ali-bahjati
21-
/lazer/contracts/solana @Riateche @ali-bahjati
20+
/lazer/contracts/aptos @Riateche @ali-behjati
21+
/lazer/contracts/evm @Riateche @ali-behjati
22+
/lazer/contracts/solana @Riateche @ali-behjati
2223
/lazer/publisher_sdk @darunrs @Riateche
23-
/lazer/sdk/js @ali-bahjati @keyvankhademi
24+
/lazer/sdk/js @ali-behjati @keyvankhademi
2425
/lazer/sdk/rust @darunrs @Riateche
2526

2627
flake.lock @cprussin

apps/pyth-lazer-agent/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/pyth-lazer-agent/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-lazer-agent"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2024"
55

66
[dependencies]

apps/pyth-lazer-agent/src/http_server.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ pub struct RelayerRequest(pub http::Request<hyper::body::Incoming>);
2828
const PUBLISHER_WS_URI: &str = "/v1/publisher";
2929
const PUBLISHER_WS_URI_V2: &str = "/v2/publisher";
3030

31+
const READINESS_PROBE_PATH: &str = "/ready";
32+
const LIVENESS_PROBE_PATH: &str = "/live";
33+
3134
pub async fn run(config: Config, lazer_publisher: LazerPublisher) -> Result<()> {
3235
let listener = TcpListener::bind(&config.listen_address).await?;
3336
info!("listening on {:?}", &config.listen_address);
@@ -74,6 +77,22 @@ async fn request_handler(
7477
let request_type = match path {
7578
PUBLISHER_WS_URI => Request::PublisherV1,
7679
PUBLISHER_WS_URI_V2 => Request::PublisherV2,
80+
LIVENESS_PROBE_PATH => {
81+
let response = Response::builder().status(StatusCode::OK);
82+
return Ok(response.body(FullBody::default())?);
83+
}
84+
READINESS_PROBE_PATH => {
85+
let status = if lazer_publisher
86+
.is_ready
87+
.load(std::sync::atomic::Ordering::Relaxed)
88+
{
89+
StatusCode::OK
90+
} else {
91+
StatusCode::SERVICE_UNAVAILABLE
92+
};
93+
let response = Response::builder().status(status);
94+
return Ok(response.body(FullBody::default())?);
95+
}
7796
_ => {
7897
return Ok(Response::builder()
7998
.status(StatusCode::NOT_FOUND)

apps/pyth-lazer-agent/src/lazer_publisher.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use pyth_lazer_publisher_sdk::transaction::{
1414
};
1515
use solana_keypair::read_keypair_file;
1616
use std::path::PathBuf;
17+
use std::sync::Arc;
18+
use std::sync::atomic::AtomicBool;
1719
use tokio::sync::broadcast;
1820
use tokio::{
1921
select,
@@ -25,6 +27,7 @@ use tracing::error;
2527
#[derive(Clone)]
2628
pub struct LazerPublisher {
2729
sender: Sender<FeedUpdate>,
30+
pub(crate) is_ready: Arc<AtomicBool>,
2831
}
2932

3033
impl LazerPublisher {
@@ -66,11 +69,13 @@ impl LazerPublisher {
6669
};
6770

6871
let (relayer_sender, _) = broadcast::channel(CHANNEL_CAPACITY);
72+
let is_ready = Arc::new(AtomicBool::new(false));
6973
for url in config.relayer_urls.iter() {
7074
let mut task = RelayerSessionTask {
7175
url: url.clone(),
7276
token: authorization_token.clone(),
7377
receiver: relayer_sender.subscribe(),
78+
is_ready: is_ready.clone(),
7479
};
7580
tokio::spawn(async move { task.run().await });
7681
}
@@ -84,7 +89,10 @@ impl LazerPublisher {
8489
signing_key,
8590
};
8691
tokio::spawn(async move { task.run().await });
87-
Self { sender }
92+
Self {
93+
sender,
94+
is_ready: is_ready.clone(),
95+
}
8896
}
8997

9098
pub async fn push_feed_update(&self, feed_update: FeedUpdate) -> Result<()> {

apps/pyth-lazer-agent/src/relayer_session.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use futures_util::{SinkExt, StreamExt};
66
use http::HeaderValue;
77
use protobuf::Message;
88
use pyth_lazer_publisher_sdk::transaction::SignedLazerTransaction;
9+
use std::sync::Arc;
10+
use std::sync::atomic::{AtomicBool, Ordering};
911
use std::time::{Duration, Instant};
1012
use tokio::net::TcpStream;
1113
use tokio::select;
@@ -63,6 +65,7 @@ pub struct RelayerSessionTask {
6365
pub url: Url,
6466
pub token: String,
6567
pub receiver: broadcast::Receiver<SignedLazerTransaction>,
68+
pub is_ready: Arc<AtomicBool>,
6669
}
6770

6871
impl RelayerSessionTask {
@@ -116,6 +119,9 @@ impl RelayerSessionTask {
116119
ws_sender: relayer_ws_sender,
117120
};
118121

122+
// If we have at least one successful connection, mark as ready.
123+
self.is_ready.store(true, Ordering::Relaxed);
124+
119125
loop {
120126
select! {
121127
recv_result = self.receiver.recv() => {
@@ -174,6 +180,8 @@ mod tests {
174180
Ed25519SignatureData, LazerTransaction, SignatureData, SignedLazerTransaction,
175181
};
176182
use std::net::SocketAddr;
183+
use std::sync::Arc;
184+
use std::sync::atomic::AtomicBool;
177185
use tokio::net::TcpListener;
178186
use tokio::sync::{broadcast, mpsc};
179187
use url::Url;
@@ -234,6 +242,7 @@ mod tests {
234242
url: Url::parse("ws://127.0.0.1:12346").unwrap(),
235243
token: "token1".to_string(),
236244
receiver: relayer_receiver,
245+
is_ready: Arc::new(AtomicBool::new(false)),
237246
};
238247
tokio::spawn(async move { relayer_session_task.run().await });
239248
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;

0 commit comments

Comments
 (0)