From c768ccde6c1c8aca5b1d36923874bde01f3655c9 Mon Sep 17 00:00:00 2001 From: Zhongsheng Ji <9573586@qq.com> Date: Thu, 7 Dec 2023 09:54:33 +0800 Subject: [PATCH] Replace cache+property with cached_property (#107) --- duetector/analyzer/jaeger/analyzer.py | 18 +++++------------- duetector/injectors/inspector.py | 10 ++++------ 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/duetector/analyzer/jaeger/analyzer.py b/duetector/analyzer/jaeger/analyzer.py index e835f82..6a8daa3 100644 --- a/duetector/analyzer/jaeger/analyzer.py +++ b/duetector/analyzer/jaeger/analyzer.py @@ -9,23 +9,17 @@ from google.protobuf.duration_pb2 import Duration from google.protobuf.timestamp_pb2 import Timestamp -from duetector.analyzer.jaeger.proto import model_pb2 -from duetector.exceptions import AnalysQueryError -from duetector.utils import get_grpc_cred_from_path - -try: - from functools import cache -except ImportError: - from functools import lru_cache as cache - from duetector.analyzer.base import Analyzer +from duetector.analyzer.jaeger.proto import model_pb2 from duetector.analyzer.jaeger.proto.model_pb2 import Span from duetector.analyzer.jaeger.proto.query_pb2 import * from duetector.analyzer.jaeger.proto.query_pb2_grpc import * from duetector.analyzer.models import AnalyzerBrief, Brief, Tracking +from duetector.exceptions import AnalysQueryError from duetector.extension.analyzer import hookimpl from duetector.log import logger from duetector.otel import OTelInspector +from duetector.utils import get_grpc_cred_from_path ChannelInitializer = Callable[[], grpc.aio.Channel] @@ -217,8 +211,7 @@ class JaegerAnalyzer(Analyzer): def __init__(self, config: dict[str, Any] | None = None, *args, **kwargs): super().__init__(config, *args, **kwargs) - @property - @cache + @functools.cached_property def channel_initializer(self) -> ChannelInitializer: """ Example: @@ -242,8 +235,7 @@ def channel_initializer(self) -> ChannelInitializer: return functools.partial(target_func, **kwargs) - @property - @cache + @functools.cached_property def connector(self): return JaegerConnector(self.channel_initializer) diff --git a/duetector/injectors/inspector.py b/duetector/injectors/inspector.py index 7ab5cff..4935d97 100644 --- a/duetector/injectors/inspector.py +++ b/duetector/injectors/inspector.py @@ -5,6 +5,7 @@ import os import signal from datetime import datetime, timedelta +from functools import cached_property from pathlib import Path from threading import Event, Thread from typing import Any, Dict, List, Optional @@ -218,8 +219,7 @@ class Inspector: prefix = "inspector" name = None - @property - @cache + @cached_property def id(self) -> str: return with_prefix(self.sep, self.prefix, self.name or self.__class__.__name__) @@ -262,8 +262,7 @@ def stop(self): class NamespaceInspector(Inspector): - @property - @cache + @cached_property def name(self) -> str: return self.sep.join(["proc", "namespace"]) @@ -290,8 +289,7 @@ def stop(self): class CgroupInspector(Inspector): - @property - @cache + @cached_property def name(self) -> str: return self.sep.join(["proc", "cgroup"])