Mercurial > code > home > repos > collector
comparison patchsink.py @ 12:032e59be8fe9
refactor to separate the nonweb stuff a bit, in prep for cyclone->starlette
author | drewp@bigasterisk.com |
---|---|
date | Fri, 25 Nov 2022 20:58:08 -0800 |
parents | |
children | bfd95926be6e |
comparison
equal
deleted
inserted
replaced
11:baf886e01ed1 | 12:032e59be8fe9 |
---|---|
1 import time | |
2 from typing import Dict | |
3 | |
4 import cyclone.sse | |
5 import cyclone.web | |
6 | |
7 class PatchSink(cyclone.sse.SSEHandler): | |
8 _handlerSerial = 0 | |
9 | |
10 def __init__(self, application: cyclone.web.Application, request): | |
11 cyclone.sse.SSEHandler.__init__(self, application, request) | |
12 self.bound = False | |
13 self.created = time.time() | |
14 self.graphClients = self.settings.graphClients | |
15 | |
16 self._serial = PatchSink._handlerSerial | |
17 PatchSink._handlerSerial += 1 | |
18 self.lastPatchSentTime: float = 0.0 | |
19 | |
20 def __repr__(self) -> str: | |
21 return '<Handler #%s>' % self._serial | |
22 | |
23 def state(self) -> Dict: | |
24 return { | |
25 'created': round(self.created, 2), | |
26 'ageHours': round((time.time() - self.created) / 3600, 2), | |
27 'streamId': self.streamId, | |
28 'remoteIp': self.request.remote_ip, # wrong, need some forwarded-for thing | |
29 'foafAgent': self.request.headers.get('X-Foaf-Agent'), | |
30 'userAgent': self.request.headers.get('user-agent'), | |
31 } | |
32 | |
33 def bind(self, *args, **kwargs): | |
34 self.streamId = args[0] | |
35 | |
36 self.graphClients.addSseHandler(self) | |
37 # If something goes wrong with addSseHandler, I don't want to | |
38 # try removeSseHandler. | |
39 self.bound = True | |
40 | |
41 def unbind(self) -> None: | |
42 if self.bound: | |
43 self.graphClients.removeSseHandler(self) |