rb-commons 0.2.3__py3-none-any.whl → 0.2.4__py3-none-any.whl
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.
- rb_commons/http/consul.py +46 -0
- {rb_commons-0.2.3.dist-info → rb_commons-0.2.4.dist-info}/METADATA +1 -1
- {rb_commons-0.2.3.dist-info → rb_commons-0.2.4.dist-info}/RECORD +5 -4
- {rb_commons-0.2.3.dist-info → rb_commons-0.2.4.dist-info}/WHEEL +0 -0
- {rb_commons-0.2.3.dist-info → rb_commons-0.2.4.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
import aiocache
|
2
|
+
import consul
|
3
|
+
from fastapi import HTTPException, Request
|
4
|
+
from rb_commons.configs.config import configs
|
5
|
+
|
6
|
+
|
7
|
+
class ServiceDiscovery:
|
8
|
+
def __init__(self) -> None:
|
9
|
+
self.consul_client = consul.Consul(host=configs.consul_host, port=configs.consul_port)
|
10
|
+
self.cache = aiocache.Cache(aiocache.SimpleMemoryCache)
|
11
|
+
|
12
|
+
async def get_service_instances(self, service_name: str) -> dict:
|
13
|
+
"""Get a healthy service instance from Consul"""
|
14
|
+
index, services = self.consul_client.health.service(service_name, passing=True)
|
15
|
+
|
16
|
+
if not services:
|
17
|
+
raise HTTPException(status_code=503,
|
18
|
+
detail="Service not available")
|
19
|
+
return services
|
20
|
+
|
21
|
+
def select_instance(self, instances: list, request: Request) -> dict:
|
22
|
+
"""Select instance using consistent hashing"""
|
23
|
+
if not instances:
|
24
|
+
raise HTTPException(status_code=503, detail="No healthy instances")
|
25
|
+
|
26
|
+
key = request.client.host
|
27
|
+
hash_value = hash(key)
|
28
|
+
return instances[hash_value % len(instances)]
|
29
|
+
|
30
|
+
async def _get_cached_service_instances(self, service_name: str) -> list:
|
31
|
+
"""Get service instances with caching"""
|
32
|
+
cache_key = f"service:{service_name}"
|
33
|
+
instances = await self.cache.get(cache_key)
|
34
|
+
if not instances:
|
35
|
+
instances = await self.get_service_instances(service_name)
|
36
|
+
if instances:
|
37
|
+
await self.cache.set(cache_key, instances, ttl=30) # Cache for 30 seconds
|
38
|
+
return instances
|
39
|
+
|
40
|
+
def build_target_url(self, instance: dict, path: str,
|
41
|
+
request: Request) -> str:
|
42
|
+
"""Build target URL with proper path handling"""
|
43
|
+
service_address = instance['Service']['Address'] or instance['Node']['Address']
|
44
|
+
service_port = instance['Service']['Port']
|
45
|
+
target_path = request.url.path.replace(path, "", 1)
|
46
|
+
return f"http://{service_address}:{service_port}{path}{target_path}"
|
@@ -7,6 +7,7 @@ rb_commons/configs/injections.py,sha256=6B1EOgIGnkWv3UrFaV9PRgG0-CJAbLu1UZ3kq-Sj
|
|
7
7
|
rb_commons/configs/rabbitmq.py,sha256=vUqa_PcZkPp1lX0B6HLSAXVMUcPR2_8-8cN-C7xFxRI,741
|
8
8
|
rb_commons/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
9
9
|
rb_commons/http/base_api.py,sha256=W-nXoO0TcngSMVgmJHFRdXeOrI9FDPi36I_Ih8hP3VE,4824
|
10
|
+
rb_commons/http/consul.py,sha256=fFqvMueo_-AWxOE4NEsikC4tvHbS6QD8l5JnoW8jyIQ,2026
|
10
11
|
rb_commons/http/exceptions.py,sha256=EGRMr1cRgiJ9Q2tkfANbf0c6-zzXf1CD6J3cmCaT_FA,1885
|
11
12
|
rb_commons/orm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
12
13
|
rb_commons/orm/exceptions.py,sha256=1aMctiEwrPjyehoXVX1l6ML5ZOhmDkmBISzlTD5ey1Y,509
|
@@ -17,7 +18,7 @@ rb_commons/schemes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuF
|
|
17
18
|
rb_commons/schemes/jwt.py,sha256=F66JJDhholuOPPzlKeoC6f1TL4gXg4oRUrV5yheNpyo,1675
|
18
19
|
rb_commons/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
19
20
|
rb_commons/utils/media.py,sha256=KNY_9SdRa3Rp7d3B1tZaXkhmzVa65RcS62BYwZP1bVM,332
|
20
|
-
rb_commons-0.2.
|
21
|
-
rb_commons-0.2.
|
22
|
-
rb_commons-0.2.
|
23
|
-
rb_commons-0.2.
|
21
|
+
rb_commons-0.2.4.dist-info/METADATA,sha256=I_QUMkGmUek2TX9zJIDw_U5Ws0lV5JhrwXS22Tw-t_g,6570
|
22
|
+
rb_commons-0.2.4.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
23
|
+
rb_commons-0.2.4.dist-info/top_level.txt,sha256=HPx_WAYo3_fbg1WCeGHsz3wPGio1ucbnrlm2lmqlJog,11
|
24
|
+
rb_commons-0.2.4.dist-info/RECORD,,
|
File without changes
|
File without changes
|