-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathevents.py
81 lines (57 loc) · 1.93 KB
/
events.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import asyncio
from dataclasses import dataclass
from typing import Awaitable, Union
from ..issues import ClientInternalError
__all__ = [
"OnCommit",
"OnPartitionGetStartOffsetRequest",
"OnPartitionGetStartOffsetResponse",
"OnInitPartition",
"OnShutdownPartition",
"EventHandler",
]
class BaseReaderEvent:
pass
@dataclass
class OnCommit(BaseReaderEvent):
topic: str
offset: int
@dataclass
class OnPartitionGetStartOffsetRequest(BaseReaderEvent):
topic: str
partition_id: int
@dataclass
class OnPartitionGetStartOffsetResponse:
start_offset: int
class OnInitPartition(BaseReaderEvent):
pass
class OnShutdownPartition:
pass
TopicEventDispatchType = Union[OnPartitionGetStartOffsetResponse, None]
class EventHandler:
def on_commit(self, event: OnCommit) -> Union[None, Awaitable[None]]:
pass
def on_partition_get_start_offset(
self,
event: OnPartitionGetStartOffsetRequest,
) -> Union[OnPartitionGetStartOffsetResponse, Awaitable[OnPartitionGetStartOffsetResponse]]:
pass
def on_init_partition(self, event: OnInitPartition) -> Union[None, Awaitable[None]]:
pass
def on_shutdown_partition(self, event: OnShutdownPartition) -> Union[None, Awaitable[None]]:
pass
async def _dispatch(self, event: BaseReaderEvent) -> Awaitable[TopicEventDispatchType]:
f = None
if isinstance(event, OnCommit):
f = self.on_commit
elif isinstance(event, OnPartitionGetStartOffsetRequest):
f = self.on_partition_get_start_offset
elif isinstance(event, OnInitPartition):
f = self.on_init_partition
elif isinstance(event, OnShutdownPartition):
f = self.on_shutdown_partition
else:
raise ClientInternalError("Unsupported topic reader event")
if asyncio.iscoroutinefunction(f):
return await f(event)
return f(event)