From 5a44411127903ce90f627eda301ae58d769b72a6 Mon Sep 17 00:00:00 2001 From: Olivier Desenfans Date: Thu, 6 Jul 2023 16:40:04 +0200 Subject: [PATCH] Feature: VM cache did not support connecting through a Unix socket Problem: the user can only specify the URL of the cache or a complete aiohttp client session object. Solution: mirror what is done for the `AlephClient` class and allow a `unix_socket` parameter in the constructor of `VmCache` to build the client session correctly for the user. --- src/aleph/sdk/vm/cache.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/aleph/sdk/vm/cache.py b/src/aleph/sdk/vm/cache.py index b9c406ee..bad6da09 100644 --- a/src/aleph/sdk/vm/cache.py +++ b/src/aleph/sdk/vm/cache.py @@ -7,6 +7,7 @@ from pydantic import AnyHttpUrl from ..conf import settings +from ..utils import check_unix_socket_valid CacheKey = NewType("CacheKey", str) @@ -52,8 +53,22 @@ def __init__( self, session: Optional[aiohttp.ClientSession] = None, connector_url: Optional[AnyHttpUrl] = None, + unix_socket: Optional[str] = None, ): - self.session = session or aiohttp.ClientSession(base_url=connector_url) + if session: + self.session = session + else: + unix_socket_path = unix_socket or settings.API_UNIX_SOCKET + if unix_socket_path: + check_unix_socket_valid(unix_socket_path) + connector = aiohttp.UnixConnector(path=unix_socket_path) + else: + connector = None + + self.session = aiohttp.ClientSession( + base_url=connector_url, connector=connector + ) + self.cache = {} self.api_host = connector_url if connector_url else settings.API_HOST