-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathclient.py
More file actions
277 lines (243 loc) · 10.4 KB
/
client.py
File metadata and controls
277 lines (243 loc) · 10.4 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import threading
from typing import Optional, Callable, List, Type, Union, Any
from dubbo.bootstrap import Dubbo
from dubbo.classes import MethodDescriptor
from dubbo.configs import ReferenceConfig
from dubbo.constants import common_constants
from dubbo.extension import extensionLoader
from dubbo.protocol import Invoker, Protocol
from dubbo.proxy import RpcCallable, RpcCallableFactory
from dubbo.proxy.callables import DefaultRpcCallableFactory
from dubbo.registry.protocol import RegistryProtocol
from dubbo.types import (
DeserializingFunction,
RpcTypes,
SerializingFunction,
)
from dubbo.url import URL
from dubbo.codec import DubboTransportService
__all__ = ["Client"]
class Client:
def __init__(self, reference: ReferenceConfig, dubbo: Optional[Dubbo] = None):
self._initialized = False
self._global_lock = threading.RLock()
self._dubbo = dubbo or Dubbo()
self._reference = reference
self._url: Optional[URL] = None
self._protocol: Optional[Protocol] = None
self._invoker: Optional[Invoker] = None
self._callable_factory: RpcCallableFactory = DefaultRpcCallableFactory()
# initialize the invoker
self._initialize()
def _initialize(self):
"""
Initialize the invoker.
"""
with self._global_lock:
if self._initialized:
return
# get the protocol
protocol = extensionLoader.get_extension(Protocol, self._reference.protocol)()
registry_config = self._dubbo.registry_config
self._protocol = RegistryProtocol(registry_config, protocol) if self._dubbo.registry_config else protocol
# build url
reference_url = self._reference.to_url()
if registry_config:
self._url = registry_config.to_url().copy()
self._url.path = reference_url.path
for k, v in reference_url.parameters.items():
self._url.parameters[k] = v
else:
self._url = reference_url
# create invoker
self._invoker = self._protocol.refer(self._url)
self._initialized = True
def _create_rpc_callable(
self,
rpc_type: str,
interface: Optional[Callable] = None,
method_name: Optional[str] = None,
params_types: Optional[List[Type]] = None,
return_type: Optional[Type] = None,
codec: Optional[str] = None,
request_serializer: Optional[SerializingFunction] = None,
response_deserializer: Optional[DeserializingFunction] = None,
default_method_name: str = "rpc_call",
) -> RpcCallable:
"""
Create RPC callable with the specified type.
"""
# Validate
if interface is None and method_name is None:
raise ValueError("Either 'interface' or 'method_name' must be provided")
# Determine the actual method name to call
actual_method_name = method_name or (interface.__name__ if interface else default_method_name)
# Build method descriptor (automatic or manual)
if interface:
method_desc = DubboTransportService.create_method_descriptor(
func=interface,
method_name=actual_method_name,
parameter_types=params_types,
return_type=return_type,
interface=interface,
)
else:
# Manual mode fallback: use dummy function for descriptor creation
def dummy(): pass
method_desc = DubboTransportService.create_method_descriptor(
func=dummy,
method_name=actual_method_name,
parameter_types=params_types or [],
return_type=return_type or Any,
)
# Determine serializers if not provided
if request_serializer and response_deserializer:
final_request_serializer = request_serializer
final_response_deserializer = response_deserializer
else:
# Use DubboTransportService to generate serialization functions
final_request_serializer, final_response_deserializer = DubboTransportService.create_serialization_functions(
transport_type=codec or "json",
parameter_types=[p.annotation for p in method_desc.parameters],
return_type=method_desc.return_parameter.annotation,
)
# Create the proper MethodDescriptor for the RPC call
rpc_method_descriptor = MethodDescriptor(
method_name=actual_method_name,
arg_serialization=(final_request_serializer, None), # (serializer, deserializer) for arguments
return_serialization=(None, final_response_deserializer), # (serializer, deserializer) for return value
rpc_type=rpc_type,
)
# Create and return the RpcCallable
return self._callable(rpc_method_descriptor)
def unary(
self,
interface: Optional[Callable] = None,
method_name: Optional[str] = None,
params_types: Optional[List[Type]] = None,
return_type: Optional[Type] = None,
codec: Optional[str] = None,
request_serializer: Optional[SerializingFunction] = None,
response_deserializer: Optional[DeserializingFunction] = None,
) -> RpcCallable:
"""
Create unary RPC call.
Supports both automatic mode (via interface) and manual mode (via method_name + params_types + return_type + codec).
"""
return self._create_rpc_callable(
rpc_type=RpcTypes.UNARY.value,
interface=interface,
method_name=method_name,
params_types=params_types,
return_type=return_type,
codec=codec,
request_serializer=request_serializer,
response_deserializer=response_deserializer,
default_method_name="unary",
)
def client_stream(
self,
interface: Optional[Callable] = None,
method_name: Optional[str] = None,
params_types: Optional[List[Type]] = None,
return_type: Optional[Type] = None,
codec: Optional[str] = None,
request_serializer: Optional[SerializingFunction] = None,
response_deserializer: Optional[DeserializingFunction] = None,
) -> RpcCallable:
"""
Create client streaming RPC call.
Supports both automatic mode (via interface) and manual mode (via method_name + params_types + return_type + codec).
"""
return self._create_rpc_callable(
rpc_type=RpcTypes.CLIENT_STREAM.value,
interface=interface,
method_name=method_name,
params_types=params_types,
return_type=return_type,
codec=codec,
request_serializer=request_serializer,
response_deserializer=response_deserializer,
default_method_name="client_stream",
)
def server_stream(
self,
interface: Optional[Callable] = None,
method_name: Optional[str] = None,
params_types: Optional[List[Type]] = None,
return_type: Optional[Type] = None,
codec: Optional[str] = None,
request_serializer: Optional[SerializingFunction] = None,
response_deserializer: Optional[DeserializingFunction] = None,
) -> RpcCallable:
"""
Create server streaming RPC call.
Supports both automatic mode (via interface) and manual mode (via method_name + params_types + return_type + codec).
"""
return self._create_rpc_callable(
rpc_type=RpcTypes.SERVER_STREAM.value,
interface=interface,
method_name=method_name,
params_types=params_types,
return_type=return_type,
codec=codec,
request_serializer=request_serializer,
response_deserializer=response_deserializer,
default_method_name="server_stream",
)
def bi_stream(
self,
interface: Optional[Callable] = None,
method_name: Optional[str] = None,
params_types: Optional[List[Type]] = None,
return_type: Optional[Type] = None,
codec: Optional[str] = None,
request_serializer: Optional[SerializingFunction] = None,
response_deserializer: Optional[DeserializingFunction] = None,
) -> RpcCallable:
"""
Create bidirectional streaming RPC call.
Supports both automatic mode (via interface) and manual mode (via method_name + params_types + return_type + codec).
"""
return self._create_rpc_callable(
rpc_type=RpcTypes.BI_STREAM.value,
interface=interface,
method_name=method_name,
params_types=params_types,
return_type=return_type,
codec=codec,
request_serializer=request_serializer,
response_deserializer=response_deserializer,
default_method_name="bi_stream",
)
def _callable(self, method_descriptor: MethodDescriptor) -> RpcCallable:
"""
Generate a proxy for the given method.
:param method_descriptor: The method descriptor.
:return: The proxy.
:rtype: RpcCallable
"""
# get invoker
url = self._invoker.get_url()
# clone url
url = url.copy()
url.parameters[common_constants.METHOD_KEY] = method_descriptor.get_method_name()
# set method descriptor
url.attributes[common_constants.METHOD_DESCRIPTOR_KEY] = method_descriptor
# create proxy
return self._callable_factory.get_callable(self._invoker, url)