66 AcquireSemaphore ,
77 ReleaseSemaphore ,
88 FromServer ,
9- SessionStart ,
109)
1110from ydb .aio .coordination .stream import CoordinationStream
1211from ydb .aio .coordination .reconnector import CoordinationReconnector
@@ -31,13 +30,15 @@ def __init__(
3130 self ._timeout_millis : int = timeout_millis
3231 self ._next_req_id : int = 1
3332
34- self ._closed : asyncio .Event = asyncio .Event ()
3533 self ._request_queue : asyncio .Queue = asyncio .Queue ()
3634 self ._stream : Optional [CoordinationStream ] = None
37- self ._reader_task : Optional [asyncio .Task ] = None
38- self .session_id : Optional [int ] = None
39- self ._session_ready : asyncio .Event = asyncio .Event ()
40- self ._reconnector = CoordinationReconnector (self )
35+
36+ self ._reconnector = CoordinationReconnector (
37+ driver = self ._driver ,
38+ request_queue = self ._request_queue ,
39+ node_path = self ._node_path ,
40+ timeout_millis = self ._timeout_millis ,
41+ )
4142
4243 self ._wait_timeout : float = self ._timeout_millis / 1000.0
4344
@@ -51,33 +52,17 @@ async def send(self, req):
5152 raise issues .Error ("Stream is not started yet" )
5253 await self ._stream .send (req )
5354
54- async def _start_session (self ):
55- if self .session_id is not None :
55+ async def _ensure_session (self ):
56+ if self ._stream is not None and self . _stream . session_id is not None :
5657 return
5758
5859 if not self ._node_path :
5960 raise issues .Error ("node_path is not set for CoordinationLock" )
6061
61- await self ._request_queue .put (
62- SessionStart (
63- path = self ._node_path ,
64- session_id = 0 ,
65- timeout_millis = self ._timeout_millis ,
66- ).to_proto ()
67- )
68-
6962 self ._reconnector .start ()
70- await self ._session_ready . wait ()
63+ await self ._reconnector . wait_ready ()
7164
72- async def _stop_session (self ):
73- self ._closed .set ()
74- if self ._stream :
75- await self ._stream .close ()
76- self ._stream = None
77-
78- await self ._reconnector .stop ()
79- self .session_id = None
80- self ._node_path = None
65+ self ._stream = self ._reconnector .get_stream ()
8166
8267 async def _wait_for_acquire_response (self ):
8368 try :
@@ -95,7 +80,7 @@ async def _wait_for_acquire_response(self):
9580 )
9681
9782 async def __aenter__ (self ):
98- await self ._start_session ()
83+ await self ._ensure_session ()
9984
10085 self ._req_id = self .next_req_id ()
10186
@@ -123,7 +108,9 @@ async def __aexit__(self, exc_type, exc, tb):
123108 except issues .Error :
124109 pass
125110
126- await self ._stop_session ()
111+ await self ._reconnector .stop ()
112+ self ._stream = None
113+ self ._node_path = None
127114
128115 async def acquire (self ):
129116 return await self .__aenter__ ()
0 commit comments