@@ -9,7 +9,12 @@ use tokio::sync::RwLock;
99use crate :: {
1010 exceptions:: rust_err:: NatsrpyError ,
1111 subscription:: Subscription ,
12- utils:: { headers:: NatsrpyHeadermapExt , natsrpy_future, py_types:: SendableValue } ,
12+ utils:: {
13+ futures:: natsrpy_future_with_timeout,
14+ headers:: NatsrpyHeadermapExt ,
15+ natsrpy_future,
16+ py_types:: { SendableValue , TimeoutValue } ,
17+ } ,
1318} ;
1419
1520#[ pyo3:: pyclass( name = "Nats" ) ]
@@ -23,8 +28,8 @@ pub struct NatsCls {
2328 read_buffer_capacity : u16 ,
2429 sender_capacity : usize ,
2530 max_reconnects : Option < usize > ,
26- connection_timeout : Duration ,
27- request_timeout : Option < Duration > ,
31+ connection_timeout : TimeoutValue ,
32+ request_timeout : Option < TimeoutValue > ,
2833}
2934
3035#[ pyo3:: pymethods]
@@ -40,8 +45,8 @@ impl NatsCls {
4045 read_buffer_capacity=65535 ,
4146 sender_capacity=128 ,
4247 max_reconnects=None ,
43- connection_timeout=Duration :: from_secs ( 5 ) ,
44- request_timeout=Duration :: from_secs ( 10 ) ,
48+ connection_timeout=TimeoutValue :: FloatSecs ( 5.0 ) ,
49+ request_timeout=TimeoutValue :: FloatSecs ( 10.0 ) ,
4550 ) ) ]
4651 fn __new__ (
4752 addrs : Vec < String > ,
@@ -52,8 +57,8 @@ impl NatsCls {
5257 read_buffer_capacity : u16 ,
5358 sender_capacity : usize ,
5459 max_reconnects : Option < usize > ,
55- connection_timeout : Duration ,
56- request_timeout : Option < Duration > ,
60+ connection_timeout : TimeoutValue ,
61+ request_timeout : Option < TimeoutValue > ,
5762 ) -> Self {
5863 Self {
5964 nats_session : Arc :: new ( RwLock :: new ( None ) ) ,
@@ -80,8 +85,8 @@ impl NatsCls {
8085 }
8186 conn_opts = conn_opts
8287 . max_reconnects ( self . max_reconnects )
83- . connection_timeout ( self . connection_timeout )
84- . request_timeout ( self . request_timeout )
88+ . connection_timeout ( self . connection_timeout . into ( ) )
89+ . request_timeout ( self . request_timeout . map ( Into :: into ) )
8590 . read_buffer_capacity ( self . read_buffer_capacity )
8691 . client_capacity ( self . sender_capacity ) ;
8792
@@ -94,23 +99,24 @@ impl NatsCls {
9499
95100 let session = self . nats_session . clone ( ) ;
96101 let address = self . addr . clone ( ) ;
97- let startup_future = async move {
98- if session. read ( ) . await . is_some ( ) {
99- return Err ( NatsrpyError :: SessionError (
100- "NATS session already exists" . to_string ( ) ,
101- ) ) ;
102- }
103- // Scoping for early-dropping of a guard.
104- {
105- let mut sesion_guard = session. write ( ) . await ;
106- * sesion_guard = Some ( conn_opts. connect ( address) . await ?) ;
107- }
108- Ok ( ( ) )
109- } ;
110102 let timeout = self . connection_timeout ;
111- return Ok ( natsrpy_future ( py, async move {
112- tokio:: time:: timeout ( timeout, startup_future) . await ?
113- } ) ?) ;
103+ return Ok ( natsrpy_future_with_timeout (
104+ py,
105+ Some ( timeout) ,
106+ async move {
107+ if session. read ( ) . await . is_some ( ) {
108+ return Err ( NatsrpyError :: SessionError (
109+ "NATS session already exists" . to_string ( ) ,
110+ ) ) ;
111+ }
112+ // Scoping for early-dropping of a guard.
113+ {
114+ let mut sesion_guard = session. write ( ) . await ;
115+ * sesion_guard = Some ( conn_opts. connect ( address) . await ?) ;
116+ }
117+ Ok ( ( ) )
118+ } ,
119+ ) ?) ;
114120 }
115121
116122 #[ pyo3( signature = ( subject, payload, * , headers=None , reply=None , err_on_disconnect = false ) ) ]
0 commit comments