1
1
use std:: {
2
2
collections:: { BTreeMap , BTreeSet } ,
3
3
net:: Ipv4Addr ,
4
- sync:: { atomic :: Ordering , Arc , Mutex , MutexGuard , Weak } ,
4
+ sync:: { Arc , Mutex , MutexGuard , Weak } ,
5
5
time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ,
6
6
} ;
7
7
@@ -11,9 +11,8 @@ use certbot::{CertBot, WorkDir};
11
11
use cmd_lib:: run_cmd as cmd;
12
12
use dstack_gateway_rpc:: {
13
13
gateway_server:: { GatewayRpc , GatewayServer } ,
14
- AcmeInfoResponse , GatewayState , GetInfoRequest , GetInfoResponse , GetMetaResponse ,
15
- GuestAgentConfig , HostInfo as PbHostInfo , RegisterCvmRequest , RegisterCvmResponse ,
16
- StatusResponse , WireGuardConfig , WireGuardPeer ,
14
+ AcmeInfoResponse , GatewayState , GetMetaResponse , GuestAgentConfig , RegisterCvmRequest ,
15
+ RegisterCvmResponse , WireGuardConfig , WireGuardPeer ,
17
16
} ;
18
17
use fs_err as fs;
19
18
use ra_rpc:: { CallContext , RpcCall , VerifiedAttestation } ;
@@ -29,7 +28,7 @@ use tracing::{debug, error, info, warn};
29
28
use crate :: {
30
29
config:: Config ,
31
30
models:: { InstanceInfo , WgConf } ,
32
- proxy:: { AddressGroup , AddressInfo , NUM_CONNECTIONS } ,
31
+ proxy:: { AddressGroup , AddressInfo } ,
33
32
} ;
34
33
35
34
mod sync_client;
@@ -55,18 +54,18 @@ pub(crate) struct GatewayNodeInfo {
55
54
}
56
55
57
56
#[ derive( Debug , Serialize , Deserialize , Default ) ]
58
- struct ProxyStateMut {
59
- nodes : BTreeMap < String , GatewayNodeInfo > ,
60
- apps : BTreeMap < String , BTreeSet < String > > ,
61
- instances : BTreeMap < String , InstanceInfo > ,
62
- allocated_addresses : BTreeSet < Ipv4Addr > ,
57
+ pub ( crate ) struct ProxyStateMut {
58
+ pub ( crate ) nodes : BTreeMap < String , GatewayNodeInfo > ,
59
+ pub ( crate ) apps : BTreeMap < String , BTreeSet < String > > ,
60
+ pub ( crate ) instances : BTreeMap < String , InstanceInfo > ,
61
+ pub ( crate ) allocated_addresses : BTreeSet < Ipv4Addr > ,
63
62
#[ serde( skip) ]
64
- top_n : BTreeMap < String , ( AddressGroup , Instant ) > ,
63
+ pub ( crate ) top_n : BTreeMap < String , ( AddressGroup , Instant ) > ,
65
64
}
66
65
67
66
pub ( crate ) struct ProxyState {
68
- config : Arc < Config > ,
69
- state : ProxyStateMut ,
67
+ pub ( crate ) config : Arc < Config > ,
68
+ pub ( crate ) state : ProxyStateMut ,
70
69
}
71
70
72
71
impl Proxy {
@@ -373,7 +372,7 @@ impl ProxyState {
373
372
/// Get latest handshakes
374
373
///
375
374
/// Return a map of public key to (timestamp, elapsed)
376
- fn latest_handshakes (
375
+ pub ( crate ) fn latest_handshakes (
377
376
& self ,
378
377
stale_timeout : Option < Duration > ,
379
378
) -> Result < BTreeMap < String , ( u64 , Duration ) > > {
@@ -559,7 +558,7 @@ impl ProxyState {
559
558
)
560
559
}
561
560
562
- fn refresh_state ( & mut self ) -> Result < ( ) > {
561
+ pub ( crate ) fn refresh_state ( & mut self ) -> Result < ( ) > {
563
562
let handshakes = self . latest_handshakes ( None ) ?;
564
563
for instance in self . state . instances . values_mut ( ) {
565
564
let Some ( ( ts, _) ) = handshakes. get ( & instance. public_key ) . copied ( ) else {
@@ -580,7 +579,7 @@ fn decode_ts(ts: u64) -> SystemTime {
580
579
. unwrap_or ( UNIX_EPOCH )
581
580
}
582
581
583
- fn encode_ts ( ts : SystemTime ) -> u64 {
582
+ pub ( crate ) fn encode_ts ( ts : SystemTime ) -> u64 {
584
583
ts. duration_since ( UNIX_EPOCH ) . unwrap_or_default ( ) . as_secs ( )
585
584
}
586
585
@@ -654,74 +653,6 @@ impl GatewayRpc for RpcHandler {
654
653
Ok ( response)
655
654
}
656
655
657
- async fn status ( self ) -> Result < StatusResponse > {
658
- let mut state = self . state . lock ( ) ;
659
- state. refresh_state ( ) ?;
660
- let base_domain = & state. config . proxy . base_domain ;
661
- let hosts = state
662
- . state
663
- . instances
664
- . values ( )
665
- . map ( |instance| PbHostInfo {
666
- instance_id : instance. id . clone ( ) ,
667
- ip : instance. ip . to_string ( ) ,
668
- app_id : instance. app_id . clone ( ) ,
669
- base_domain : base_domain. clone ( ) ,
670
- port : state. config . proxy . listen_port as u32 ,
671
- latest_handshake : encode_ts ( instance. last_seen ) ,
672
- num_connections : instance. num_connections ( ) ,
673
- } )
674
- . collect :: < Vec < _ > > ( ) ;
675
- let nodes = state
676
- . state
677
- . nodes
678
- . values ( )
679
- . cloned ( )
680
- . map ( Into :: into)
681
- . collect :: < Vec < _ > > ( ) ;
682
- Ok ( StatusResponse {
683
- url : state. config . sync . my_url . clone ( ) ,
684
- id : state. config . id ( ) ,
685
- bootnode_url : state. config . sync . bootnode . clone ( ) ,
686
- nodes,
687
- hosts,
688
- num_connections : NUM_CONNECTIONS . load ( Ordering :: Relaxed ) ,
689
- } )
690
- }
691
-
692
- async fn get_info ( self , request : GetInfoRequest ) -> Result < GetInfoResponse > {
693
- let state = self . state . lock ( ) ;
694
- let base_domain = & state. config . proxy . base_domain ;
695
- let handshakes = state. latest_handshakes ( None ) ?;
696
-
697
- if let Some ( instance) = state. state . instances . get ( & request. id ) {
698
- let host_info = PbHostInfo {
699
- instance_id : instance. id . clone ( ) ,
700
- ip : instance. ip . to_string ( ) ,
701
- app_id : instance. app_id . clone ( ) ,
702
- base_domain : base_domain. clone ( ) ,
703
- port : state. config . proxy . listen_port as u32 ,
704
- latest_handshake : {
705
- let ( ts, _) = handshakes
706
- . get ( & instance. public_key )
707
- . copied ( )
708
- . unwrap_or_default ( ) ;
709
- ts
710
- } ,
711
- num_connections : instance. num_connections ( ) ,
712
- } ;
713
- Ok ( GetInfoResponse {
714
- found : true ,
715
- info : Some ( host_info) ,
716
- } )
717
- } else {
718
- Ok ( GetInfoResponse {
719
- found : false ,
720
- info : None ,
721
- } )
722
- }
723
- }
724
-
725
656
async fn acme_info ( self ) -> Result < AcmeInfoResponse > {
726
657
let state = self . state . lock ( ) ;
727
658
let workdir = WorkDir :: new ( & state. config . certbot . workdir ) ;
0 commit comments