@@ -16,7 +16,7 @@ class Client:
16
16
"_port" ,
17
17
"_namespace" ,
18
18
"_constant_tags" ,
19
- "_closing " ,
19
+ "_state " ,
20
20
"_protocol" ,
21
21
"_queue" ,
22
22
"_listen_future" ,
@@ -26,6 +26,18 @@ class Client:
26
26
"_sample_rate" ,
27
27
)
28
28
29
+ @property
30
+ def connected (self ) -> bool :
31
+ return self ._state == typedefs .CState .CONNECTED
32
+
33
+ @property
34
+ def closing (self ) -> bool :
35
+ return self ._state == typedefs .CState .CLOSING
36
+
37
+ @property
38
+ def disconnected (self ) -> bool :
39
+ return self ._state == typedefs .CState .DISCONNECTED
40
+
29
41
def __init__ (
30
42
self ,
31
43
* ,
@@ -52,7 +64,7 @@ def __init__(
52
64
self ._namespace = namespace
53
65
self ._constant_tags = constant_tags or {}
54
66
55
- self ._closing = False
67
+ self ._state = typedefs . CState . DISCONNECTED
56
68
57
69
self ._protocol = DatagramProtocol ()
58
70
@@ -81,14 +93,18 @@ async def connect(self) -> None:
81
93
self ._listen_future = asyncio .ensure_future (self ._listen ())
82
94
self ._listen_future_join = asyncio .Future ()
83
95
96
+ self ._state = typedefs .CState .CONNECTED
97
+
84
98
async def close (self ) -> None :
85
- self ._closing = True
99
+ self ._state = typedefs . CState . CLOSING
86
100
87
101
try :
88
102
await asyncio .wait_for (self ._close (), timeout = self ._close_timeout )
89
103
except asyncio .TimeoutError :
90
104
pass
91
105
106
+ self ._state = typedefs .CState .DISCONNECTED
107
+
92
108
async def _close (self ) -> None :
93
109
await self ._listen_future_join
94
110
self ._listen_future .cancel ()
@@ -176,7 +192,7 @@ def timing(
176
192
177
193
async def _listen (self ) -> None :
178
194
try :
179
- while not self ._closing :
195
+ while self .connected :
180
196
await self ._listen_and_send ()
181
197
finally :
182
198
# Note that `asyncio.CancelledError` raised on app clean up
@@ -203,8 +219,8 @@ def _report(
203
219
tags : Optional [typedefs .MTags ] = None ,
204
220
sample_rate : Optional [typedefs .MSampleRate ] = None ,
205
221
) -> None :
206
- # If client in closing state, then ignore any new incoming metric
207
- if self ._closing :
222
+ # Ignore any new incoming metric if client in closing or disconnected state
223
+ if self .closing or self . disconnected :
208
224
return
209
225
210
226
sample_rate = sample_rate or self ._sample_rate
0 commit comments