Ejemplo n.º 1
0
 def __init__(
     self,
     instance,
     service_url,
     modes=[],
     id='geovelo',
     timeout=10,
     api_key=None,
     feed_publisher=DEFAULT_GEOVELO_FEED_PUBLISHER,
     verify=True,
     **kwargs
 ):
     self.instance = instance
     self.sn_system_id = id
     if not is_url(service_url):
         raise ValueError('service_url {} is not a valid url'.format(service_url))
     self.service_url = service_url
     self.api_key = api_key
     self.timeout = timeout
     self.modes = modes
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=app.config['CIRCUIT_BREAKER_MAX_GEOVELO_FAIL'],
         reset_timeout=app.config['CIRCUIT_BREAKER_GEOVELO_TIMEOUT_S'],
     )
     self._feed_publisher = FeedPublisher(**feed_publisher) if feed_publisher else None
     self.verify = verify
Ejemplo n.º 2
0
    def __init__(self, id, service_url, service_args, timezone,
                 object_id_tag=None, destination_id_tag=None, instance=None, timeout=10, **kwargs):
        self.service_url = service_url
        self.service_args = service_args
        self.timeout = timeout  #timeout in seconds
        self.rt_system_id = id
        self.object_id_tag = object_id_tag if object_id_tag else id
        self.destination_id_tag = destination_id_tag
        self.instance = instance
        self.breaker = pybreaker.CircuitBreaker(fail_max=app.config['CIRCUIT_BREAKER_MAX_TIMEO_FAIL'],
                                                reset_timeout=app.config['CIRCUIT_BREAKER_TIMEO_TIMEOUT_S'])

        # Note: if the timezone is not know, pytz raise an error
        self.timezone = pytz.timezone(timezone)

        if kwargs.get('redis_host') and kwargs.get('rate_limit_count'):
            self.rate_limiter = RateLimiter(conditions=[{'requests': kwargs.get('rate_limit_count'),
                                                         'seconds': kwargs.get('rate_limit_duration', 1)}],
                                            redis_host=kwargs.get('redis_host'),
                                            redis_port=kwargs.get('redis_port', 6379),
                                            redis_db=kwargs.get('redis_db', 0),
                                            redis_password=kwargs.get('redis_password'),
                                            redis_namespace=kwargs.get('redis_namespace', 'jormungandr.rate_limiter'))
        else:
            self.rate_limiter = FakeRateLimiter()
Ejemplo n.º 3
0
    def __init__(self,
                 id,
                 service_url,
                 service_args,
                 timezone,
                 object_id_tag=None,
                 destination_id_tag=None,
                 instance=None,
                 timeout=10,
                 **kwargs):
        self.service_url = service_url if (service_url[-1]
                                           == u'/') else (service_url + '/')
        self.service_args = service_args
        self.timeout = timeout  # timeout in seconds
        self.rt_system_id = id
        self.object_id_tag = object_id_tag if object_id_tag else id
        self.destination_id_tag = destination_id_tag
        self.instance = instance

        fail_max = kwargs.get(
            'circuit_breaker_max_fail',
            app.config.get(str('CIRCUIT_BREAKER_MAX_CLEVERAGE_FAIL'), 5))
        reset_timeout = kwargs.get(
            'circuit_breaker_reset_timeout',
            app.config.get(str('CIRCUIT_BREAKER_CLEVERAGE_TIMEOUT_S'), 60))
        self.breaker = pybreaker.CircuitBreaker(fail_max=fail_max,
                                                reset_timeout=reset_timeout)
        self.timezone = pytz.timezone(timezone)
Ejemplo n.º 4
0
 def init_breaker(cls):
     from app import settings
     cls._breaker = pybreaker.CircuitBreaker(
         fail_max=settings['WIKI_API_CIRCUIT_MAXFAIL'],
         reset_timeout=settings['WIKI_API_CIRCUIT_TIMEOUT'],
         exclude=[HTTPError40X],
         listeners=[LogListener()])
Ejemplo n.º 5
0
 def __init__(self,
              url,
              network,
              username,
              password,
              operators={'cykleo'},
              verify_certificate=False,
              service_id=None,
              organization_id=None,
              timeout=2,
              feed_publisher=DEFAULT_CYKLEO_FEED_PUBLISHER,
              **kwargs):
     self.url = url
     self.network = network.lower()
     self.service_id = service_id
     self.organization_id = organization_id
     self.verify_certificate = verify_certificate
     self.username = username
     self.password = password
     self.operators = [o.lower() for o in operators]
     self.timeout = timeout
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=kwargs.get('circuit_breaker_max_fail',
                             app.config['CIRCUIT_BREAKER_MAX_CYKLEO_FAIL']),
         reset_timeout=kwargs.get(
             'circuit_breaker_reset_timeout',
             app.config['CIRCUIT_BREAKER_CYKLEO_TIMEOUT_S']),
     )
     self._feed_publisher = FeedPublisher(
         **feed_publisher) if feed_publisher else None
Ejemplo n.º 6
0
    def __init__(self,
                 url,
                 operators,
                 dataset,
                 timeout=1,
                 feed_publisher=DEFAULT_STAR_FEED_PUBLISHER,
                 **kwargs):

        self.ws_service_template = url + '/?dataset={}&refine.idparc={}'

        self.operators = [o.lower() for o in operators]
        self.timeout = timeout
        self.dataset = dataset

        fail_max = kwargs.get('circuit_breaker_max_fail',
                              app.config['CIRCUIT_BREAKER_MAX_STAR_FAIL'])
        reset_timeout = kwargs.get(
            'circuit_breaker_reset_timeout',
            app.config['CIRCUIT_BREAKER_STAR_TIMEOUT_S'])

        self.breaker = pybreaker.CircuitBreaker(fail_max=fail_max,
                                                reset_timeout=reset_timeout)
        self._feed_publisher = FeedPublisher(
            **feed_publisher) if feed_publisher else None

        self.log = logging.LoggerAdapter(logging.getLogger(__name__),
                                         extra={'dataset': self.dataset})
Ejemplo n.º 7
0
    def __init__(self,
                 instance,
                 service_url,
                 api_key,
                 network,
                 feed_publisher=DEFAULT_INSTANT_SYSTEM_FEED_PUBLISHER,
                 rating_scale_min=None,
                 rating_scale_max=None,
                 timeout=2):
        self.instance = instance
        self.service_url = service_url
        self.api_key = api_key
        self.network = network
        self.rating_scale_min = rating_scale_min
        self.rating_scale_max = rating_scale_max
        self.system_id = 'Instant System'
        self.timeout = timeout
        self.feed_publisher = None if feed_publisher is None else RsFeedPublisher(
            **feed_publisher)

        self.journey_metadata = rsj.MetaData(
            system_id=self.system_id,
            network=self.network,
            rating_scale_min=self.rating_scale_min,
            rating_scale_max=self.rating_scale_max)

        self.logger = logging.getLogger("{} {}".format(__name__,
                                                       self.system_id))

        self.breaker = pybreaker.CircuitBreaker(
            fail_max=app.config['CIRCUIT_BREAKER_MAX_INSTANT_SYSTEM_FAIL'],
            reset_timeout=app.
            config['CIRCUIT_BREAKER_INSTANT_SYSTEM_TIMEOUT_S'])
Ejemplo n.º 8
0
    def __init__(self,
                 instance,
                 service_url,
                 asgard_socket,
                 modes=None,
                 id=None,
                 timeout=10,
                 api_key=None,
                 socket_ttl=app.config['ASGARD_ZMQ_SOCKET_TTL_SECONDS'],
                 **kwargs):
        super(Asgard, self).__init__(name=instance.name,
                                     zmq_context=instance.context,
                                     socket_path=asgard_socket,
                                     socket_ttl=socket_ttl,
                                     instance=instance,
                                     service_url=service_url,
                                     modes=modes or [],
                                     id=id or 'asgard',
                                     timeout=timeout,
                                     api_key=api_key,
                                     **kwargs)
        self.asgard_socket = asgard_socket
        self.timeout = timeout

        self.breaker = pybreaker.CircuitBreaker(
            fail_max=app.config['CIRCUIT_BREAKER_MAX_ASGARD_FAIL'],
            reset_timeout=app.config['CIRCUIT_BREAKER_ASGARD_TIMEOUT_S'],
        )
        self.socket_ttl = socket_ttl
        self.logger = logging.getLogger(__name__)
Ejemplo n.º 9
0
def circuit_breaker(logger,
                    name,
                    fail_max=5,
                    reset_timeout=60,
                    state_storage=None,
                    exclude=None):
    if exclude is None:
        exclude = []
    breaker = pybreaker.CircuitBreaker(
        name=name,
        listeners=[breaker_logger(logger)],
        exclude=exclude,
        fail_max=fail_max,
        reset_timeout=reset_timeout,
        state_storage=state_storage,
    )

    def circuit_breaker_middleware(ctx, call):
        context = ctx.get(_CONTEXT, {})
        log = logger.bind(**context)

        if breaker.current_state == "open":
            log.info("circuitbreaker.open", name=breaker.name)

        call = breaker(call)

        try:
            ctx = call(ctx)
            return ctx
        except pybreaker.CircuitBreakerError:
            return ctx

    return circuit_breaker_middleware
Ejemplo n.º 10
0
    def __init__(
        self,
        service_url,
        api_key,
        network,
        feed_publisher=DEFAULT_BLABLALINES_FEED_PUBLISHER,
        timedelta=3600,
        timeout=2,
    ):
        self.service_url = service_url
        self.api_key = api_key
        self.network = network
        self.system_id = 'blablalines'
        self.timeout = timeout
        self.timedelta = timedelta
        self.feed_publisher = None if feed_publisher is None else RsFeedPublisher(
            **feed_publisher)

        self.logger = logging.getLogger("{} {}".format(__name__,
                                                       self.system_id))

        self.breaker = pybreaker.CircuitBreaker(
            fail_max=app.config.get(
                str('CIRCUIT_BREAKER_MAX_BLABLALINES_FAIL'), 4),
            reset_timeout=app.config.get(
                str('CIRCUIT_BREAKER_BLABLALINES_TIMEOUT_S'), 60),
        )
        self.call_params = ''

        self.journey_metadata = rsj.MetaData(system_id=self.system_id,
                                             network=self.network,
                                             rating_scale_min=None,
                                             rating_scale_max=None)
Ejemplo n.º 11
0
 def __init__(
     self, host='localhost', port=6379, password=None, db=0, default_timeout=300, key_prefix=None, **kwargs
 ):
     fail_max = kwargs.pop('fail_max', 5)
     reset_timeout = kwargs.pop('reset_timeout', 60)
     BaseRedisCache.__init__(self, host, port, password, db, default_timeout, key_prefix, **kwargs)
     self.breaker = pybreaker.CircuitBreaker(fail_max=fail_max, reset_timeout=reset_timeout)
Ejemplo n.º 12
0
    def __init__(self,
                 context,
                 name,
                 zmq_socket,
                 realtime_proxies_configuration=[]):
        self.geom = None
        self._sockets = queue.Queue()
        self.socket_path = zmq_socket
        self._scenario = None
        self._scenario_name = None
        self.nb_created_socket = 0
        self.lock = Lock()
        self.context = context
        self.name = name
        self.timezone = None  # timezone will be fetched from the kraken
        self.publication_date = -1
        self.is_up = True
        self.breaker = pybreaker.CircuitBreaker(
            fail_max=app.config['CIRCUIT_BREAKER_MAX_INSTANCE_FAIL'],
            reset_timeout=app.config['CIRCUIT_BREAKER_INSTANCE_TIMEOUT_S'])
        self.georef = georef.Kraken(self)
        self.planner = planner.Kraken(self)

        self.schedule = schedule.MixedSchedule(self)
        self.realtime_proxy_manager = realtime_schedule.RealtimeProxyManager(
            realtime_proxies_configuration)
        from jormungandr.autocomplete.kraken import Kraken
        self.autocomplete = Kraken()
Ejemplo n.º 13
0
 def __init__(
     self,
     id,
     service_url,
     requestor_ref,
     object_id_tag=None,
     destination_id_tag=None,
     instance=None,
     timeout=10,
     **kwargs
 ):
     self.service_url = service_url
     self.requestor_ref = requestor_ref  # login for siri
     self.timeout = timeout  # timeout in seconds
     self.rt_system_id = id
     self.object_id_tag = object_id_tag if object_id_tag else id
     self.destination_id_tag = destination_id_tag
     self.instance = instance
     fail_max = kwargs.get(
         'circuit_breaker_max_fail', app.config.get(str('CIRCUIT_BREAKER_MAX_SIRI_FAIL'), 5)
     )
     reset_timeout = kwargs.get(
         'circuit_breaker_reset_timeout', app.config.get(str('CIRCUIT_BREAKER_SIRI_TIMEOUT_S'), 60)
     )
     self.breaker = pybreaker.CircuitBreaker(fail_max=fail_max, reset_timeout=reset_timeout)
     # A step is applied on from_datetime to discretize calls and allow caching them
     self.from_datetime_step = kwargs.get(
         'from_datetime_step', app.config['CACHE_CONFIGURATION'].get('TIMEOUT_SIRI', 60)
     )
Ejemplo n.º 14
0
 def __init__(self,
              instance,
              service_url,
              modes=[],
              id='valhalla',
              timeout=10,
              api_key=None,
              **kwargs):
     self.instance = instance
     self.sn_system_id = id
     if not is_url(service_url):
         raise ValueError(
             'service_url {} is not a valid url'.format(service_url))
     self.service_url = service_url
     self.api_key = api_key
     self.timeout = timeout
     self.modes = modes
     self.costing_options = kwargs.get('costing_options', None)
     # kilometres is default units
     self.directions_options = {'units': 'kilometers'}
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=app.config['CIRCUIT_BREAKER_MAX_VALHALLA_FAIL'],
         reset_timeout=app.config['CIRCUIT_BREAKER_VALHALLA_TIMEOUT_S'],
     )
     # the mode's park cost represent the initial cost to leave the given mode
     # it is used to represent that it takes time to park a car or a bike
     # since valhalla does not handle such a time, we rig valhalla's result to add it
     self.mode_park_cost = kwargs.get(
         'mode_park_cost', {})  # a dict giving the park time (in s) by mode
Ejemplo n.º 15
0
    def __init__(self,
                 instance,
                 service_base_url,
                 modes=[],
                 id='here',
                 timeout=10,
                 api_id=None,
                 api_code=None,
                 feed_publisher=DEFAULT_HERE_FEED_PUBLISHER,
                 **kwargs):
        self.instance = instance
        self.sn_system_id = id
        if not service_base_url:
            raise ValueError('service_url {} is not a valid HERE url'.format(
                service_base_url))
        service_base_url = service_base_url.rstrip('/')
        self.routing_service_url = 'https://{base_url}/calculateroute.json'.format(
            base_url=service_base_url)
        self.matrix_service_url = 'https://matrix.{base_url}/calculatematrix.json'.format(
            base_url=service_base_url)
        self.api_id = api_id
        self.api_code = api_code
        self.modes = modes
        self.timeout = timeout
        self.max_points = 100  # max number of point asked in the routing matrix
        self.breaker = pybreaker.CircuitBreaker(
            fail_max=app.config['CIRCUIT_BREAKER_MAX_HERE_FAIL'],
            reset_timeout=app.config['CIRCUIT_BREAKER_HERE_TIMEOUT_S'])

        self.log = logging.LoggerAdapter(logging.getLogger(__name__),
                                         extra={'streetnetwork_id': id})
        self._feed_publisher = FeedPublisher(
            **feed_publisher) if feed_publisher else None
Ejemplo n.º 16
0
 def __init__(self,
              id,
              service_url,
              timezone,
              object_id_tag=None,
              timeout=10,
              redis_host=None,
              redis_db=0,
              redis_port=6379,
              redis_password=None,
              max_requests_by_second=15,
              redis_namespace='jormungandr.rate_limiter'):
     self.service_url = service_url
     self.timeout = timeout  # timeout in seconds
     self.rt_system_id = id
     self.object_id_tag = object_id_tag if object_id_tag else id
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=app.config['CIRCUIT_BREAKER_MAX_SYNTHESE_FAIL'],
         reset_timeout=app.config['CIRCUIT_BREAKER_SYNTHESE_TIMEOUT_S'])
     self.timezone = pytz.timezone(timezone)
     if not redis_host:
         self.rate_limiter = FakeRateLimiter()
     else:
         self.rate_limiter = RateLimiter(conditions=[{
             'requests': max_requests_by_second,
             'seconds': 1
         }],
                                         redis_host=redis_host,
                                         redis_port=redis_port,
                                         redis_db=redis_db,
                                         redis_password=redis_password,
                                         redis_namespace=redis_namespace)
Ejemplo n.º 17
0
    def __init__(self, id, service_url, service_args, timezone,
                 object_id_tag=None, destination_id_tag=None, instance=None, timeout=10, **kwargs):
        self.service_url = service_url
        self.service_args = service_args
        self.timeout = timeout  #timeout in seconds
        self.rt_system_id = id
        self.object_id_tag = object_id_tag if object_id_tag else id
        self.destination_id_tag = destination_id_tag
        self.instance = instance
        fail_max = kwargs.get('circuit_breaker_max_fail', app.config['CIRCUIT_BREAKER_MAX_TIMEO_FAIL'])
        reset_timeout = kwargs.get('circuit_breaker_reset_timeout', app.config['CIRCUIT_BREAKER_TIMEO_TIMEOUT_S'])
        self.breaker = pybreaker.CircuitBreaker(fail_max=fail_max, reset_timeout=reset_timeout)
        # A step is applied on from_datetime to discretize calls and allow caching them
        self.from_datetime_step = kwargs.get('from_datetime_step', app.config['CACHE_CONFIGURATION'].get('TIMEOUT_TIMEO', 60))

        # Note: if the timezone is not know, pytz raise an error
        self.timezone = pytz.timezone(timezone)

        if kwargs.get('redis_host') and kwargs.get('rate_limit_count'):
            self.rate_limiter = RateLimiter(conditions=[{'requests': kwargs.get('rate_limit_count'),
                                                         'seconds': kwargs.get('rate_limit_duration', 1)}],
                                            redis_host=kwargs.get('redis_host'),
                                            redis_port=kwargs.get('redis_port', 6379),
                                            redis_db=kwargs.get('redis_db', 0),
                                            redis_password=kwargs.get('redis_password'),
                                            redis_namespace=kwargs.get('redis_namespace', 'jormungandr.rate_limiter'))
        else:
            self.rate_limiter = FakeRateLimiter()
Ejemplo n.º 18
0
 def __init__(self,
              network,
              contract,
              api_key,
              operators={'jcdecaux'},
              timeout=10,
              feed_publisher=DEFAULT_JCDECAUX_FEED_PUBLISHER,
              **kwargs):
     self.network = network.lower()
     self.contract = contract
     self.api_key = api_key
     self.operators = [o.lower() for o in operators]
     self.timeout = timeout
     fail_max = kwargs.get('circuit_breaker_max_fail',
                           app.config['CIRCUIT_BREAKER_MAX_JCDECAUX_FAIL'])
     reset_timeout = kwargs.get(
         'circuit_breaker_reset_timeout',
         app.config['CIRCUIT_BREAKER_JCDECAUX_TIMEOUT_S'])
     self.breaker = pybreaker.CircuitBreaker(fail_max=fail_max,
                                             reset_timeout=reset_timeout)
     self._feed_publisher = FeedPublisher(
         **feed_publisher) if feed_publisher else None
     self._data = {}
     self._last_update = datetime.datetime(1970, 1, 1)
     self._update_interval = 30
Ejemplo n.º 19
0
 def __init__(self, network, contract, api_key, timeout=10):
     self.network = network
     self.contract = contract
     self.api_key = api_key
     self.timeout = timeout
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=app.config['CIRCUIT_BREAKER_MAX_JCDECAUX_FAIL'],
         reset_timeout=app.config['CIRCUIT_BREAKER_JCDECAUX_TIMEOUT_S'])
Ejemplo n.º 20
0
 def __init__(self, id_ao, network, url, operators={'keolis'}, timeout=5, **kwargs):
     self.id_ao = id_ao
     self.network = network.lower()
     self.WS_URL = url
     self.operators = [o.lower() for o in operators]
     self.timeout = timeout
     self._client = None
     self.breaker = pybreaker.CircuitBreaker(fail_max=kwargs.get('fail_max', 5), reset_timeout=kwargs.get('reset_timeout', 120))
Ejemplo n.º 21
0
    def __init__(self, service_url, max_park_duration=DEFAULT_MAX_PARKING_DURATION):
        self.service_url = service_url
        self.max_park_duration = max_park_duration
        # TODO: put into a config when the POC is validated
        one_minute = timedelta(seconds=60).total_seconds()
        self.breaker = pybreaker.CircuitBreaker(fail_max=10, reset_timeout=one_minute)
        self.request_timeout = timedelta(seconds=0.1).total_seconds()

        self.logger = logging.getLogger(__name__)
Ejemplo n.º 22
0
 def __init__(self, network, contract, api_key, operators={'jcdecaux'}, timeout=10, **kwargs):
     self.network = network.lower()
     self.contract = contract
     self.api_key = api_key
     self.operators = [o.lower() for o in operators]
     self.timeout = timeout
     fail_max = kwargs.get('circuit_breaker_max_fail', app.config['CIRCUIT_BREAKER_MAX_JCDECAUX_FAIL'])
     reset_timeout = kwargs.get('circuit_breaker_reset_timeout', app.config['CIRCUIT_BREAKER_JCDECAUX_TIMEOUT_S'])
     self.breaker = pybreaker.CircuitBreaker(fail_max=fail_max, reset_timeout=reset_timeout)
Ejemplo n.º 23
0
 def __init__(self, url, timeout=2, **kwargs):
     self.url = url
     self.timeout = timeout
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=kwargs.get('circuit_breaker_max_fail', app.config['CIRCUIT_BREAKER_MAX_SYTRAL_FAIL']),
         reset_timeout=kwargs.get(
             'circuit_breaker_reset_timeout', app.config['CIRCUIT_BREAKER_SYTRAL_TIMEOUT_S']
         ),
     )
Ejemplo n.º 24
0
 def __init__(self, id_ao, network, url, operators={'keolis'}, timeout=5,
              feed_publisher=DEFAULT_ATOS_FEED_PUBLISHER, **kwargs):
     self.id_ao = id_ao
     self.network = network.lower()
     self.WS_URL = url
     self.operators = [o.lower() for o in operators]
     self.timeout = timeout
     self._client = None
     self.breaker = pybreaker.CircuitBreaker(fail_max=kwargs.get('fail_max', 5), reset_timeout=kwargs.get('reset_timeout', 120))
     self._feed_publisher = FeedPublisher(**feed_publisher) if feed_publisher else None
Ejemplo n.º 25
0
 def __init__(self, **kwargs):
     self.host = kwargs.get('host')
     self.timeout = kwargs.get('timeout',
                               2)  # used for slow call, like geocoding
     # used for fast call like reverse geocoding and features
     self.fast_timeout = kwargs.get('fast_timeout', 0.2)
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=app.config['CIRCUIT_BREAKER_MAX_BRAGI_FAIL'],
         reset_timeout=app.config['CIRCUIT_BREAKER_BRAGI_TIMEOUT_S'],
     )
Ejemplo n.º 26
0
    def __init__(
        self,
        id,
        service_url,
        service_args,
        timezone,
        object_id_tag=None,
        destination_id_tag=None,
        instance=None,
        timeout=10,
        **kwargs
    ):
        self.service_url = service_url
        self.service_args = service_args
        self.timeout = timeout  # timeout in seconds
        self.rt_system_id = id
        self.object_id_tag = object_id_tag if object_id_tag else id
        self.destination_id_tag = destination_id_tag if destination_id_tag else "source"
        self.timeo_stop_code = kwargs.get("source_stop_code", "StopTimeoCode")
        self.timeo_line_code = kwargs.get("source_line_code", "LineTimeoCode")
        self.next_stop_time_number = kwargs.get("next_stop_time_number", 5)

        self.instance = instance
        fail_max = kwargs.get(
            'circuit_breaker_max_fail', app.config.get(str('CIRCUIT_BREAKER_MAX_TIMEO_FAIL'), 5)
        )
        reset_timeout = kwargs.get(
            'circuit_breaker_reset_timeout', app.config.get(str('CIRCUIT_BREAKER_TIMEO_TIMEOUT_S'), 60)
        )
        self.breaker = pybreaker.CircuitBreaker(fail_max=fail_max, reset_timeout=reset_timeout)
        # A step is applied on from_datetime to discretize calls and allow caching them
        self.from_datetime_step = kwargs.get(
            'from_datetime_step', app.config.get(str('CACHE_CONFIGURATION'), {}).get(str('TIMEOUT_TIMEO'), 60)
        )

        # Note: if the timezone is not know, pytz raise an error
        self.timezone = pytz.timezone(timezone)

        if kwargs.get('redis_host') and kwargs.get('rate_limit_count'):
            self.rate_limiter = RateLimiter(
                conditions=[
                    {'requests': kwargs.get('rate_limit_count'), 'seconds': kwargs.get('rate_limit_duration', 1)}
                ],
                redis_host=kwargs.get('redis_host'),
                redis_port=kwargs.get('redis_port', 6379),
                redis_db=kwargs.get('redis_db', 0),
                redis_password=kwargs.get('redis_password'),
                redis_namespace=kwargs.get('redis_namespace', 'jormungandr.rate_limiter'),
            )
        else:
            self.rate_limiter = FakeRateLimiter()

        # We consider that all errors, greater than or equal to 100, are blocking
        self.INTERNAL_TIMEO_ERROR_CODE_LIMIT = 100
Ejemplo n.º 27
0
    def __init__(self,
                 instance,
                 service_base_url,
                 modes=[],
                 id='here',
                 timeout=10,
                 apiKey=None,
                 matrix_type=MatrixType.simple_matrix.value,
                 max_matrix_points=default_values.here_max_matrix_points,
                 realtime_traffic=True,
                 language="english",
                 engine_type="diesel",
                 engine_average_consumption=7,
                 feed_publisher=DEFAULT_HERE_FEED_PUBLISHER,
                 **kwargs):
        self.instance = instance
        self.sn_system_id = id
        if not service_base_url:
            raise ValueError('service_url {} is not a valid HERE url'.format(
                service_base_url))
        service_base_url = service_base_url.rstrip('/')
        self.routing_service_url = 'https://{base_url}/calculateroute.json'.format(
            base_url=service_base_url)
        self.matrix_service_url = 'https://matrix.{base_url}/calculatematrix.json'.format(
            base_url=service_base_url)
        self.log = logging.LoggerAdapter(
            logging.getLogger(__name__),
            extra={'streetnetwork_id': text_type(id)})
        self.apiKey = apiKey
        self.modes = modes
        self.timeout = timeout
        self.matrix_type = self._get_matrix_type(matrix_type)
        self.max_matrix_points = self._get_max_matrix_points(max_matrix_points)
        self.realtime_traffic = self._get_realtime_traffic(realtime_traffic)
        self.language = self._get_language(language.lower())
        self.engine_type = self._get_engine_type(engine_type.lower())
        self.engine_average_consumption = engine_average_consumption
        self.breaker = pybreaker.CircuitBreaker(
            fail_max=app.config['CIRCUIT_BREAKER_MAX_HERE_FAIL'],
            reset_timeout=app.config['CIRCUIT_BREAKER_HERE_TIMEOUT_S'],
        )

        self.log.debug(
            'Here, load configuration max_matrix_points={} - matrix_type={} - realtime_traffic={} - language={} - engine_type={} - engine_average_consumption={}'
            .format(
                self.max_matrix_points,
                self.matrix_type.value,
                self.realtime_traffic.value,
                self.language.value,
                self.engine_type,
                self.engine_average_consumption,
            ))
        self._feed_publisher = FeedPublisher(
            **feed_publisher) if feed_publisher else None
Ejemplo n.º 28
0
 def __init__(self, service_url, timeout=2, **kwargs):
     self.logger = logging.getLogger(__name__)
     self.service_url = service_url
     self.timeout = timeout
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=kwargs.get(
             'circuit_breaker_max_fail',
             app.config['CIRCUIT_BREAKER_MAX_FORSETI_FAIL']),
         reset_timeout=kwargs.get(
             'circuit_breaker_reset_timeout',
             app.config['CIRCUIT_BREAKER_FORSETI_TIMEOUT_S']),
     )
Ejemplo n.º 29
0
 def __init__(self, instance, url, timeout=10, api_key=None, **kwargs):
     self.instance = instance
     if not is_url(url):
         raise ValueError('service_url is invalid, you give {}'.format(url))
     self.service_url = url
     self.api_key = api_key
     self.timeout = timeout
     self.costing_options = kwargs.get('costing_options', None)
     # kilometres is default units
     self.directions_options = {'units': 'kilometers'}
     self.breaker = pybreaker.CircuitBreaker(
         fail_max=app.config['CIRCUIT_BREAKER_MAX_VALHALLA_FAIL'],
         reset_timeout=app.config['CIRCUIT_BREAKER_VALHALLA_TIMEOUT_S'])
Ejemplo n.º 30
0
    def __init__(
        self,
        context,
        name,
        zmq_socket,
        street_network_configurations,
        ridesharing_configurations,
        realtime_proxies_configuration,
        zmq_socket_type,
        autocomplete_type,
    ):
        self.geom = None
        self._sockets = deque()
        self.socket_path = zmq_socket
        self._scenario = None
        self._scenario_name = None
        self.lock = Lock()
        self.context = context
        self.name = name
        self.timezone = None  # timezone will be fetched from the kraken
        self.publication_date = -1
        self.is_initialized = False  # kraken hasn't been called yet we don't have geom nor timezone
        self.breaker = pybreaker.CircuitBreaker(
            fail_max=app.config['CIRCUIT_BREAKER_MAX_INSTANCE_FAIL'],
            reset_timeout=app.config['CIRCUIT_BREAKER_INSTANCE_TIMEOUT_S'],
        )
        self.georef = georef.Kraken(self)
        self.planner = planner.Kraken(self)

        street_network_configurations = _set_default_street_network_config(
            street_network_configurations)
        self.street_network_services = street_network.StreetNetwork.get_street_network_services(
            self, street_network_configurations)
        self.ridesharing_services = []
        if ridesharing_configurations is not None:
            self.ridesharing_services = ridesharing_service.Ridesharing.get_ridesharing_services(
                self, ridesharing_configurations)

        self.ptref = ptref.PtRef(self)

        self.schedule = schedule.MixedSchedule(self)
        self.realtime_proxy_manager = realtime_schedule.RealtimeProxyManager(
            realtime_proxies_configuration, self)

        self._autocomplete_type = autocomplete_type
        if self._autocomplete_type is not None and self._autocomplete_type not in global_autocomplete:
            raise RuntimeError('impossible to find autocomplete system {} '
                               'cannot initialize instance {}'.format(
                                   autocomplete_type, name))

        self.zmq_socket_type = zmq_socket_type