Skip to content

Commit 3b58d20

Browse files
committed
fix(gateway): move get_meta to admin API.
1 parent 0119de4 commit 3b58d20

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

gateway/rpc/proto/gateway_rpc.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ service Gateway {
133133
rpc RegisterCvm(RegisterCvmRequest) returns (RegisterCvmResponse) {}
134134
// List all ACME account URIs and the public key history of the certificates for the Content Addressable HTTPS.
135135
rpc AcmeInfo(google.protobuf.Empty) returns (AcmeInfoResponse) {}
136-
// Summary API for inspect.
137-
rpc GetMeta(google.protobuf.Empty) returns (GetMetaResponse);
138136
// Merge state from other Gateway instances.
139137
rpc UpdateState(GatewayState) returns (google.protobuf.Empty) {}
140138
}
@@ -157,4 +155,6 @@ service Admin {
157155
rpc ReloadCert(google.protobuf.Empty) returns (google.protobuf.Empty) {}
158156
// Set CAA records
159157
rpc SetCaa(google.protobuf.Empty) returns (google.protobuf.Empty) {}
158+
// Summary API for inspect.
159+
rpc GetMeta(google.protobuf.Empty) returns (GetMetaResponse) {}
160160
}

gateway/src/admin_service.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::sync::atomic::Ordering;
2+
use std::time::{SystemTime, UNIX_EPOCH};
23

34
use anyhow::{Context, Result};
45
use dstack_gateway_rpc::{
56
admin_server::{AdminRpc, AdminServer},
6-
GetInfoRequest, GetInfoResponse, HostInfo, RenewCertResponse, StatusResponse,
7+
GetInfoRequest, GetInfoResponse, GetMetaResponse, HostInfo, RenewCertResponse, StatusResponse,
78
};
89
use ra_rpc::{CallContext, RpcCall};
910

@@ -113,6 +114,34 @@ impl AdminRpc for AdminRpcHandler {
113114
})
114115
}
115116
}
117+
118+
async fn get_meta(self) -> Result<GetMetaResponse> {
119+
let state = self.state.lock();
120+
let handshakes = state.latest_handshakes(None)?;
121+
122+
// Total registered instances
123+
let registered = state.state.instances.len();
124+
125+
// Get current timestamp
126+
let now = SystemTime::now()
127+
.duration_since(UNIX_EPOCH)
128+
.context("system time before Unix epoch")?
129+
.as_secs();
130+
131+
// Count online instances (those with handshakes in last 5 minutes)
132+
let online = handshakes
133+
.values()
134+
.filter(|(ts, _)| {
135+
// Skip instances that never connected (ts == 0)
136+
*ts != 0 && (now - *ts) < 300
137+
})
138+
.count();
139+
140+
Ok(GetMetaResponse {
141+
registered: registered as u32,
142+
online: online as u32,
143+
})
144+
}
116145
}
117146

118147
impl RpcCall<Proxy> for AdminRpcHandler {

gateway/src/main_service.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use certbot::{CertBot, WorkDir};
1212
use cmd_lib::run_cmd as cmd;
1313
use dstack_gateway_rpc::{
1414
gateway_server::{GatewayRpc, GatewayServer},
15-
AcmeInfoResponse, GatewayState, GetMetaResponse, GuestAgentConfig, RegisterCvmRequest,
15+
AcmeInfoResponse, GatewayState, GuestAgentConfig, RegisterCvmRequest,
1616
RegisterCvmResponse, WireGuardConfig, WireGuardPeer,
1717
};
1818
use fs_err as fs;
@@ -691,34 +691,6 @@ impl GatewayRpc for RpcHandler {
691691
})
692692
}
693693

694-
async fn get_meta(self) -> Result<GetMetaResponse> {
695-
let state = self.state.lock();
696-
let handshakes = state.latest_handshakes(None)?;
697-
698-
// Total registered instances
699-
let registered = state.state.instances.len();
700-
701-
// Get current timestamp
702-
let now = SystemTime::now()
703-
.duration_since(UNIX_EPOCH)
704-
.context("system time before Unix epoch")?
705-
.as_secs();
706-
707-
// Count online instances (those with handshakes in last 5 minutes)
708-
let online = handshakes
709-
.values()
710-
.filter(|(ts, _)| {
711-
// Skip instances that never connected (ts == 0)
712-
*ts != 0 && (now - *ts) < 300
713-
})
714-
.count();
715-
716-
Ok(GetMetaResponse {
717-
registered: registered as u32,
718-
online: online as u32,
719-
})
720-
}
721-
722694
async fn update_state(self, request: GatewayState) -> Result<()> {
723695
self.ensure_from_gateway()?;
724696
let mut nodes = vec![];

0 commit comments

Comments
 (0)