Esempio n. 1
0
 def __init__(self,
              label: str,
              loader: Callable,
              default=None,
              lazy_load: bool = True,
              on_access: Optional[Callable] = None,
              on_pre_load: Optional[Callable] = None,
              on_post_load: Optional[Callable] = None,
              on_load_error: Optional[Callable] = None,
              on_use_default: Optional[Callable] = None,
              on_refresh: Optional[Callable] = None):
     assert isinstance(
         label, str) and label, "[label] must be a string and not empty"
     self.__label = label
     #
     assert callable(loader), "[loader] must be a function"
     self.__loader = loader
     #
     self.__default = default
     #
     if callable(on_access):
         self.__on_access = on_access
     #
     if callable(on_pre_load):
         self.__on_pre_load = on_pre_load
     else:
         self.__on_pre_load = default_on_pre_load
     #
     if callable(on_post_load):
         self.__on_post_load = on_post_load
     else:
         self.__on_post_load = default_on_post_load
     #
     if callable(on_load_error):
         self.__on_load_error = on_load_error
     else:
         self.__on_load_error = default_on_load_error
     #
     if callable(on_use_default):
         self.__on_use_default = on_use_default
     else:
         self.__on_use_default = default_on_use_default
     #
     if callable(on_refresh):
         self.__on_refresh = on_refresh
     else:
         self.__on_refresh = default_on_refresh
     #
     self.__load_lock = threading.RLock()
     self.__rwhandler = rwlock.RWLockFairD()
     #
     if not lazy_load:
         self.payload()
Esempio n. 2
0
    def test_read_vs_downgrade_fair(self) -> None:
        """
		# Given: Instance of RWLockFairD.

		# When: A reader lock is acquired OR A writer lock is downgraded.

		# Then: The internal state should be the same.
		"""
        # ## Arrange
        c_rwlock_1 = rwlock.RWLockFairD()
        c_rwlock_2 = rwlock.RWLockFairD()

        def assert_internal_state():
            """Assert internal."""
            self.assertEqual(int(c_rwlock_1.v_read_count),
                             int(c_rwlock_2.v_read_count))
            self.assertEqual(bool(c_rwlock_1.c_lock_read_count.locked()),
                             bool(c_rwlock_2.c_lock_read_count.locked()))
            self.assertEqual(bool(c_rwlock_1.c_lock_read.locked()),
                             bool(c_rwlock_2.c_lock_read.locked()))
            self.assertEqual(bool(c_rwlock_1.c_lock_write.locked()),
                             bool(c_rwlock_2.c_lock_write.locked()))

        # ## Assume
        assert_internal_state()

        # ## Act
        a_read_lock = c_rwlock_1.gen_rlock()
        a_read_lock.acquire()
        a_downgrade_lock: Union[rwlock.LockableD,
                                rwlock.Lockable] = c_rwlock_2.gen_wlock()
        a_downgrade_lock.acquire()
        assert isinstance(a_downgrade_lock, rwlock.LockableD)
        a_downgrade_lock = a_downgrade_lock.downgrade()
        # ## Assert
        assert_internal_state()

        a_read_lock.release()
        a_downgrade_lock.release()
        assert_internal_state()
Esempio n. 3
0
 def __init__(self,
              label: str,
              loader: Callable,
              transformer: Callable = None,
              default=None,
              lazy_load: bool = True,
              on_access: Optional[Callable] = None,
              on_pre_load: Optional[Callable] = None,
              on_post_load: Optional[Callable] = None,
              on_load_error: Optional[Callable] = None,
              on_use_default: Optional[Callable] = None,
              on_refresh: Optional[Callable] = None):
     assert isinstance(
         label, str) and label, "[label] must be a string and not empty"
     self.__label = label
     #
     assert callable(loader), "[loader] must be a function"
     self.__loader = loader
     #
     assert transformer is None or callable(
         transformer), "[transformer] must be a function"
     self.__transformer = transformer
     #
     if default is not None:
         self.__default = default
         self.__default_initial = True
     #
     self.__setting = None
     self.__setting_state = 0
     self.__context = None
     self.__context_state = 0
     #
     if callable(on_access):
         self.__on_access = on_access
     #
     if callable(on_pre_load):
         self.__on_pre_load = on_pre_load
     else:
         self.__on_pre_load = default_on_pre_load
     #
     if callable(on_post_load):
         self.__on_post_load = on_post_load
     else:
         self.__on_post_load = default_on_post_load
     #
     if callable(on_load_error):
         self.__on_load_error = on_load_error
     else:
         self.__on_load_error = default_on_load_error
     #
     if callable(on_use_default):
         self.__on_use_default = on_use_default
     else:
         self.__on_use_default = default_on_use_default
     #
     if callable(on_refresh):
         self.__on_refresh = on_refresh
     else:
         self.__on_refresh = default_on_refresh
     #
     self.__load_lock = threading.RLock()
     self.__rwhandler = rwlock.RWLockFairD()
     #
     if not lazy_load:
         self.payload()
     #
     self._observe()
Esempio n. 4
0
                        help='Capacity of functions')
    parser.add_argument('--rate',
                        default=-1,
                        type=float,
                        help='Capacity of functions')
    args = parser.parse_args()

    # Examle run:
    # time python lru.py --requests 20 --workload torch-vgg --nr_fn 10 > output.log

    workload = args.workload
    requests = args.requests
    nr_fn = args.nr_fn
    capacity = args.capacity
    rate = args.rate
    rw = [rwlock.RWLockFairD() for _ in range(nr_fn)]

    commands = []
    threads = []

    if rate == 0.75:
        commands = [
            0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
        ]  # 75%
    elif rate == 0.5:
        commands = [
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 7, 8, 9, 6, 5, 6, 7, 8, 9
        ]  # 50%
    elif rate == 0.25:
        commands = [
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 3, 4, 1, 2, 0
    def __init__(
        self,
        keys=None,
        source="",
        cache_time=300,
        ignore_errors_period=0,
        fileformat="jwks",
        keytype="RSA",
        keyusage=None,
        kid="",
        ignore_invalid_keys=True,
        httpc=None,
        httpc_params=None,
    ):
        """
        Contains a set of keys that have a common origin.
        The sources can be serveral:
        - A dictionary provided at the initialization, see keys below.
        - A list of dictionaries provided at initialization
        - A file containing one of: JWKS, DER encoded key
        - A URL pointing to a webpages from which an JWKS can be downloaded

        :param keys: A dictionary or a list of dictionaries
            with the keys ["kty", "key", "alg", "use", "kid"]
        :param source: Where the key set can be fetch from
        :param fileformat: For a local file either "jwks" or "der"
        :param keytype: Iff local file and 'der' format what kind of key it is.
            presently 'rsa' and 'ec' are supported.
        :param keyusage: What the key loaded from file should be used for.
            Only applicable for DER files
        :param ignore_invalid_keys: Ignore invalid keys
        :param httpc: A HTTP client function
        :param httpc_params: Additional parameters to pass to the HTTP client
            function
        """

        self._keys = []
        self.cache_time = cache_time
        self.etag = ""
        self.fileformat = fileformat.lower()
        self.ignore_errors_period = ignore_errors_period
        self.ignore_errors_until = None  # UNIX timestamp of last error
        self.ignore_invalid_keys = ignore_invalid_keys
        self.imp_jwks = None
        self.keytype = keytype
        self.keyusage = keyusage
        self.last_local = None  # UNIX timestamp of last local update
        self.last_remote = None  # HTTP Date of last remote update
        self.last_updated = 0
        self.local = False
        self.remote = False
        self.source = None
        self.time_out = 0

        self._lock = rwlock.RWLockFairD()
        self._lock_reader = self._lock.gen_rlock()
        self._lock_writer = self._lock.gen_wlock()

        if httpc:
            self.httpc = httpc
        else:
            self.httpc = requests.request

        self.httpc_params = httpc_params or {}

        if keys:
            self.source = None
            if isinstance(keys, dict):
                if "keys" in keys:
                    self._do_keys(keys["keys"])
                else:
                    self._do_keys([keys])
            else:
                self._do_keys(keys)
        else:
            self._set_source(source, fileformat)
            if self.local:
                self._do_local(kid)
Esempio n. 6
0
    def __init__(self, dirname):

        pathlib.Path('storage').mkdir(parents=True, exist_ok=True)
        self.dirname = r"storage/" + dirname
        pathlib.Path(self.dirname).mkdir(parents=True, exist_ok=True)
        self.lock = rwlock.RWLockFairD()
Esempio n. 7
0
 def __init__(self, segments_path, log_path):
     self.rwlock = rwlock.RWLockFairD()
     self.commitlog, self.memtable = CommitLog.resume(log_path)
     self.segments = Segments(segments_path)