@@ -11,7 +11,6 @@ use pony::http::MyRejection;
1111use pony:: http:: ResponseMessage ;
1212use pony:: memory:: tag:: ProtoTag ;
1313use pony:: utils;
14- use pony:: zmq:: publisher:: Publisher as ZmqPublisher ;
1514use pony:: Connection ;
1615use pony:: ConnectionApiOp ;
1716use pony:: ConnectionBaseOp ;
@@ -34,8 +33,7 @@ use super::super::request::ConnCreateRequest;
3433/// Handler get connection
3534// GET /connections
3635pub async fn get_connections_handler < N , C , S > (
37- conn_req : ConnTypeParam ,
38- publisher : ZmqPublisher ,
36+ req : ConnTypeParam ,
3937 memory : MemSync < N , C , S > ,
4038) -> Result < impl warp:: Reply , warp:: Rejection >
4139where
@@ -58,55 +56,58 @@ where
5856 + std:: convert:: From < pony:: Subscription > ,
5957{
6058 let mem = memory. memory . read ( ) . await ;
61- let proto = conn_req . proto ;
62- let env = conn_req . env ;
63-
64- let last_update = conn_req . last_update ;
59+ let proto = req . proto ;
60+ let topic = req . topic ;
61+ let last_update = req . last_update ;
62+ let env = req . env ;
6563
6664 let connections_to_send: Vec < _ > = mem
6765 . connections
6866 . iter ( )
6967 . filter ( |( _, conn) | {
70- if conn. get_deleted ( ) {
71- return false ;
72- }
73-
74- if conn. get_proto ( ) . proto ( ) != proto {
75- return false ;
76- }
77-
78- if let Some ( ts) = last_update {
79- conn. get_modified_at ( ) . and_utc ( ) . timestamp ( ) as u64 >= ts
80- } else {
81- true
82- }
68+ !conn. get_deleted ( )
69+ && conn. get_proto ( ) . proto ( ) == proto
70+ && env. as_ref ( ) . is_none_or ( |e| conn. get_env ( ) == * e)
71+ && last_update. is_none_or ( |ts| conn. get_modified_at ( ) . timestamp ( ) as u64 >= ts)
8372 } )
8473 . collect ( ) ;
8574
86- log:: debug!(
87- "Sending {} {:?} connections to auth" ,
88- connections_to_send. len( ) ,
89- proto
90- ) ;
91-
92- let messages: Vec < _ > = connections_to_send
93- . iter ( )
94- . map ( |( conn_id, conn) | conn. as_create_message ( conn_id) )
95- . collect ( ) ;
96-
97- let bytes = to_bytes :: < _ , 1024 > ( & messages) . map_err ( |e| {
98- log:: error!( "Serialization error: {}" , e) ;
99- warp:: reject:: custom ( MyRejection ( Box :: new ( e) ) )
100- } ) ?;
101-
102- publisher
103- . send_binary ( & env, bytes. as_ref ( ) )
104- . await
105- . map_err ( |e| {
106- log:: error!( "Publish error: {}" , e) ;
75+ if !connections_to_send. is_empty ( ) {
76+ log:: debug!(
77+ "Sending {} {:?} connections for env {:?} to topic {} " ,
78+ connections_to_send. len( ) ,
79+ proto,
80+ env,
81+ topic
82+ ) ;
83+
84+ let messages: Vec < _ > = connections_to_send
85+ . iter ( )
86+ . map ( |( conn_id, conn) | conn. as_create_message ( conn_id) )
87+ . collect ( ) ;
88+
89+ let bytes = to_bytes :: < _ , 1024 > ( & messages) . map_err ( |e| {
90+ log:: error!( "Serialization error: {}" , e) ;
10791 warp:: reject:: custom ( MyRejection ( Box :: new ( e) ) )
10892 } ) ?;
10993
94+ memory
95+ . publisher
96+ . send_binary ( & topic, bytes. as_ref ( ) )
97+ . await
98+ . map_err ( |e| {
99+ log:: error!( "Publish error: {}" , e) ;
100+ warp:: reject:: custom ( MyRejection ( Box :: new ( e) ) )
101+ } ) ?;
102+ } else {
103+ log:: debug!(
104+ "No message {} to send for env {:?} to topic {}" ,
105+ proto,
106+ env,
107+ topic
108+ ) ;
109+ }
110+
110111 let resp = ResponseMessage :: < Option < IdResponse > > {
111112 status : StatusCode :: OK . as_u16 ( ) ,
112113 message : "Ok" . to_string ( ) ,
@@ -122,7 +123,6 @@ where
122123// POST /connection
123124pub async fn create_connection_handler < N , C , S > (
124125 conn_req : ConnCreateRequest ,
125- publisher : ZmqPublisher ,
126126 memory : MemSync < N , C , S > ,
127127) -> Result < impl warp:: Reply , warp:: Rejection >
128128where
@@ -342,7 +342,7 @@ where
342342 conn. get_env ( )
343343 } ;
344344
345- let _ = publisher. send_binary ( & topic, bytes. as_ref ( ) ) . await ;
345+ let _ = memory . publisher . send_binary ( & topic, bytes. as_ref ( ) ) . await ;
346346
347347 Ok ( http:: success_response (
348348 format ! ( "Connection {} has been created" , id) ,
@@ -376,7 +376,7 @@ where
376376// DELETE /connection?id=
377377pub async fn delete_connection_handler < N , C , S > (
378378 conn_param : ConnQueryParam ,
379- publisher : ZmqPublisher ,
379+
380380 memory : MemSync < N , C , S > ,
381381) -> Result < impl warp:: Reply , warp:: Rejection >
382382where
@@ -413,36 +413,17 @@ where
413413
414414 if conn. get_deleted ( ) {
415415 return Ok ( http:: not_found ( & format ! (
416- "Connection {} is deleted" ,
416+ "Connection {} already is deleted" ,
417417 conn_id
418418 ) ) ) ;
419419 }
420420
421- match SyncOp :: delete_connection ( & memory, & conn_id) . await {
422- Ok ( StorageOperationStatus :: Ok ( id) ) => {
423- let msg = conn. as_delete_message ( & conn_id) ;
424-
425- let bytes = match rkyv:: to_bytes :: < _ , 1024 > ( & msg) {
426- Ok ( b) => b,
427- Err ( e) => {
428- return Ok ( http:: internal_error ( & format ! ( "Serialization error: {}" , e) ) ) ;
429- }
430- } ;
431-
432- if let Some ( node_id) = conn. get_wireguard_node_id ( ) {
433- let _ = publisher
434- . send_binary ( & node_id. to_string ( ) , bytes. as_ref ( ) )
435- . await ;
436- } else {
437- let _ = publisher. send_binary ( & conn. get_env ( ) , bytes. as_ref ( ) ) . await ;
438- }
439-
440- Ok ( http:: success_response (
441- format ! ( "Connection {} has been deleted" , id) ,
442- Some ( id) ,
443- http:: Instance :: Connection ( conn. clone ( ) . into ( ) ) ,
444- ) )
445- }
421+ match SyncOp :: delete_connection ( & memory, & conn_id, & conn) . await {
422+ Ok ( StorageOperationStatus :: Ok ( id) ) => Ok ( http:: success_response (
423+ format ! ( "Connection {} has been deleted" , id) ,
424+ Some ( id) ,
425+ http:: Instance :: Connection ( conn. clone ( ) . into ( ) ) ,
426+ ) ) ,
446427
447428 Ok ( StorageOperationStatus :: NotFound ( id) ) => {
448429 Ok ( http:: not_found ( & format ! ( "Connection {} not found" , id) ) )
0 commit comments