Пример #1
0
    async def ingest(self, queue):
        self.enabled = True
        client = ClientSession(self.host,
                               self.port,
                               self.ident,
                               self.secret,
                               ssl=self.tls)

        logger.info(f"Connecting to {self.host} on port {self.port}")

        for f in self.channels:
            logger.info(f"subcribing for {f}")
            client.subscribe(f)

        try:
            async for ident, channel, payload in client:
                if not any(x in channel for x in (';', '"', '{', '}')):
                    feed_msg = FeedMsg(ident, channel, payload)
                    logger.debug(f"Received feed msg {channel}")
                    if queue:
                        await queue.put(feed_msg)

        except asyncio.exceptions.CancelledError as e:
            logger.error("Cancelled ingestion")
            self.enabled = False
Пример #2
0
        async def inner():
            self.log.debug('Starting server')
            server_future = asyncio.ensure_future(self.server.serve_forever())

            self.log.debug('Creating client service')
            async with ClientSession('127.0.0.1', self.port, 'test', 'secret') as client:
                assert prometheus.REGISTRY.get_sample_value('hpfeeds_broker_client_connections') == 1
                assert prometheus.REGISTRY.get_sample_value('hpfeeds_broker_connection_made') == 1

                # Subscribe to a new thing after connection is up
                client.subscribe('test-chan')

                self.log.debug('Publishing test message')
                client.publish('test-chan', b'test message')

                self.log.debug('Waiting for read()')
                assert ('test', 'test-chan', b'test message') == await client.read()

                # We would test this after call to subscribe, but need to wait until sure server has processed command
                assert prometheus.REGISTRY.get_sample_value('hpfeeds_broker_subscriptions', {'ident': 'test', 'chan': 'test-chan'}) == 1

                # Unsubscribe while the connection is up
                client.unsubscribe('test-chan')

                # FIXME: How to test that did anything!

                # This will only have incremented when server has processed auth message
                # Test can only reliably assert this is the case after reading a message
                assert prometheus.REGISTRY.get_sample_value('hpfeeds_broker_connection_ready', {'ident': 'test'}) == 1

            self.log.debug('Stopping server')
            server_future.cancel()
            await server_future
Пример #3
0
    async def distribute_queued(self, queue):
        logging.info(f"Starting to distribute to {self.broker}...")
        self.enabled = True
        client = ClientSession(self.broker, self.port, self.identity, self.secret)

        while self.enabled or queue.qsize() > 0:

            try:
                msg = await queue.get()
                msg_as_json = self.construct_hpfeeds_msg(msg)
                for c in self.channels:
                    client.publish(c, msg_as_json)

            except asyncio.CancelledError:
                self.enabled = False
                logging.info(f"Distribution to {self.broker} cancelled")

        logging.info(f"Stopped to distribute to {self.broker}...")##
Пример #4
0
        async def inner():
            self.log.debug('Starting server')
            server_future = asyncio.ensure_future(self.server.serve_forever())
            await self.server.when_started

            self.port = self.server.endpoints[0]['port']

            import ssl
            ssl_context = ssl.create_default_context(
                ssl.Purpose.SERVER_AUTH, cafile='hpfeeds/tests/testcert.crt')
            ssl_context.check_hostname = False

            self.log.debug('Creating client service')
            client = ClientSession('127.0.0.1',
                                   self.port,
                                   'test',
                                   'secret',
                                   ssl=ssl_context)
            client.subscribe('test-chan')

            # Wait till client connected
            await client.when_connected

            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_client_connections') == 1
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_made') == 1

            self.log.debug('Publishing test message')
            client.publish('test-chan', b'test message')

            self.log.debug('Waiting for read()')
            assert ('test', 'test-chan',
                    b'test message') == await client.read()

            # We would test this after call to subscribe, but need to wait until sure server has processed command
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_subscriptions', {
                    'ident': 'test',
                    'chan': 'test-chan'
                }) == 1

            # This will only have incremented when server has processed auth message
            # Test can only reliably assert this is the case after reading a message
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_ready', {'ident': 'test'}) == 1

            self.log.debug('Stopping client')
            await client.close()

            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_send_buffer_fill',
                {'ident': 'test'}) == 12
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_send_buffer_drain',
                {'ident': 'test'}) == 32

            self.log.debug('Stopping server')
            server_future.cancel()
            await server_future
Пример #5
0
    async def distribute_queued(self, queue):
        logging.info(f"Starting to distribute to {self.broker}:{self.port}...")
        self.enabled = True

        if self.tls_enabled:
            # create default SSL context, which requires valid cert chain
            client = ClientSession(self.broker,
                                   self.port,
                                   self.identity,
                                   self.secret,
                                   ssl=self.tls_enabled)
        else:
            client = ClientSession(self.broker, self.port, self.identity,
                                   self.secret)

        while self.enabled or queue.qsize() > 0:
            # logging.info(f"Created distributor {self.broker}...")
            try:
                msg = await queue.get()
                msg_as_json = self.construct_hpfeeds_msg(msg)
                for c in self.channels:
                    client.publish(c, msg_as_json)

            except asyncio.CancelledError:
                self.enabled = False
                logging.debug(f"Distribution to {self.broker} cancelled")

        logging.debug(f"Stopped to distribute to {self.broker}...")
Пример #6
0
        async def inner():
            server_future = asyncio.ensure_future(self.server.serve_forever())

            async def example_iter():
                yield b'test message'

            async with ClientSession('127.0.0.1', self.port, 'test', 'secret') as client:
                client.subscribe('test-chan')

                await client.publish_async_iterable('test-chan', example_iter())

                assert ('test', 'test-chan', b'test message') == await client.read()

            server_future.cancel()
            await server_future
Пример #7
0
        async def inner():
            server_future = asyncio.ensure_future(self.server.serve_forever())

            async with ClientSession('127.0.0.1', self.port, 'test', 'secret') as client:
                client.subscribe('test-chan')

                client.publish('test-chan', b'test message')

                async for ident, chan, payload in client:
                    assert ident == 'test'
                    assert chan == 'test-chan'
                    assert payload == b'test message'
                    break

            server_future.cancel()
            await server_future
async def hpfeeds_publish(event_message):
    async with ClientSession(HPFSERVER, HPFPORT, HPFIDENT,
                             HPFSECRET) as client:
        client.publish('wordpress.sessions',
                       json.dumps(event_message).encode('utf-8'))
    return True
Пример #9
0
 def __init__(self, host, port, ident, secret, ssl=None):
     super().__init__(*args, **kwargs)
     self.sessions = LRUCache(1000)
     self.session = ClientSession(host, port, ident, secret, ssl)
     self.exit_stack = AsyncExitStack()