Exemplo n.º 1
0
    async def _get_all_subscribers(self) -> Optional[CloudSubscribersInfo]:
        subscriberdb_cloud_client = self._grpc_client_manager.get_client()
        subscribers = []
        root_digest = None
        leaf_digests = None
        req_page_token = ""  # noqa: S105
        found_empty_token = False
        sync_start = datetime.datetime.now()

        # Next page token empty means read all updates
        while not found_empty_token:  # noqa: S105
            try:
                req = ListSubscribersRequest(
                    page_size=self._subscriber_page_size,
                    page_token=req_page_token,
                )
                res = await grpc_async_wrapper(
                    subscriberdb_cloud_client.ListSubscribers.future(
                        req,
                        self.SUBSCRIBERDB_REQUEST_TIMEOUT,
                    ),
                    self._loop,
                )
                subscribers.extend(res.subscribers)
                # Cloud sends back digests during request for the first page
                if req_page_token == "":
                    root_digest = res.digests.root_digest
                    leaf_digests = res.digests.leaf_digests

                req_page_token = res.next_page_token
                found_empty_token = req_page_token == ""

            except grpc.RpcError as err:
                logging.error(
                    "Fetch subscribers error! [%s] %s", err.code(),
                    err.details(),
                )
                time_elapsed = datetime.datetime.now() - sync_start
                SUBSCRIBER_SYNC_LATENCY.observe(
                    time_elapsed.total_seconds() * 1000,
                )
                SUBSCRIBER_SYNC_FAILURE_TOTAL.inc()
                return None
        logging.info(
            "Successfully fetched all subscriber "
            "pages from the cloud!",
        )
        SUBSCRIBER_SYNC_SUCCESS_TOTAL.inc()
        time_elapsed = datetime.datetime.now() - sync_start
        SUBSCRIBER_SYNC_LATENCY.observe(
            time_elapsed.total_seconds() * 1000,
        )

        subscribers_info = CloudSubscribersInfo(
            subscribers=subscribers,
            root_digest=root_digest,
            leaf_digests=leaf_digests,
        )
        return subscribers_info
Exemplo n.º 2
0
 async def _get_subscribers(self) -> [SubscriberData]:
     subscriberdb_cloud_client = self._grpc_client_manager.get_client()
     subscribers = []
     req_page_token = ""  # noqa: S105
     res_page_token = "start_token"  # noqa: S105
     sync_start = datetime.datetime.now()
     while res_page_token != "":  # noqa: S105
         try:
             req = ListSubscribersRequest(
                 page_size=self._subscriber_page_size,
                 page_token=req_page_token,
             )
             response = await grpc_async_wrapper(
                 subscriberdb_cloud_client.ListSubscribers.future(
                     req,
                     self.SUBSCRIBERDB_REQUEST_TIMEOUT,
                 ),
                 self._loop,
             )
             subscribers.extend(response.subscribers)
             res_page_token = response.next_page_token
             req_page_token = response.next_page_token
         except grpc.RpcError as err:
             logging.error(
                 "Fetch subscribers error! [%s] %s", err.code(),
                 err.details(),
             )
             time_elapsed = datetime.datetime.now() - sync_start
             SUBSCRIBER_SYNC_LATENCY.observe(
                 time_elapsed.total_seconds() * 1000,
             )
             SUBSCRIBER_SYNC_FAILURE_TOTAL.inc()
             return None
     logging.info(
         "Successfully fetched all subscriber "
         "pages from the cloud!",
     )
     SUBSCRIBER_SYNC_SUCCESS_TOTAL.inc()
     time_elapsed = datetime.datetime.now() - sync_start
     SUBSCRIBER_SYNC_LATENCY.observe(
         time_elapsed.total_seconds() * 1000,
     )
     return subscribers