forked from apache/dubbo-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
103 lines (86 loc) · 3.83 KB
/
protocol.py
File metadata and controls
103 lines (86 loc) · 3.83 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
#
# 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 functools
import uuid
from collections.abc import Iterable
from concurrent.futures import ThreadPoolExecutor
from typing import Optional
from dubbo.constants import common_constants
from dubbo.extension import extensionLoader
from dubbo.loggers import loggerFactory
from dubbo.protocol import Invoker, Protocol
from dubbo.protocol.triple.invoker import TripleInvoker
from dubbo.protocol.triple.stream.server_stream import ServerTransportListener
from dubbo.proxy.handlers import RpcServiceHandler
from dubbo.remoting import Server, Transporter
from dubbo.remoting.aio import constants as aio_constants
from dubbo.remoting.aio.http2.protocol import Http2ClientProtocol, Http2ServerProtocol
from dubbo.remoting.aio.http2.stream_handler import (
StreamClientMultiplexHandler,
StreamServerMultiplexHandler,
)
from dubbo.url import URL
_LOGGER = loggerFactory.get_logger()
class TripleProtocol(Protocol):
"""
Triple protocol.
"""
__slots__ = ["_url", "_transporter", "_invokers"]
def __init__(self):
self._transporter: Transporter = extensionLoader.get_extension(
Transporter, common_constants.TRANSPORTER_DEFAULT_VALUE
)()
self._invokers = []
self._server: Optional[Server] = None
self._path_resolver: dict[str, RpcServiceHandler] = {}
def export(self, url: URL):
"""
Export a service.
"""
if self._server is not None:
return
service_handler = url.attributes[common_constants.SERVICE_HANDLER_KEY]
if isinstance(service_handler, Iterable):
for handler in service_handler:
self._path_resolver[handler.service_name] = handler
else:
self._path_resolver[service_handler.service_name] = service_handler
method_executor = ThreadPoolExecutor(thread_name_prefix=f"dubbo_tri_method_{str(uuid.uuid4())}", max_workers=10)
listener_factory = functools.partial(ServerTransportListener, self._path_resolver, method_executor)
# Create a stream handler
stream_multiplexer = StreamServerMultiplexHandler(listener_factory)
# set stream handler and protocol
url.attributes[aio_constants.LISTENER_FACTORY_KEY] = listener_factory
url.attributes[aio_constants.STREAM_HANDLER_KEY] = stream_multiplexer
url.attributes[common_constants.PROTOCOL_KEY] = Http2ServerProtocol
# Create a server
self._server = self._transporter.bind(url)
def refer(self, url: URL) -> Invoker:
"""
Refer a remote service.
:param url: The URL.
:type url: URL
"""
# Create a stream handler
stream_multiplexer = StreamClientMultiplexHandler()
# set stream handler and protocol
url.attributes[aio_constants.STREAM_HANDLER_KEY] = stream_multiplexer
url.attributes[common_constants.PROTOCOL_KEY] = Http2ClientProtocol
# Create a client
client = self._transporter.connect(url)
invoker = TripleInvoker(url, client, stream_multiplexer)
self._invokers.append(invoker)
return invoker