@@ -5,17 +5,17 @@ use crate::{
55 js:: counters:: { Counters , CountersConfig } ,
66} ;
77use pyo3:: { Bound , PyAny , Python } ;
8- use tokio:: sync:: RwLock ;
98
109use crate :: { exceptions:: rust_err:: NatsrpyResult , utils:: natsrpy_future} ;
1110
1211#[ pyo3:: pyclass]
1312pub struct CountersManager {
14- ctx : Arc < RwLock < async_nats:: jetstream:: Context > > ,
13+ ctx : Arc < async_nats:: jetstream:: Context > ,
1514}
1615
1716impl CountersManager {
18- pub const fn new ( ctx : Arc < RwLock < async_nats:: jetstream:: Context > > ) -> Self {
17+ #[ must_use]
18+ pub const fn new ( ctx : Arc < async_nats:: jetstream:: Context > ) -> Self {
1919 Self { ctx }
2020 }
2121}
@@ -27,13 +27,13 @@ impl CountersManager {
2727 py : Python < ' py > ,
2828 config : CountersConfig ,
2929 ) -> NatsrpyResult < Bound < ' py , PyAny > > {
30- let ctx = self . ctx . clone ( ) ;
30+ let client = self . ctx . clone ( ) ;
3131 natsrpy_future ( py, async move {
32- let js = ctx. read ( ) . await ;
3332 Ok ( Counters :: new (
34- js. create_stream ( async_nats:: jetstream:: stream:: Config :: try_from ( config) ?)
33+ client
34+ . create_stream ( async_nats:: jetstream:: stream:: Config :: try_from ( config) ?)
3535 . await ?,
36- ctx . clone ( ) ,
36+ client ,
3737 ) )
3838 } )
3939 }
@@ -43,24 +43,22 @@ impl CountersManager {
4343 py : Python < ' py > ,
4444 config : CountersConfig ,
4545 ) -> NatsrpyResult < Bound < ' py , PyAny > > {
46- let ctx = self . ctx . clone ( ) ;
46+ let client = self . ctx . clone ( ) ;
4747 natsrpy_future ( py, async move {
48- let info = ctx
49- . read ( )
50- . await
48+ let info = client
5149 . create_or_update_stream ( async_nats:: jetstream:: stream:: Config :: try_from ( config) ?)
5250 . await ?;
5351 Ok ( Counters :: new (
54- ctx . read ( ) . await . get_stream ( info. config . name ) . await ?,
55- ctx . clone ( ) ,
52+ client . get_stream ( info. config . name ) . await ?,
53+ client ,
5654 ) )
5755 } )
5856 }
5957
6058 pub fn get < ' py > ( & self , py : Python < ' py > , name : String ) -> NatsrpyResult < Bound < ' py , PyAny > > {
61- let ctx = self . ctx . clone ( ) ;
59+ let client = self . ctx . clone ( ) ;
6260 natsrpy_future ( py, async move {
63- let stream = ctx . read ( ) . await . get_stream ( & name) . await ?;
61+ let stream = client . get_stream ( & name) . await ?;
6462 let config = stream. get_info ( ) . await ?. config ;
6563 if !config. allow_direct {
6664 return Err ( NatsrpyError :: SessionError ( format ! (
@@ -72,33 +70,31 @@ impl CountersManager {
7270 "Stream {name} doesn't allow message counters." ,
7371 ) ) ) ;
7472 }
75- Ok ( Counters :: new ( stream, ctx . clone ( ) ) )
73+ Ok ( Counters :: new ( stream, client ) )
7674 } )
7775 }
7876
7977 pub fn delete < ' py > ( & self , py : Python < ' py > , name : String ) -> NatsrpyResult < Bound < ' py , PyAny > > {
80- let ctx = self . ctx . clone ( ) ;
81- natsrpy_future ( py , async move {
82- let js = ctx . read ( ) . await ;
83- Ok ( js . delete_stream ( name) . await ?. success )
84- } )
78+ let client = self . ctx . clone ( ) ;
79+ natsrpy_future (
80+ py ,
81+ async move { Ok ( client . delete_stream ( name) . await ?. success ) } ,
82+ )
8583 }
8684
8785 pub fn update < ' py > (
8886 & self ,
8987 py : Python < ' py > ,
9088 config : CountersConfig ,
9189 ) -> NatsrpyResult < Bound < ' py , PyAny > > {
92- let ctx = self . ctx . clone ( ) ;
90+ let client = self . ctx . clone ( ) ;
9391 natsrpy_future ( py, async move {
94- let info = ctx
95- . read ( )
96- . await
92+ let info = client
9793 . update_stream ( async_nats:: jetstream:: stream:: Config :: try_from ( config) ?)
9894 . await ?;
9995 Ok ( Counters :: new (
100- ctx . read ( ) . await . get_stream ( info. config . name ) . await ?,
101- ctx . clone ( ) ,
96+ client . get_stream ( info. config . name ) . await ?,
97+ client ,
10298 ) )
10399 } )
104100 }
0 commit comments