|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +import abc |
| 17 | + |
| 18 | +from dubbo.common import URL |
| 19 | +from dubbo.protocol import AsyncInvoker, Invocation, Invoker |
| 20 | + |
| 21 | + |
| 22 | +class Router(abc.ABC): |
| 23 | + """Router interface for service invocation""" |
| 24 | + |
| 25 | + @property |
| 26 | + @abc.abstractmethod |
| 27 | + def priority(self) -> int: |
| 28 | + """Get the priority of the router.""" |
| 29 | + raise NotImplementedError() |
| 30 | + |
| 31 | + @abc.abstractmethod |
| 32 | + def route(self, invokers: list[Invoker], url: URL, invocation: Invocation) -> list[Invoker]: |
| 33 | + """Route the invokers based on the URL and invocation.""" |
| 34 | + raise NotImplementedError() |
| 35 | + |
| 36 | + @abc.abstractmethod |
| 37 | + def get_url(self) -> URL: |
| 38 | + """Get the URL associated with this router.""" |
| 39 | + raise NotImplementedError() |
| 40 | + |
| 41 | + @abc.abstractmethod |
| 42 | + def notify(self, invokers: list[Invoker]) -> None: |
| 43 | + """Notify the router of changes in the invoker list.""" |
| 44 | + raise NotImplementedError() |
| 45 | + |
| 46 | + |
| 47 | +class RouterFactory(abc.ABC): |
| 48 | + """RouterFactory""" |
| 49 | + |
| 50 | + @abc.abstractmethod |
| 51 | + def get_router(self, url: URL) -> Router: |
| 52 | + """Get the router.""" |
| 53 | + raise NotImplementedError() |
| 54 | + |
| 55 | + |
| 56 | +class AsyncRouter(abc.ABC): |
| 57 | + """Asynchronous Router interface for service invocation""" |
| 58 | + |
| 59 | + @property |
| 60 | + @abc.abstractmethod |
| 61 | + def priority(self) -> int: |
| 62 | + """Get the priority of the router.""" |
| 63 | + raise NotImplementedError() |
| 64 | + |
| 65 | + @abc.abstractmethod |
| 66 | + async def route(self, invokers: list[AsyncInvoker], url: URL, invocation: Invocation) -> list[AsyncInvoker]: |
| 67 | + """Asynchronously route the invokers based on the URL and invocation.""" |
| 68 | + raise NotImplementedError() |
| 69 | + |
| 70 | + @abc.abstractmethod |
| 71 | + def get_url(self) -> URL: |
| 72 | + """Get the URL associated with this router.""" |
| 73 | + raise NotImplementedError() |
| 74 | + |
| 75 | + @abc.abstractmethod |
| 76 | + async def notify(self, invokers: list[AsyncInvoker]) -> None: |
| 77 | + """Notify the router of changes in the invoker list.""" |
| 78 | + raise NotImplementedError() |
| 79 | + |
| 80 | + |
| 81 | +class AsyncRouterFactory(abc.ABC): |
| 82 | + """AsyncRouterFactory""" |
| 83 | + |
| 84 | + @abc.abstractmethod |
| 85 | + def get_router(self, url: URL) -> AsyncRouter: |
| 86 | + """Get the router.""" |
| 87 | + raise NotImplementedError() |
0 commit comments