示例#1
0
 def _do_request(self, uri, allow_cache):
     hdrs = _headers(self._config.sdk_key)
     if allow_cache:
         cache_entry = self._cache.get(uri)
         if cache_entry is not None:
             hdrs['If-None-Match'] = cache_entry.etag
     r = self._http.request('GET',
                            uri,
                            headers=hdrs,
                            timeout=urllib3.Timeout(
                                connect=self._config.connect_timeout,
                                read=self._config.read_timeout),
                            retries=1)
     throw_if_unsuccessful_response(r)
     if r.status == 304 and allow_cache and cache_entry is not None:
         data = cache_entry.data
         etag = cache_entry.etag
         from_cache = True
     else:
         data = json.loads(r.data.decode('UTF-8'))
         etag = r.getheader('ETag')
         from_cache = False
         if allow_cache and etag is not None:
             self._cache[uri] = CacheEntry(data=data, etag=etag)
     log.debug("%s response status:[%d] From cache? [%s] ETag:[%s]", uri,
               r.status, from_cache, etag)
     return data
    def get_all_data(self):
        uri = self._poll_uri
        hdrs = _headers(self._config)
        cache_entry = self._cache.get(uri)
        if cache_entry is not None:
            hdrs['If-None-Match'] = cache_entry.etag
        r = self._http.request('GET',
                               uri,
                               headers=hdrs,
                               timeout=urllib3.Timeout(
                                   connect=self._config.connect_timeout,
                                   read=self._config.read_timeout),
                               retries=1)
        throw_if_unsuccessful_response(r)
        if r.status == 304 and cache_entry is not None:
            data = cache_entry.data
            etag = cache_entry.etag
            from_cache = True
        else:
            data = json.loads(r.data.decode('UTF-8'))
            etag = r.getheader('ETag')
            from_cache = False
            if etag is not None:
                self._cache[uri] = CacheEntry(data=data, etag=etag)
        log.debug("%s response status:[%d] From cache? [%s] ETag:[%s]", uri,
                  r.status, from_cache, etag)

        return {FEATURES: data['flags'], SEGMENTS: data['segments']}
        def do_send(should_retry):
            # noinspection PyBroadException
            try:
                if isinstance(events, dict):
                    body = [events]
                else:
                    body = events

                json_body = jsonpickle.encode(body, unpicklable=False)
                log.debug('Sending events payload: ' + json_body)
                hdrs = _headers(self.sdk_key)
                uri = self._config.events_uri
                r = self._session.post(uri,
                                       headers=hdrs,
                                       timeout=(self._config.connect_timeout,
                                                self._config.read_timeout),
                                       data=json_body)
                r.raise_for_status()
            except ProtocolError as e:
                if e.args is not None and len(
                        e.args) > 1 and e.args[1] is not None:
                    inner = e.args[1]
                    if inner.errno is not None and inner.errno == errno.ECONNRESET and should_retry:
                        log.warning(
                            'ProtocolError exception caught while sending events. Retrying.'
                        )
                        do_send(False)
                else:
                    log.exception(
                        'Unhandled exception in event consumer. Analytics events were not processed.'
                    )
            except:
                log.exception(
                    'Unhandled exception in event consumer. Analytics events were not processed.'
                )
示例#4
0
 def do_send(should_retry):
     # noinspection PyBroadException
     try:
         if isinstance(events, dict):
             body = [events]
         else:
             body = events
         hdrs = _headers(self._sdk_key)
         r = yield self._session.post(
             self._config.events_uri,
             headers=hdrs,
             timeout=(self._config.connect_timeout,
                      self._config.read_timeout),
             data=json.dumps(body))
         r.raise_for_status()
     except ProtocolError as e:
         inner = e.args[1]
         if inner.errno == errno.ECONNRESET and should_retry:
             log.warning(
                 'ProtocolError exception caught while sending events. Retrying.'
             )
             yield do_send(False)
         else:
             log.exception(
                 'Unhandled exception in event consumer. Analytics events were not processed.'
             )
     except:
         log.exception(
             'Unhandled exception in event consumer. Analytics events were not processed.'
         )
示例#5
0
def _post_events_with_retry(http_client, config, uri, payload_id, body,
                            events_description):
    hdrs = _headers(config)
    hdrs['Content-Type'] = 'application/json'
    if payload_id:
        hdrs['X-LaunchDarkly-Event-Schema'] = str(__CURRENT_EVENT_SCHEMA__)
        hdrs['X-LaunchDarkly-Payload-ID'] = payload_id
    can_retry = True
    context = "posting %s" % events_description
    while True:
        next_action_message = "will retry" if can_retry else "some events were dropped"
        try:
            r = http_client.request('POST',
                                    uri,
                                    headers=hdrs,
                                    body=body,
                                    timeout=urllib3.Timeout(
                                        connect=config.http.connect_timeout,
                                        read=config.http.read_timeout),
                                    retries=0)
            if r.status < 300:
                return r
            recoverable = check_if_error_is_recoverable_and_log(
                context, r.status, None, next_action_message)
            if not recoverable:
                return r
        except Exception as e:
            check_if_error_is_recoverable_and_log(context, None, str(e),
                                                  next_action_message)
        if not can_retry:
            return None
        can_retry = False
        # fixed delay of 1 second for event retries
        time.sleep(1)
示例#6
0
 def _toggle(self, key):
     hdrs = _headers(self._api_key)
     uri = self._config.base_uri + '/api/eval/features/' + key
     r = self._session.get(uri, headers=hdrs, timeout=(self._config.connect, self._config.read))
     r.raise_for_status()
     feature = r.json()
     return feature
 def get_one(self, key):
     hdrs = _headers(self._sdk_key)
     uri = self._config.get_latest_features_uri + '/' + key
     r = self._session.get(uri,
                           headers=hdrs,
                           timeout=(self._config.connect_timeout,
                                    self._config.read_timeout))
     r.raise_for_status()
     feature = r.json()
     return feature
 def get_one(self, key):
     hdrs = _headers(self._sdk_key)
     uri = self._config.get_latest_features_uri + '/' + key
     log.debug("Getting one feature flag using uri: " + uri)
     r = self._session.get(uri,
                           headers=hdrs,
                           timeout=(self._config.connect_timeout,
                                    self._config.read_timeout))
     r.raise_for_status()
     feature = r.json()
     return feature
 def get_all(self):
     hdrs = _headers(self._sdk_key)
     uri = self._config.get_latest_features_uri
     log.debug("Getting all flags using uri: " + uri)
     r = self._session.get(uri,
                           headers=hdrs,
                           timeout=(self._config.connect_timeout,
                                    self._config.read_timeout))
     r.raise_for_status()
     features = r.json()
     return features
 def run(self):
     # noinspection PyBroadException
     try:
         json_body = json.dumps(self._event_body)
         log.debug('Sending diagnostic event: ' + json_body)
         hdrs = _headers(self._config)
         uri = self._config.events_base_uri + '/diagnostic'
         r = self._http.request('POST', uri,
                                headers=hdrs,
                                timeout=urllib3.Timeout(connect=self._config.connect_timeout, read=self._config.read_timeout),
                                body=json_body,
                                retries=1)
     except Exception as e:
         log.warning(
             'Unhandled exception in event processor. Diagnostic event was not sent. [%s]', e)
示例#11
0
 def _do_send(self, output_events):
     # noinspection PyBroadException
     try:
         json_body = json.dumps(output_events)
         log.debug('Sending events payload: ' + json_body)
         hdrs = _headers(self._config.sdk_key)
         hdrs['X-LaunchDarkly-Event-Schema'] = str(__CURRENT_EVENT_SCHEMA__)
         uri = self._config.events_uri
         r = self._http.request('POST', uri,
                                headers=hdrs,
                                timeout=urllib3.Timeout(connect=self._config.connect_timeout, read=self._config.read_timeout),
                                body=json_body,
                                retries=1)
         self._response_fn(r)
         return r
     except Exception as e:
         log.warning(
             'Unhandled exception in event processor. Analytics events were not processed. [%s]', e)
示例#12
0
 def do_send(should_retry):
     # noinspection PyBroadException
     try:
         if isinstance(events, dict):
             body = [events]
         else:
             body = events
         hdrs = _headers(self._api_key)
         uri = self._config.base_uri + '/api/events/bulk'
         r = self._session.post(uri, headers=hdrs, timeout=(self._config.connect, self._config.read),
                                data=json.dumps(body))
         r.raise_for_status()
     except ProtocolError as e:
         inner = e.args[1]
         if inner.errno == errno.ECONNRESET and should_retry:
             log.warning('ProtocolError exception caught while sending events. Retrying.')
             do_send(False)
         else:
             log.exception('Unhandled exception in event consumer. Analytics events were not processed.')
     except:
         log.exception('Unhandled exception in event consumer. Analytics events were not processed.')
示例#13
0
 def do_send(should_retry):
     # noinspection PyBroadException
     try:
         json_body = self._serializer.serialize_events(events)
         log.debug('Sending events payload: ' + json_body)
         hdrs = _headers(self._config.sdk_key)
         uri = self._config.events_uri
         r = self._session.post(uri,
                                headers=hdrs,
                                timeout=(self._config.connect_timeout,
                                         self._config.read_timeout),
                                data=json_body)
         if r.status_code == 401:
             log.error(
                 'Received 401 error, no further events will be posted since SDK key is invalid'
             )
             self.stop()
             return
         r.raise_for_status()
     except ProtocolError as e:
         if e.args is not None and len(
                 e.args) > 1 and e.args[1] is not None:
             inner = e.args[1]
             if inner.errno is not None and inner.errno == errno.ECONNRESET and should_retry:
                 log.warning(
                     'ProtocolError exception caught while sending events. Retrying.'
                 )
                 do_send(False)
         else:
             log.warning(
                 'Unhandled exception in event consumer. Analytics events were not processed.',
                 exc_info=True)
     except:
         log.warning(
             'Unhandled exception in event consumer. Analytics events were not processed.',
             exc_info=True)
 def _do_request(self, uri, allow_cache):
     hdrs = _headers(self._config.sdk_key)
     if allow_cache:
         cache_entry = self._cache.get(uri)
         if cache_entry is not None:
             hdrs['If-None-Match'] = cache_entry.etag
     r = self._http.request('GET', uri,
                            headers=hdrs,
                            timeout=urllib3.Timeout(connect=self._config.connect_timeout, read=self._config.read_timeout),
                            retries=1)
     throw_if_unsuccessful_response(r)
     if r.status == 304 and cache_entry is not None:
         data = cache_entry.data
         etag = cache_entry.etag
         from_cache = True
     else:
         data = json.loads(r.data.decode('UTF-8'))
         etag = r.getheader('ETag')
         from_cache = False
         if allow_cache and etag is not None:
             self._cache[uri] = CacheEntry(data=data, etag=etag)
     log.debug("%s response status:[%d] From cache? [%s] ETag:[%s]",
         uri, r.status, from_cache, etag)
     return data