grpc-router 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ # Credits
2
+
3
+ ## Development Lead
4
+
5
+ * Ciprian Miclaus <https://github.com/ciprianmiclaus2>
6
+
7
+ ## Contributors
8
+
9
+ None yet. Why not be the first?
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ciprian Miclaus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,85 @@
1
+ Metadata-Version: 2.1
2
+ Name: grpc_router
3
+ Version: 0.1.0
4
+ Summary: A simple grpc router to establish a simple grpc mesh.
5
+ Author-email: Ciprian Miclaus <ciprianm@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Ciprian Miclaus
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: homepage, https://github.com/ciprianmiclaus2/grpc_router
29
+ Project-URL: documentation, https://ciprianmiclaus2.github.io/grpc_router/
30
+ Project-URL: repository, https://github.com/ciprianmiclaus2/grpc_router
31
+ Project-URL: history, https://github.com/ciprianmiclaus2/grpc_router/blob/main/HISTORY.md
32
+ Classifier: Development Status :: 4 - Beta
33
+ Classifier: Intended Audience :: Developers
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Natural Language :: English
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Programming Language :: Python :: 3.9
38
+ Classifier: Programming Language :: Python :: 3.10
39
+ Classifier: Programming Language :: Python :: 3.11
40
+ Classifier: Programming Language :: Python :: 3.12
41
+ Requires-Python: >=3.9
42
+ Description-Content-Type: text/markdown
43
+ License-File: LICENSE
44
+ License-File: AUTHORS.md
45
+ Requires-Dist: grpcio
46
+ Requires-Dist: protobuf
47
+ Provides-Extra: test
48
+ Requires-Dist: pytest; extra == "test"
49
+ Provides-Extra: dev
50
+ Requires-Dist: grpcio-tools; extra == "dev"
51
+ Requires-Dist: tox; extra == "dev"
52
+ Requires-Dist: pre-commit; extra == "dev"
53
+ Requires-Dist: pip; extra == "dev"
54
+ Requires-Dist: twine; extra == "dev"
55
+ Requires-Dist: toml; extra == "dev"
56
+
57
+ # GRPC Router
58
+
59
+ <p align="center">
60
+ <a href="https://pypi.python.org/pypi/grpc_router">
61
+ <img src="https://img.shields.io/pypi/v/grpc_router.svg"
62
+ alt = "Release Status">
63
+ </a>
64
+
65
+ <a href="http://github.com/ciprianmiclaus2/grpc_router/">
66
+
67
+ <img src="https://img.shields.io/pypi/pyversions/grpc_router.svg" alt="Python versions">
68
+ </a>
69
+
70
+ <a href="https://github.com/ciprianmiclaus2/grpc_router/actions">
71
+ <img src="https://github.com/ciprianmiclaus2/grpc_router/actions/workflows/python-package.yml/badge.svg?branch=main" alt="CI Status">
72
+ </a>
73
+
74
+ <a href="https://codecov.io/gh/ciprianmiclaus2/grpc_router" >
75
+ <img src="https://codecov.io/gh/ciprianmiclaus2/grpc_router/graph/badge.svg?token=4N0N8XSVZY"/>
76
+ </a>
77
+
78
+ <a href="https://ciprianmiclaus2.github.io/grpc_router/">
79
+ <img src="https://img.shields.io/website/https/ciprianmiclaus2.github.io/grpc_router/index.html.svg?label=docs&down_message=unavailable&up_message=available" alt="Documentation Status">
80
+ </a>
81
+
82
+ </p>
83
+
84
+ GRPC Router is a small components that implements a simple service register to be used for client side service discovery.
85
+ Services register with the GRPC router, clients requests the details of such services in order to discover them and connect directly.
@@ -0,0 +1,29 @@
1
+ # GRPC Router
2
+
3
+ <p align="center">
4
+ <a href="https://pypi.python.org/pypi/grpc_router">
5
+ <img src="https://img.shields.io/pypi/v/grpc_router.svg"
6
+ alt = "Release Status">
7
+ </a>
8
+
9
+ <a href="http://github.com/ciprianmiclaus2/grpc_router/">
10
+
11
+ <img src="https://img.shields.io/pypi/pyversions/grpc_router.svg" alt="Python versions">
12
+ </a>
13
+
14
+ <a href="https://github.com/ciprianmiclaus2/grpc_router/actions">
15
+ <img src="https://github.com/ciprianmiclaus2/grpc_router/actions/workflows/python-package.yml/badge.svg?branch=main" alt="CI Status">
16
+ </a>
17
+
18
+ <a href="https://codecov.io/gh/ciprianmiclaus2/grpc_router" >
19
+ <img src="https://codecov.io/gh/ciprianmiclaus2/grpc_router/graph/badge.svg?token=4N0N8XSVZY"/>
20
+ </a>
21
+
22
+ <a href="https://ciprianmiclaus2.github.io/grpc_router/">
23
+ <img src="https://img.shields.io/website/https/ciprianmiclaus2.github.io/grpc_router/index.html.svg?label=docs&down_message=unavailable&up_message=available" alt="Documentation Status">
24
+ </a>
25
+
26
+ </p>
27
+
28
+ GRPC Router is a small components that implements a simple service register to be used for client side service discovery.
29
+ Services register with the GRPC router, clients requests the details of such services in order to discover them and connect directly.
File without changes
@@ -0,0 +1,139 @@
1
+ import argparse
2
+ from concurrent.futures import ThreadPoolExecutor
3
+ import grpc
4
+
5
+ from grpc_router.stubs.grpc_router_service_pb2_grpc import add_GRPCRouterServiceServicer_to_server, GRPCRouterServiceServicer
6
+ from grpc_router.stubs.grpc_router_service_pb2 import (
7
+ ServiceRegistrationResponse,
8
+ ServiceDeregistrationResponse,
9
+ GetRegisteredServiceResponse,
10
+ )
11
+
12
+ from grpc_router.core.models import ConfigOptions
13
+ from grpc_router.core.register import ServiceRegister
14
+
15
+
16
+ class GRPCRouterServer(GRPCRouterServiceServicer):
17
+
18
+ def __init__(self, config: ConfigOptions):
19
+ self._config = config
20
+ self._register = ServiceRegister(config)
21
+
22
+ def _validate_RegisterService(self, request, context):
23
+ service_id = request.service_id
24
+ if not service_id:
25
+ context.abort(
26
+ grpc.StatusCode.INVALID_ARGUMENT,
27
+ "The service_id cannot be empty."
28
+ )
29
+ host = request.endpoint.host
30
+ if not host:
31
+ context.abort(
32
+ grpc.StatusCode.INVALID_ARGUMENT,
33
+ "The host cannot be empty."
34
+ )
35
+
36
+ port = request.endpoint.port
37
+ if port <= 0:
38
+ context.abort(
39
+ grpc.StatusCode.INVALID_ARGUMENT,
40
+ "The port cannot be negative or zero."
41
+ )
42
+ region = request.metadata.region
43
+ if not region and not self._config.allow_global_region:
44
+ context.abort(
45
+ grpc.StatusCode.INVALID_ARGUMENT,
46
+ "The region cannot be empty in this current configuration."
47
+ )
48
+
49
+ def RegisterService(self, request, context) -> ServiceRegistrationResponse:
50
+ self._validate_RegisterService(request, context)
51
+ service_token, error = self._register.register_service(
52
+ service_id=request.service_id,
53
+ host=request.endpoint.host,
54
+ port=request.endpoint.port,
55
+ region=request.metadata.region,
56
+ slots=request.metadata.slots,
57
+ )
58
+ return ServiceRegistrationResponse(
59
+ service_token=service_token
60
+ )
61
+
62
+ def _validate_DeregisterService(self, request, context) -> None:
63
+ service_id = request.service_id
64
+ if not service_id:
65
+ context.abort(
66
+ grpc.StatusCode.INVALID_ARGUMENT,
67
+ "The service_id cannot be empty."
68
+ )
69
+ service_token = request.service_token
70
+ if not service_token:
71
+ context.abort(
72
+ grpc.StatusCode.INVALID_ARGUMENT,
73
+ "The service_context cannot be empty."
74
+ )
75
+
76
+ def DeregisterService(self, request, context) -> ServiceDeregistrationResponse:
77
+ self._validate_DeregisterService(request, context)
78
+ self._register.deregister_service(
79
+ service_id=request.service_id,
80
+ service_token=request.service_token
81
+ )
82
+ return ServiceDeregistrationResponse()
83
+
84
+ def _validate_GetRegisteredService(self, request, context) -> None:
85
+ service_id = request.service_id
86
+ if not service_id:
87
+ context.abort(
88
+ grpc.StatusCode.INVALID_ARGUMENT,
89
+ "The service_id cannot be empty."
90
+ )
91
+
92
+ def GetRegisteredService(self, request, context) -> GetRegisteredServiceResponse:
93
+ self._validate_GetRegisteredService(request, context)
94
+ service = self._register.get_service(
95
+ service_id=request.service_id,
96
+ region=request.hints.region
97
+ )
98
+ if service is None:
99
+ context.abort(
100
+ grpc.StatusCode.NOT_FOUND,
101
+ "The service_id has no registered instances."
102
+ )
103
+ assert service is not None
104
+ response = GetRegisteredServiceResponse()
105
+ response.service_id = service.service_id
106
+ response.endpoint.host = service.host
107
+ response.endpoint.port = service.port
108
+ return response
109
+
110
+
111
+ def serve(config: ConfigOptions) -> None:
112
+ server = grpc.server(ThreadPoolExecutor(max_workers=config.max_workers))
113
+ add_GRPCRouterServiceServicer_to_server(GRPCRouterServer(config), server)
114
+ server.add_insecure_port(f"{config.hostname}:{config.port}")
115
+ server.start()
116
+ server.wait_for_termination()
117
+
118
+
119
+ def main():
120
+ parser = argparse.ArgumentParser()
121
+ parser.add_argument('-h', '--hostname', dest='hostname', default='[::]',
122
+ help='Hostname to bind the service to')
123
+ parser.add_argument('-p', '--port', dest='port', default=50034,
124
+ help='Port to bind this service to')
125
+ parser.add_argument('-w', '--max-workers', dest='max_workers',
126
+ type=int, default=10,
127
+ help='Maximum concurrent workers to handle requests.')
128
+
129
+ args = parser.parse_args()
130
+ config = ConfigOptions(
131
+ hostname=args.hostname,
132
+ port=args.port,
133
+ max_workers=args.max_workers,
134
+ )
135
+ serve(config)
136
+
137
+
138
+ if __name__ == "__main__":
139
+ main()
@@ -0,0 +1,85 @@
1
+ Metadata-Version: 2.1
2
+ Name: grpc_router
3
+ Version: 0.1.0
4
+ Summary: A simple grpc router to establish a simple grpc mesh.
5
+ Author-email: Ciprian Miclaus <ciprianm@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Ciprian Miclaus
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: homepage, https://github.com/ciprianmiclaus2/grpc_router
29
+ Project-URL: documentation, https://ciprianmiclaus2.github.io/grpc_router/
30
+ Project-URL: repository, https://github.com/ciprianmiclaus2/grpc_router
31
+ Project-URL: history, https://github.com/ciprianmiclaus2/grpc_router/blob/main/HISTORY.md
32
+ Classifier: Development Status :: 4 - Beta
33
+ Classifier: Intended Audience :: Developers
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Natural Language :: English
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Programming Language :: Python :: 3.9
38
+ Classifier: Programming Language :: Python :: 3.10
39
+ Classifier: Programming Language :: Python :: 3.11
40
+ Classifier: Programming Language :: Python :: 3.12
41
+ Requires-Python: >=3.9
42
+ Description-Content-Type: text/markdown
43
+ License-File: LICENSE
44
+ License-File: AUTHORS.md
45
+ Requires-Dist: grpcio
46
+ Requires-Dist: protobuf
47
+ Provides-Extra: test
48
+ Requires-Dist: pytest; extra == "test"
49
+ Provides-Extra: dev
50
+ Requires-Dist: grpcio-tools; extra == "dev"
51
+ Requires-Dist: tox; extra == "dev"
52
+ Requires-Dist: pre-commit; extra == "dev"
53
+ Requires-Dist: pip; extra == "dev"
54
+ Requires-Dist: twine; extra == "dev"
55
+ Requires-Dist: toml; extra == "dev"
56
+
57
+ # GRPC Router
58
+
59
+ <p align="center">
60
+ <a href="https://pypi.python.org/pypi/grpc_router">
61
+ <img src="https://img.shields.io/pypi/v/grpc_router.svg"
62
+ alt = "Release Status">
63
+ </a>
64
+
65
+ <a href="http://github.com/ciprianmiclaus2/grpc_router/">
66
+
67
+ <img src="https://img.shields.io/pypi/pyversions/grpc_router.svg" alt="Python versions">
68
+ </a>
69
+
70
+ <a href="https://github.com/ciprianmiclaus2/grpc_router/actions">
71
+ <img src="https://github.com/ciprianmiclaus2/grpc_router/actions/workflows/python-package.yml/badge.svg?branch=main" alt="CI Status">
72
+ </a>
73
+
74
+ <a href="https://codecov.io/gh/ciprianmiclaus2/grpc_router" >
75
+ <img src="https://codecov.io/gh/ciprianmiclaus2/grpc_router/graph/badge.svg?token=4N0N8XSVZY"/>
76
+ </a>
77
+
78
+ <a href="https://ciprianmiclaus2.github.io/grpc_router/">
79
+ <img src="https://img.shields.io/website/https/ciprianmiclaus2.github.io/grpc_router/index.html.svg?label=docs&down_message=unavailable&up_message=available" alt="Documentation Status">
80
+ </a>
81
+
82
+ </p>
83
+
84
+ GRPC Router is a small components that implements a simple service register to be used for client side service discovery.
85
+ Services register with the GRPC router, clients requests the details of such services in order to discover them and connect directly.
@@ -0,0 +1,13 @@
1
+ AUTHORS.md
2
+ LICENSE
3
+ README.md
4
+ pyproject.toml
5
+ grpc_router/__init__.py
6
+ grpc_router/server.py
7
+ grpc_router.egg-info/PKG-INFO
8
+ grpc_router.egg-info/SOURCES.txt
9
+ grpc_router.egg-info/dependency_links.txt
10
+ grpc_router.egg-info/requires.txt
11
+ grpc_router.egg-info/top_level.txt
12
+ tests/test_connectivity.py
13
+ tests/test_regions.py
@@ -0,0 +1,13 @@
1
+ grpcio
2
+ protobuf
3
+
4
+ [dev]
5
+ grpcio-tools
6
+ tox
7
+ pre-commit
8
+ pip
9
+ twine
10
+ toml
11
+
12
+ [test]
13
+ pytest
@@ -0,0 +1 @@
1
+ grpc_router
@@ -0,0 +1,48 @@
1
+ [project]
2
+ name = "grpc_router"
3
+ version = "0.1.0"
4
+ description = "A simple grpc router to establish a simple grpc mesh."
5
+ authors = [{name = "Ciprian Miclaus", email = "ciprianm@gmail.com"}]
6
+ readme = "README.md"
7
+ license = {file = "LICENSE"}
8
+ requires-python = ">=3.9"
9
+ classifiers=[
10
+ 'Development Status :: 4 - Beta',
11
+ 'Intended Audience :: Developers',
12
+ 'License :: OSI Approved :: MIT License',
13
+ 'Natural Language :: English',
14
+ 'Programming Language :: Python :: 3',
15
+ 'Programming Language :: Python :: 3.9',
16
+ 'Programming Language :: Python :: 3.10',
17
+ 'Programming Language :: Python :: 3.11',
18
+ 'Programming Language :: Python :: 3.12',
19
+ ]
20
+
21
+ dependencies = [
22
+ "grpcio",
23
+ "protobuf",
24
+ ]
25
+
26
+
27
+ [project.optional-dependencies]
28
+ test = [
29
+ "pytest",
30
+ ]
31
+
32
+ dev = [
33
+ "grpcio-tools",
34
+ "tox",
35
+ "pre-commit",
36
+ "pip",
37
+ "twine",
38
+ "toml"
39
+ ]
40
+
41
+ [project.urls]
42
+ homepage = "https://github.com/ciprianmiclaus2/grpc_router"
43
+ documentation = "https://ciprianmiclaus2.github.io/grpc_router/"
44
+ repository = "https://github.com/ciprianmiclaus2/grpc_router"
45
+ history = "https://github.com/ciprianmiclaus2/grpc_router/blob/main/HISTORY.md"
46
+
47
+ [tool.setuptools]
48
+ packages = ["grpc_router"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,83 @@
1
+ import grpc
2
+
3
+ from grpc_router.client.client import GRPCRouterClient
4
+ import pytest
5
+
6
+
7
+ def test_connectivity(grpc_router_server):
8
+ service_id = "my.test.service"
9
+ client = GRPCRouterClient("localhost", 7654)
10
+
11
+ with pytest.raises(grpc.RpcError) as exc:
12
+ client.get_service(service_id)
13
+ assert exc.value.code() == grpc.StatusCode.NOT_FOUND
14
+ assert exc.value.details() == "The service_id has no registered instances."
15
+
16
+ token = client.register_service(
17
+ service_id=service_id,
18
+ host="myhost.mydomain.com",
19
+ port=9998
20
+ )
21
+ assert token is not None
22
+
23
+ host, port = client.get_service(service_id)
24
+ assert host == "myhost.mydomain.com"
25
+ assert port == 9998
26
+
27
+ client.deregister_service(
28
+ service_id=service_id,
29
+ service_token=token
30
+ )
31
+
32
+ with pytest.raises(grpc.RpcError) as exc:
33
+ client.get_service(service_id)
34
+ assert exc.value.code() == grpc.StatusCode.NOT_FOUND
35
+ assert exc.value.details() == "The service_id has no registered instances."
36
+
37
+
38
+ def test_multiple_services_round_robin(grpc_router_server):
39
+ service_id = "my.own.test.service"
40
+
41
+ client = GRPCRouterClient("localhost", 7654)
42
+
43
+ token1 = client.register_service(
44
+ service_id=service_id,
45
+ host="myhost1.mydomain.com",
46
+ port=9990
47
+ )
48
+ token2 = client.register_service(
49
+ service_id=service_id,
50
+ host="myhost2.mydomain.com",
51
+ port=9991
52
+ )
53
+ token3 = client.register_service(
54
+ service_id=service_id,
55
+ host="myhost3.mydomain.com",
56
+ port=9992
57
+ )
58
+
59
+ host, port = client.get_service(service_id)
60
+ assert host == "myhost1.mydomain.com"
61
+ assert port == 9990
62
+ host, port = client.get_service(service_id)
63
+ assert host == "myhost2.mydomain.com"
64
+ assert port == 9991
65
+ host, port = client.get_service(service_id)
66
+ assert host == "myhost3.mydomain.com"
67
+ assert port == 9992
68
+ host, port = client.get_service(service_id)
69
+ assert host == "myhost1.mydomain.com"
70
+ assert port == 9990
71
+
72
+ client.deregister_service(
73
+ service_id=service_id,
74
+ service_token=token1
75
+ )
76
+ client.deregister_service(
77
+ service_id=service_id,
78
+ service_token=token2
79
+ )
80
+ client.deregister_service(
81
+ service_id=service_id,
82
+ service_token=token3
83
+ )
@@ -0,0 +1,104 @@
1
+ from grpc_router.client.client import GRPCRouterClient
2
+ import pytest
3
+
4
+
5
+ def register_svc(client: GRPCRouterClient, service_id: str, region: str, instance: int, port: int) -> str:
6
+ token = client.register_service(
7
+ service_id=service_id,
8
+ host=f"{region}{instance}.mydomain.com",
9
+ port=port,
10
+ region=region
11
+ )
12
+ assert token is not None
13
+ return token
14
+
15
+
16
+ @pytest.mark.parametrize('region,expected_host,expected_port', [
17
+ ["USWest", "USWest1.mydomain.com", 9002],
18
+ ["USWest", "USWest2.mydomain.com", 9003],
19
+ ["USWest", "USWest3.mydomain.com", 9004],
20
+ ["USEast", "USEast1.mydomain.com", 9000],
21
+ ["USEast", "USEast2.mydomain.com", 9001],
22
+ ["USWest", "USWest1.mydomain.com", 9002],
23
+ ["USEast", "USEast1.mydomain.com", 9000],
24
+ ["", "1.mydomain.com", 9005],
25
+ ["", "2.mydomain.com", 9006],
26
+ ["", "1.mydomain.com", 9005],
27
+ ["UNKNOWN", "2.mydomain.com", 9006],
28
+ ["UNKNOWN2", "1.mydomain.com", 9005],
29
+ ])
30
+ def test_region_stickiness(region, expected_host, expected_port, grpc_router_server):
31
+ # the instance in the same region is preferred over other regions
32
+ service_id = "my.region.stickiness.test.service"
33
+
34
+ client = GRPCRouterClient("localhost", 7654)
35
+
36
+ tokens = [
37
+ register_svc(client, service_id, "USEast", 1, 9000),
38
+ register_svc(client, service_id, "USEast", 2, 9001),
39
+ register_svc(client, service_id, "USWest", 1, 9002),
40
+ register_svc(client, service_id, "USWest", 2, 9003),
41
+ register_svc(client, service_id, "USWest", 3, 9004),
42
+ register_svc(client, service_id, "", 1, 9005),
43
+ register_svc(client, service_id, "", 2, 9006),
44
+ ]
45
+
46
+ try:
47
+ host, port = client.get_service(service_id, region=region)
48
+ assert host == expected_host
49
+ assert port == expected_port
50
+ finally:
51
+ for token in tokens:
52
+ client.deregister_service(
53
+ service_id=service_id,
54
+ service_token=token
55
+ )
56
+
57
+
58
+ @pytest.mark.parametrize('region,expected_host,expected_port', [
59
+ ["USWest", "USWest1.mydomain.com", 9002],
60
+ ["USWest", "USWest2.mydomain.com", 9003],
61
+ ["USWest", "USWest3.mydomain.com", 9004],
62
+ ["USEast", "USEast1.mydomain.com", 9000],
63
+ ["", "USEast1.mydomain.com", 9000],
64
+ ["USEast", "USEast2.mydomain.com", 9001],
65
+ ["USCentral", "USCentral1.mydomain.com", 9005],
66
+ ["", "USEast2.mydomain.com", 9001],
67
+ ["", "USWest1.mydomain.com", 9002],
68
+ ["USWest", "USWest1.mydomain.com", 9002],
69
+ ["USEast", "USEast1.mydomain.com", 9000],
70
+ ["UNKNOWN", "USWest2.mydomain.com", 9003],
71
+ ["UNKNOWN2", "USWest3.mydomain.com", 9004],
72
+ ["USCentral", "USCentral2.mydomain.com", 9006],
73
+ ["UNKNOWN", "USCentral1.mydomain.com", 9005],
74
+ ["UNKNOWN", "USCentral2.mydomain.com", 9006],
75
+ ["NOREGION", "USEast1.mydomain.com", 9000],
76
+ ])
77
+ def test_region_no_global_region(region, expected_host, expected_port, grpc_router_server):
78
+ # the instance in the same region is preferred over other regions
79
+ service_id = "my.region.noglobal.test.service"
80
+
81
+ client = GRPCRouterClient("localhost", 7654)
82
+
83
+ tokens = []
84
+
85
+ tokens = [
86
+ register_svc(client, service_id, "USEast", 1, 9000),
87
+ register_svc(client, service_id, "USEast", 2, 9001),
88
+ register_svc(client, service_id, "USWest", 1, 9002),
89
+ register_svc(client, service_id, "USWest", 2, 9003),
90
+ register_svc(client, service_id, "USWest", 3, 9004),
91
+ register_svc(client, service_id, "USCentral", 1, 9005),
92
+ register_svc(client, service_id, "USCentral", 2, 9006),
93
+ ]
94
+
95
+ try:
96
+ host, port = client.get_service(service_id, region=region)
97
+ assert host == expected_host
98
+ assert port == expected_port
99
+ finally:
100
+ for token in tokens:
101
+ client.deregister_service(
102
+ service_id=service_id,
103
+ service_token=token
104
+ )