__init__.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:ablator 作者: ablator 项目源码 文件源码
def which(client_user: ClientUser, functionality: Functionality) -> Optional[Availability]:
    """
    Which Flavor of the given Functionality is enabled for the user, if any?

    Returns a Flavor object that corresponds to the ClientUser's enabled functionality,
    or `None` if the user does not have any Flavor in the given Functionality.

    Use ClientUser.user_from_object to get or create a ClientUser instance from any hashable
    object (usually a string).
    """
    context = WhichContext()
    context.client_user = client_user
    context.functionality = functionality

    pipeline = [
        # roll out strategies
        check_roll_out_recall,
        check_roll_out_enable_globally,

        # retrieve availability
        get_availability,

        # check availability and switch on based on max user count
        check_for_existing_enabled_availability,
        assert_roll_out_is_not_paused,
        assert_existence_of_release,
        assert_existence_of_flavors,
        get_enabled_count,
        create_new_availability_with_random_flavor,
        enable_availability_by_user_count,
    ]

    # Go through each function in the pipeline. If it yields an Availability, we're done
    # and can return it. Otherwise, continue until we hit the end, or catch a NoAvailability
    # exception.
    # Splitting the methods up like this helps with testing, caching, and gaining an overview over
    # what actually happens through logging. Hopefully.
    start_time = time.process_time()
    for func in pipeline:
        try:
            av = func(context)
            if av:
                save_request_log_entry(
                    str(context.functionality.id),
                    str(av.flavor_id),
                    func.__name__,
                    client_user.id,
                    time.process_time() - start_time
                )
                return av
        except NoAvailability:
            save_request_log_entry(
                str(context.functionality.id),
                None,
                func.__name__,
                client_user.id,
                time.process_time() - start_time
            )
            return None
    return None
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号