Beispiel #1
0
def init(confdict):
    """
    Initializes this module according to :ref:`our module initialization
    guidelines <module_initialization>` with the following configuration keys:

    :confkey:`hosts`
        A list of Varnish hosts passed to :func:`parse_host_port
        <score.init.parse_host_port>` and :func:`parse_list
        <score.init.parse_list>`.

    :confkey:`soft` :faint:`[default=true]`
        Whether to purge :term:`soft <soft purge>` or :term:`hard <hard purge>`.

    :confkey:`timeout` :faint:`[default=5s]`
        The timeout for sending requests to a Varnish host passed to
        :func:`parse_time_interval <score.init.parse_time_interval>`.

    :confkey:`header.domain` :faint:`[default=X-Purge-Domain]`
        The name of the header used for purging a domain.

    :confkey:`header.path` :faint:`[default=X-Purge-Path]`
        The name of the header used for purging a path.

    :confkey:`header.soft` :faint:`[default=X-Purge-Soft]`
        The name of the header used for triggering a :term:`soft purge`.
    """
    conf = dict(defaults.items())
    conf.update(confdict)
    hosts = [parse_host_port(host) for host in parse_list(conf['hosts'])]
    soft = parse_bool(conf['soft'])
    timeout = parse_time_interval(conf['timeout'])
    header_mapping = extract_conf(conf, 'header.')
    return ConfiguredVarnishModule(hosts, soft, timeout, header_mapping)
Beispiel #2
0
def parse_connect_conf(conf):
    conf = conf.copy()
    if 'hosts' in conf:
        conf['hosts'] = parse_list(conf['hosts'])
    if 'verify_certs' in conf:
        conf['verify_certs'] = parse_bool(conf['verify_certs'])
    if 'use_ssl' in conf:
        conf['use_ssl'] = parse_bool(conf['use_ssl'])
    if 'timeout' in conf:
        conf['timeout'] = parse_time_interval(conf['timeout'])
    return conf
Beispiel #3
0
def init(confdict):
    """
    Initializes this module according to :ref:`our module initialization
    guidelines <module_initialization>` with the following configuration keys:

    :confkey:`sample_interval` :confdefault:`100ms`
        Time duration to wait between measurements.

    :confkey:`output_interval` :confdefault:`5s`
        Interval in which to update resulting graph.

    :confkey:`output.folder` :confdefault:`.`
        Folder to write results to. Will create a file with this process's start
        time and PID as file name.

    :confkey:`output.file`
        This may be provided instead of ``output.folder``, in which case the
        output will be written to this specific file.
    """
    conf = dict(defaults.items())
    conf.update(confdict)
    sample_interval = parse_time_interval(conf['sample_interval'])
    output_interval = parse_time_interval(conf['output_interval'])
    if conf['output.file']:
        file = conf['output.file']
        if not os.path.isdir(os.path.dirname(file)):
            import score.perf
            raise ConfigurationError(
                score.perf, 'Folder of configured `output.file` does not exist')
    else:
        if not os.path.isdir(conf['output.folder']):
            import score.perf
            raise ConfigurationError(
                score.perf, 'Configured `output.folder` does not exist')
        now = datetime.now()
        file = os.path.join(
            conf['output.folder'], '%s-%d.svg' % (
                now.strftime('%Y-%m-%d %H:%M:%S'), os.getpid()))

    return ConfiguredPerfModule(sample_interval, output_interval, file)
Beispiel #4
0
def parse_cookie_kwargs(conf):
    if not conf['cookie'] or conf['cookie'] == 'None':
        return None
    cookie_kwargs = {
        'name': conf['cookie'],
        'path': conf['cookie.path'],
        'domain': conf['cookie.domain'],
        'secure': parse_bool(conf['cookie.secure']),
        'httponly': parse_bool(conf['cookie.httponly']),
    }
    if conf['cookie.max_age']:
        cookie_kwargs['max_age'] = \
            parse_time_interval(conf['cookie.max_age'])
    return cookie_kwargs
Beispiel #5
0
def add_route_caching(duration):
    duration = parse_time_interval(duration)

    def add_caching(route):
        callback = route.callback

        @functools.wraps(callback)
        def wrapper(ctx, *args, **kwargs):
            result = callback(ctx, *args, **kwargs)
            if ctx.http.request.method == 'GET':
                header = ('Cache-Control', 's-maxage=%d' % duration)
                ctx.http.response.headerlist.append(header)
            return result

        route.callback = wrapper
        return route

    return add_caching
Beispiel #6
0
def init(confdict, ctx):
    """
    Initializes this module acoording to :ref:`our module initialization
    guidelines <module_initialization>` with the following configuration keys:

    :confkey:`host` :confdefault:`0.0.0.0`
        The hostname to listen for connnections on.

    :confkey:`port` :confdefault:`8081`
        The port to listen for connnections on.

    :confkey:`stop_timeout` :confdefault:`None`
        Defines how long the module will wait for connections to close
        when pausing the worker. The value will be interpreted through
        a call to :func:`score.init.parse_time_interval`.

        The default value `None` indicates that the module will wait
        indefinitely. If you want to the server to terminate immediately,
        without waiting for open connections at all, you must pass "0".

    :confkey:`reuse_port` :confdefault:`False`
        Whether the ``reuse_port`` keyword argument should be passed to the
        underlying event loop's :meth:`create_server()
        <asyncio.AbstractEventLoop.create_server>` method.

    """
    conf = dict(defaults.items())
    conf.update(confdict)
    host = conf['host']
    port = int(conf['port'])
    stop_timeout = conf['stop_timeout']
    if stop_timeout == 'None':
        stop_timeout = None
    if stop_timeout is not None:
        stop_timeout = parse_time_interval(stop_timeout)
    reuse_port = parse_bool(conf['reuse_port'])
    return ConfiguredWebsocketsModule(ctx, host, port, stop_timeout,
                                      reuse_port)
Beispiel #7
0
def init(confdict):
    """
    Initializes this module acoording to :ref:`our module initialization
    guidelines <module_initialization>` with the following configuration keys:

    :confkey:`sqlalchemy.*`
        All configuration values under this key will be passed to
        :func:`sqlalchemy.engine_from_config`, which in turn calls
        :func:`sqlalchemy.create_engine` with these configuration values as
        keyword arguments. Usually the following is sufficient::

            sqlalchemy.url = postgresql://dbuser@localhost/projname

    :confkey:`maxtime` :default:`1m`
        Maximum time frame a lock can be held without being updated. Any
        lock older than this time frame is considered expired.

    """
    conf = defaults.copy()
    conf.update(confdict)
    engine = engine_from_config(conf)
    maxtime = parse_time_interval(conf['maxtime'])
    return ConfiguredDistlockModule(engine, maxtime)
Beispiel #8
0
def parse_cookie_kwargs(conf):
    if not conf['cookie'] or conf['cookie'] == 'None':
        return None
    samesite = conf['cookie.samesite']
    if samesite and samesite.strip().lower() != 'none':
        samesite = samesite.strip()
        samesite = samesite[0].upper() + samesite[1:].lower()
        if samesite not in ('Strict', 'Lax'):
            raise ValueError('cookie.samesite must be "Strict" or "Lax"')
    else:
        samesite = None
    cookie_kwargs = {
        'name': conf['cookie'],
        'path': conf['cookie.path'],
        'domain': conf['cookie.domain'],
        'secure': parse_bool(conf['cookie.secure']),
        'httponly': parse_bool(conf['cookie.httponly']),
        'samesite': samesite,
    }
    if conf['cookie.max_age']:
        cookie_kwargs['max_age'] = \
            parse_time_interval(conf['cookie.max_age'])
    return cookie_kwargs
Beispiel #9
0
def init(confdict):
    """
    Initializes this module according to :ref:`our module initialization
    guidelines <module_initialization>` with the following configuration keys:

    :confkey:`container`
        The cache container configuration. A container defines a name,
        a backend and optionally a generator and an expire. The configuration
        key for a container starts with ``container`` followed by the name
        and the configuration keys for the container.

        For example, the following configuration::

            container.greeter.backend = score.kvcache.backend.FileCache
            container.greeter.backend.path = /tmp/greeter.sqlite3
            container.greeter.generator = dotted.path.to.greeting_generator
            container.greeter.expire = 1m

        The Backend config will be passed to
        :func:`score.init.init_object`. Have a look at the configurable
        backend's constructor parameters for further information about
        the backend's configurable keys.

        To make life easier for a huge set of container configurations, we serve
        the possibility to configure backend aliases that will replace the
        container's backend config if the name matches.

        For example::

            backend.example_filecache = score.kvcache.backend.FileCache
            backend.example_filecache.path = /tmp/filecache.sqlite3
            container.greeter.backend = example_filecache
            container.greeter.generator = dotted.path.to.greeting_generator
            container.greeter.expire = 1m
            container.counter.backend = example_filecache
            container.counter.generator = dotted.path.to.counting_generator
            container.counter.expire = 30 seconds

    """
    containers = {}
    for container_conf in extract_conf(confdict, 'container.'):
        if not container_conf.endswith('.backend'):
            continue
        backend_key = 'container.%s' % container_conf
        backend_val = confdict[backend_key]
        if backend_val in extract_conf(confdict, 'backend.'):
            alias_conf = extract_conf(confdict, 'backend.%s' % backend_val)
            for k, v in alias_conf.items():
                confdict.update({'%s%s' % (backend_key, k): v})
        container_name = container_conf[:-len('.backend')]
        backend = parse_object(confdict, backend_key)
        generator_key = 'container.%s.generator' % container_name
        generator = None
        if generator_key in confdict:
            generator = parse_dotted_path(confdict[generator_key])
        expire_key = 'container.%s.expire' % container_name
        expire = None
        if expire_key in confdict:
            expire = parse_time_interval(confdict[expire_key])
        containers[container_name] = CacheContainer(container_name, backend,
                                                    generator=generator,
                                                    expire=expire)
    return ConfiguredKvCacheModule(containers)
Beispiel #10
0
 def __init__(self, uri, key, timeout):
     super().__init__()
     self.uri = uri
     self.key = key
     self.timeout = parse_time_interval(timeout)