-
Notifications
You must be signed in to change notification settings - Fork 57
/
interceptor.py
184 lines (148 loc) · 6.68 KB
/
interceptor.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from __future__ import annotations
from contextlib import contextmanager
from typing import Any, Mapping, Protocol, Type
import temporalio.activity
import temporalio.api.common.v1
import temporalio.client
import temporalio.converter
import temporalio.worker
import temporalio.workflow
with temporalio.workflow.unsafe.imports_passed_through():
from context_propagation.shared import HEADER_KEY, user_id
class _InputWithHeaders(Protocol):
headers: Mapping[str, temporalio.api.common.v1.Payload]
def set_header_from_context(
input: _InputWithHeaders, payload_converter: temporalio.converter.PayloadConverter
) -> None:
user_id_val = user_id.get()
if user_id_val:
input.headers = {
**input.headers,
HEADER_KEY: payload_converter.to_payload(user_id_val),
}
@contextmanager
def context_from_header(
input: _InputWithHeaders, payload_converter: temporalio.converter.PayloadConverter
):
payload = input.headers.get(HEADER_KEY)
token = (
user_id.set(payload_converter.from_payload(payload, str)) if payload else None
)
try:
yield
finally:
if token:
user_id.reset(token)
class ContextPropagationInterceptor(
temporalio.client.Interceptor, temporalio.worker.Interceptor
):
"""Interceptor that can serialize/deserialize contexts."""
def __init__(
self,
payload_converter: temporalio.converter.PayloadConverter = temporalio.converter.default().payload_converter,
) -> None:
self._payload_converter = payload_converter
def intercept_client(
self, next: temporalio.client.OutboundInterceptor
) -> temporalio.client.OutboundInterceptor:
return _ContextPropagationClientOutboundInterceptor(
next, self._payload_converter
)
def intercept_activity(
self, next: temporalio.worker.ActivityInboundInterceptor
) -> temporalio.worker.ActivityInboundInterceptor:
return _ContextPropagationActivityInboundInterceptor(next)
def workflow_interceptor_class(
self, input: temporalio.worker.WorkflowInterceptorClassInput
) -> Type[_ContextPropagationWorkflowInboundInterceptor]:
return _ContextPropagationWorkflowInboundInterceptor
class _ContextPropagationClientOutboundInterceptor(
temporalio.client.OutboundInterceptor
):
def __init__(
self,
next: temporalio.client.OutboundInterceptor,
payload_converter: temporalio.converter.PayloadConverter,
) -> None:
super().__init__(next)
self._payload_converter = payload_converter
async def start_workflow(
self, input: temporalio.client.StartWorkflowInput
) -> temporalio.client.WorkflowHandle[Any, Any]:
set_header_from_context(input, self._payload_converter)
return await super().start_workflow(input)
async def query_workflow(self, input: temporalio.client.QueryWorkflowInput) -> Any:
set_header_from_context(input, self._payload_converter)
return await super().query_workflow(input)
async def signal_workflow(
self, input: temporalio.client.SignalWorkflowInput
) -> None:
set_header_from_context(input, self._payload_converter)
await super().signal_workflow(input)
async def start_workflow_update(
self, input: temporalio.client.StartWorkflowUpdateInput
) -> temporalio.client.WorkflowUpdateHandle[Any]:
set_header_from_context(input, self._payload_converter)
return await self.next.start_workflow_update(input)
class _ContextPropagationActivityInboundInterceptor(
temporalio.worker.ActivityInboundInterceptor
):
async def execute_activity(
self, input: temporalio.worker.ExecuteActivityInput
) -> Any:
with context_from_header(input, temporalio.activity.payload_converter()):
return await self.next.execute_activity(input)
class _ContextPropagationWorkflowInboundInterceptor(
temporalio.worker.WorkflowInboundInterceptor
):
def init(self, outbound: temporalio.worker.WorkflowOutboundInterceptor) -> None:
self.next.init(_ContextPropagationWorkflowOutboundInterceptor(outbound))
async def execute_workflow(
self, input: temporalio.worker.ExecuteWorkflowInput
) -> Any:
with context_from_header(input, temporalio.workflow.payload_converter()):
return await self.next.execute_workflow(input)
async def handle_signal(self, input: temporalio.worker.HandleSignalInput) -> None:
with context_from_header(input, temporalio.workflow.payload_converter()):
return await self.next.handle_signal(input)
async def handle_query(self, input: temporalio.worker.HandleQueryInput) -> Any:
with context_from_header(input, temporalio.workflow.payload_converter()):
return await self.next.handle_query(input)
def handle_update_validator(
self, input: temporalio.worker.HandleUpdateInput
) -> None:
with context_from_header(input, temporalio.workflow.payload_converter()):
self.next.handle_update_validator(input)
async def handle_update_handler(
self, input: temporalio.worker.HandleUpdateInput
) -> Any:
with context_from_header(input, temporalio.workflow.payload_converter()):
return await self.next.handle_update_handler(input)
class _ContextPropagationWorkflowOutboundInterceptor(
temporalio.worker.WorkflowOutboundInterceptor
):
async def signal_child_workflow(
self, input: temporalio.worker.SignalChildWorkflowInput
) -> None:
set_header_from_context(input, temporalio.workflow.payload_converter())
return await self.next.signal_child_workflow(input)
async def signal_external_workflow(
self, input: temporalio.worker.SignalExternalWorkflowInput
) -> None:
set_header_from_context(input, temporalio.workflow.payload_converter())
return await self.next.signal_external_workflow(input)
def start_activity(
self, input: temporalio.worker.StartActivityInput
) -> temporalio.workflow.ActivityHandle:
set_header_from_context(input, temporalio.workflow.payload_converter())
return self.next.start_activity(input)
async def start_child_workflow(
self, input: temporalio.worker.StartChildWorkflowInput
) -> temporalio.workflow.ChildWorkflowHandle:
set_header_from_context(input, temporalio.workflow.payload_converter())
return await self.next.start_child_workflow(input)
def start_local_activity(
self, input: temporalio.worker.StartLocalActivityInput
) -> temporalio.workflow.ActivityHandle:
set_header_from_context(input, temporalio.workflow.payload_converter())
return self.next.start_local_activity(input)