|
2 | 2 | import queue |
3 | 3 |
|
4 | 4 | from splitio.api import APIException |
| 5 | +from splitio.optional.loaders import asyncio |
5 | 6 |
|
6 | 7 | _LOGGER = logging.getLogger(__name__) |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class ImpressionSynchronizer(object): |
| 11 | + """Impressions synchronizer class.""" |
10 | 12 | def __init__(self, impressions_api, storage, bulk_size): |
11 | 13 | """ |
12 | 14 | Class constructor. |
@@ -95,3 +97,95 @@ def synchronize_counters(self): |
95 | 97 | except APIException: |
96 | 98 | _LOGGER.error('Exception raised while reporting impression counts') |
97 | 99 | _LOGGER.debug('Exception information: ', exc_info=True) |
| 100 | + |
| 101 | + |
| 102 | +class ImpressionSynchronizerAsync(object): |
| 103 | + """Impressions async synchronizer class.""" |
| 104 | + def __init__(self, impressions_api, storage, bulk_size): |
| 105 | + """ |
| 106 | + Class constructor. |
| 107 | +
|
| 108 | + :param impressions_api: Impressions Api object to send data to the backend |
| 109 | + :type impressions_api: splitio.api.impressions.ImpressionsAPI |
| 110 | + :param storage: Impressions Storage |
| 111 | + :type storage: splitio.storage.ImpressionsStorage |
| 112 | + :param bulk_size: How many impressions to send per push. |
| 113 | + :type bulk_size: int |
| 114 | +
|
| 115 | + """ |
| 116 | + self._api = impressions_api |
| 117 | + self._impression_storage = storage |
| 118 | + self._bulk_size = bulk_size |
| 119 | + self._failed = asyncio.Queue() |
| 120 | + |
| 121 | + async def _get_failed(self): |
| 122 | + """Return up to <BULK_SIZE> impressions stored in the failed impressions queue.""" |
| 123 | + imps = [] |
| 124 | + count = 0 |
| 125 | + while count < self._bulk_size and self._failed.qsize() > 0: |
| 126 | + try: |
| 127 | + imps.append(await self._failed.get()) |
| 128 | + count += 1 |
| 129 | + except asyncio.QueueEmpty: |
| 130 | + # If no more items in queue, break the loop |
| 131 | + break |
| 132 | + return imps |
| 133 | + |
| 134 | + async def _add_to_failed_queue(self, imps): |
| 135 | + """ |
| 136 | + Add impressions that were about to be sent to a secondary queue for failed sends. |
| 137 | +
|
| 138 | + :param imps: List of impressions that failed to be pushed. |
| 139 | + :type imps: list |
| 140 | + """ |
| 141 | + for impression in imps: |
| 142 | + await self._failed.put(impression) |
| 143 | + |
| 144 | + async def synchronize_impressions(self): |
| 145 | + """Send impressions from both the failed and new queues.""" |
| 146 | + to_send = await self._get_failed() |
| 147 | + if len(to_send) < self._bulk_size: |
| 148 | + # If the amount of previously failed items is less than the bulk |
| 149 | + # size, try to complete with new impressions from storage |
| 150 | + to_send.extend(await self._impression_storage.pop_many(self._bulk_size - len(to_send))) |
| 151 | + |
| 152 | + if not to_send: |
| 153 | + return |
| 154 | + |
| 155 | + try: |
| 156 | + await self._api.flush_impressions(to_send) |
| 157 | + except APIException: |
| 158 | + _LOGGER.error('Exception raised while reporting impressions') |
| 159 | + _LOGGER.debug('Exception information: ', exc_info=True) |
| 160 | + await self._add_to_failed_queue(to_send) |
| 161 | + |
| 162 | + |
| 163 | +class ImpressionsCountSynchronizerAsync(object): |
| 164 | + def __init__(self, impressions_api, imp_counter): |
| 165 | + """ |
| 166 | + Class constructor. |
| 167 | +
|
| 168 | + :param impressions_api: Impressions Api object to send data to the backend |
| 169 | + :type impressions_api: splitio.api.impressions.ImpressionsAPI |
| 170 | + :param impressions_manager: Impressions manager instance |
| 171 | + :type impressions_manager: splitio.engine.impressions.Manager |
| 172 | +
|
| 173 | + """ |
| 174 | + self._impressions_api = impressions_api |
| 175 | + self._impressions_counter = imp_counter |
| 176 | + |
| 177 | + async def synchronize_counters(self): |
| 178 | + """Send impressions from both the failed and new queues.""" |
| 179 | + |
| 180 | + if self._impressions_counter == None: |
| 181 | + return |
| 182 | + |
| 183 | + to_send = await self._impressions_counter.pop_all() |
| 184 | + if not to_send: |
| 185 | + return |
| 186 | + |
| 187 | + try: |
| 188 | + await self._impressions_api.flush_counters(to_send) |
| 189 | + except APIException: |
| 190 | + _LOGGER.error('Exception raised while reporting impression counts') |
| 191 | + _LOGGER.debug('Exception information: ', exc_info=True) |
0 commit comments