Ejemplo n.º 1
0
    def __init__(self, locators=("localhost:10053",),
                 cache=DEFAULT_SERVICE_CACHE_COUNT,
                 request_id_header="", sticky_header="X-Cocaine-Sticky",
                 forcegen_request_header=False,
                 default_tracing_chance=DEFAULT_TRACING_CHANCE,
                 configuration_service="unicorn",
                 client_id=0,
                 client_secret='',
                 mapped_headers=[],
                 tracing_conf_path="/zipkin_sampling",
                 timeouts_conf_path="/proxy_apps_timeouts",
                 srw_config=None,
                 allow_json_rpc=True,
                 ioloop=None, **config):
        # stats
        self.requests_in_progress = 0
        self.requests_disconnections = 0
        self.requests_total = 0

        self.io_loop = ioloop or tornado.ioloop.IOLoop.current()
        self.service_cache_count = cache
        self.spool_size = int(self.service_cache_count * 1.5)
        self.refresh_period = config.get("refresh_timeout", DEFAULT_REFRESH_PERIOD)
        self.locator_endpoints = [parse_locators_endpoints(i) for i in locators]
        # it's initialized after start
        # to avoid an io_loop creation before fork
        self.locator = Locator(endpoints=self.locator_endpoints)
        # it's used to reply on `ping` method
        self.locator_status = False

        # active applications
        self.cache = collections.defaultdict(list)
        # routing groups from Locator service
        self.current_rg = {}

        self.logger = logging.getLogger("cocaine.proxy.general")
        self.access_log = logging.getLogger("cocaine.proxy.access")
        self.access_log.propagate = False
        self.logger.info("locators %s",
                         ','.join("%s:%d" % (h, p) for h, p in self.locator_endpoints))

        self.sticky_header = sticky_header
        self.mapped_headers = mapped_headers
        self.logger.info("mapping headers - %s", str(self.mapped_headers))

        self.plugins = []
        if srw_config:
            for config in srw_config:
                name, cfg = config["type"], config["args"]
                self.logger.info("initialize plugin %s", name)
                self.plugins.append(load_plugin(name, self, cfg))

        if allow_json_rpc:
            self.plugins.append(load_plugin('cocaine.proxy.jsonrpc.JSONRPC', self, {}))

        self.logger.info("conf path in `%s` configuration service: %s",
                         configuration_service, tracing_conf_path)
        repo = PooledServiceFactory(self.locator_endpoints)
        repo.secure = TVM(repo, client_id, client_secret)

        if client_id == 0 or client_secret == '':
            self.logger.info("using non-authenticated unicorn access")
            self.unicorn = repo.create_service(configuration_service)
        else:
            self.logger.info("using authenticated unicorn access")
            self.unicorn = repo.create_secure_service(configuration_service)
        self.sampled_apps = {}
        self.default_tracing_chance = default_tracing_chance
        self.tracing_conf_path = tracing_conf_path

        self.io_loop.add_future(self.on_sampling_updates(),
                                lambda x: self.logger.error("the sample updater must not exit"))

        self.timeouts_conf_path = timeouts_conf_path
        self.timeouts = {}
        self.io_loop.add_future(self.on_timeouts_updates(),
                                lambda x: self.logger.error("the timeouts updater must not exit"))

        if request_id_header:
            self.get_request_id = functools.partial(get_request_id, request_id_header,
                                                    force=forcegen_request_header)
        else:
            self.get_request_id = generate_request_id

        # post the watcher for routing groups
        self.io_loop.add_future(self.on_routing_groups_update(),
                                lambda x: self.logger.error("the updater must not exit"))
        # run infinity check locator health status
        self.locator_health_check()
Ejemplo n.º 2
0
    def __init__(self,
                 locators=("localhost:10053", ),
                 cache=DEFAULT_SERVICE_CACHE_COUNT,
                 request_id_header="",
                 sticky_header="X-Cocaine-Sticky",
                 forcegen_request_header=False,
                 default_tracing_chance=DEFAULT_TRACING_CHANCE,
                 configuration_service="unicorn",
                 tracing_conf_path="/zipkin_sampling",
                 timeouts_conf_path="/proxy_apps_timeouts",
                 srw_config=None,
                 allow_json_rpc=True,
                 ioloop=None,
                 **config):
        # stats
        self.requests_in_progress = 0
        self.requests_disconnections = 0
        self.requests_total = 0

        self.io_loop = ioloop or tornado.ioloop.IOLoop.current()
        self.service_cache_count = cache
        self.spool_size = int(self.service_cache_count * 1.5)
        self.refresh_period = config.get("refresh_timeout",
                                         DEFAULT_REFRESH_PERIOD)
        self.locator_endpoints = [
            parse_locators_endpoints(i) for i in locators
        ]
        # it's initialized after start
        # to avoid an io_loop creation before fork
        self.locator = Locator(endpoints=self.locator_endpoints)
        # it's used to reply on `ping` method
        self.locator_status = False

        # active applications
        self.cache = collections.defaultdict(list)
        # routing groups from Locator service
        self.current_rg = {}

        self.logger = logging.getLogger("cocaine.proxy.general")
        self.access_log = logging.getLogger("cocaine.proxy.access")
        self.access_log.propagate = False
        self.logger.info(
            "locators %s",
            ','.join("%s:%d" % (h, p) for h, p in self.locator_endpoints))

        self.sticky_header = sticky_header

        self.plugins = []
        if srw_config:
            for config in srw_config:
                name, cfg = config["type"], config["args"]
                self.logger.info("initialize plugin %s", name)
                self.plugins.append(load_plugin(name, self, cfg))

        if allow_json_rpc:
            self.plugins.append(
                load_plugin('cocaine.proxy.jsonrpc.JSONRPC', self, {}))

        self.logger.info("conf path in `%s` configuration service: %s",
                         configuration_service, tracing_conf_path)
        self.unicorn = Service(configuration_service, locator=self.locator)
        self.sampled_apps = {}
        self.default_tracing_chance = default_tracing_chance
        self.tracing_conf_path = tracing_conf_path

        self.io_loop.add_future(
            self.on_sampling_updates(),
            lambda x: self.logger.error("the sample updater must not exit"))

        self.timeouts_conf_path = timeouts_conf_path
        self.timeouts = {}
        self.io_loop.add_future(
            self.on_timeouts_updates(),
            lambda x: self.logger.error("the timeouts updater must not exit"))

        if request_id_header:
            self.get_request_id = functools.partial(
                get_request_id,
                request_id_header,
                force=forcegen_request_header)
        else:
            self.get_request_id = generate_request_id

        # post the watcher for routing groups
        self.io_loop.add_future(
            self.on_routing_groups_update(),
            lambda x: self.logger.error("the updater must not exit"))
        # run infinity check locator health status
        self.locator_health_check()