Skip to content

Commit 1c53c72

Browse files
committed
[feat] connection state (#23)
1 parent c0c2d56 commit 1c53c72

File tree

7 files changed

+39
-12
lines changed

7 files changed

+39
-12
lines changed

.github/workflows/default.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
python-version: ${{ matrix.python-version }}
2020
- uses: Gr1N/setup-poetry@v1
2121
with:
22-
poetry-version: 1.0.0
22+
poetry-version: 1.0.5
2323
- uses: actions/cache@v1
2424
with:
2525
path: ~/.cache/pypoetry/virtualenvs

.github/workflows/release-created.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
python-version: 3.8
1616
- uses: Gr1N/setup-poetry@v1
1717
with:
18-
poetry-version: 1.0.0
18+
poetry-version: 1.0.5
1919
- uses: actions/cache@v1
2020
with:
2121
path: ~/.cache/pypoetry/virtualenvs
@@ -40,7 +40,7 @@ jobs:
4040
python-version: 3.8
4141
- uses: Gr1N/setup-poetry@v1
4242
with:
43-
poetry-version: 1.0.0
43+
poetry-version: 1.0.5
4444
- uses: actions/cache@v1
4545
with:
4646
path: ~/.cache/pypoetry/virtualenvs

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## 0.12.0 (20XX-XX-XX)
44

5+
- Added `connected`, `closing` and `disconnected` client properties. Can be used to check connection state of client, #23
6+
- Bumped minimum required `Sanic` version, #23
7+
58
## 0.11.0 (2020-02-21)
69

710
- Updated documentation: described why 9125 port used by default, #16

aiodogstatsd/client.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Client:
1616
"_port",
1717
"_namespace",
1818
"_constant_tags",
19-
"_closing",
19+
"_state",
2020
"_protocol",
2121
"_queue",
2222
"_listen_future",
@@ -26,6 +26,18 @@ class Client:
2626
"_sample_rate",
2727
)
2828

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+
2941
def __init__(
3042
self,
3143
*,
@@ -52,7 +64,7 @@ def __init__(
5264
self._namespace = namespace
5365
self._constant_tags = constant_tags or {}
5466

55-
self._closing = False
67+
self._state = typedefs.CState.DISCONNECTED
5668

5769
self._protocol = DatagramProtocol()
5870

@@ -81,14 +93,18 @@ async def connect(self) -> None:
8193
self._listen_future = asyncio.ensure_future(self._listen())
8294
self._listen_future_join = asyncio.Future()
8395

96+
self._state = typedefs.CState.CONNECTED
97+
8498
async def close(self) -> None:
85-
self._closing = True
99+
self._state = typedefs.CState.CLOSING
86100

87101
try:
88102
await asyncio.wait_for(self._close(), timeout=self._close_timeout)
89103
except asyncio.TimeoutError:
90104
pass
91105

106+
self._state = typedefs.CState.DISCONNECTED
107+
92108
async def _close(self) -> None:
93109
await self._listen_future_join
94110
self._listen_future.cancel()
@@ -176,7 +192,7 @@ def timing(
176192

177193
async def _listen(self) -> None:
178194
try:
179-
while not self._closing:
195+
while self.connected:
180196
await self._listen_and_send()
181197
finally:
182198
# Note that `asyncio.CancelledError` raised on app clean up
@@ -203,8 +219,8 @@ def _report(
203219
tags: Optional[typedefs.MTags] = None,
204220
sample_rate: Optional[typedefs.MSampleRate] = None,
205221
) -> 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:
208224
return
209225

210226
sample_rate = sample_rate or self._sample_rate

aiodogstatsd/contrib/sanic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ def middlewares_factory(
6666
collect_not_found: bool = False,
6767
) -> Tuple[MiddlewareCallable, MiddlewareCallable]:
6868
async def middleware_request(request: Request) -> None:
69-
request["_statsd_request_started_at"] = get_event_loop().time()
69+
request.ctx._statsd_request_started_at = get_event_loop().time()
7070

7171
async def middleware_response(request: Request, response: HTTPResponse) -> None:
7272
if _proceed_collecting(
7373
request, response, collect_not_allowed, collect_not_found
7474
):
7575
request_duration = (
76-
get_event_loop().time() - request["_statsd_request_started_at"]
76+
get_event_loop().time() - request.ctx._statsd_request_started_at
7777
) * 1000
7878
getattr(request.app, client_app_key).timing(
7979
request_duration_metric_name,

aiodogstatsd/typedefs.py

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Mapping, Union
33

44
__all__ = (
5+
"CState",
56
"MName",
67
"MNamespace",
78
"MType",
@@ -30,3 +31,10 @@ class MType(enum.Enum):
3031
GAUGE = "g"
3132
HISTOGRAM = "h"
3233
TIMING = "ms"
34+
35+
36+
@enum.unique
37+
class CState(enum.IntEnum):
38+
CONNECTED = enum.auto()
39+
CLOSING = enum.auto()
40+
DISCONNECTED = enum.auto()

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ classifiers = [
7777
python = "^3.6"
7878

7979
aiohttp = { version = ">=3.0.0", optional = true }
80-
sanic = { version = ">=19.3.0", optional = true }
80+
sanic = { version = ">=20.3.0", optional = true }
8181
starlette = { version = ">=0.13.0", optional = true }
8282

8383
[tool.poetry.dev-dependencies]

0 commit comments

Comments
 (0)