예제 #1
0
    def broadcast_dollar_sys_topics(self):
        """
        Broadcast dynamic $SYS topics updates and reschedule next execution depending on 'sys_interval' config
        parameter.
        """

        # Update stats
        uptime = datetime.now() - self._stats[STAT_START_TIME]
        client_connected = self._stats[STAT_CLIENTS_CONNECTED]
        client_disconnected = self._stats[STAT_CLIENTS_DISCONNECTED]
        inflight_in = 0
        inflight_out = 0
        messages_stored = 0
        for session in self.context.sessions:
            inflight_in += session.inflight_in_count
            inflight_out += session.inflight_out_count
            messages_stored += session.retained_messages_count
        messages_stored += len(self.context.retained_messages)
        subscriptions_count = 0
        for topic in self.context.subscriptions:
            subscriptions_count += len(self.context.subscriptions[topic])

        # Broadcast updates
        tasks = deque()
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'load/bytes/received',
                int_to_bytes_str(self._stats[STAT_BYTES_RECEIVED])))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'load/bytes/sent',
                int_to_bytes_str(self._stats[STAT_BYTES_SENT])))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'messages/received',
                int_to_bytes_str(self._stats[STAT_MSG_RECEIVED])))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'messages/sent', int_to_bytes_str(self._stats[STAT_MSG_SENT])))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'time',
                str(datetime.now()).encode('utf-8')))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'uptime', int_to_bytes_str(int(uptime.total_seconds()))))
        tasks.append(
            self.schedule_broadcast_sys_topic('uptime/formated',
                                              str(uptime).encode('utf-8')))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'clients/connected', int_to_bytes_str(client_connected)))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'clients/disconnected', int_to_bytes_str(client_disconnected)))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'clients/maximum',
                int_to_bytes_str(self._stats[STAT_CLIENTS_MAXIMUM])))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'clients/total',
                int_to_bytes_str(client_connected + client_disconnected)))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'messages/inflight',
                int_to_bytes_str(inflight_in + inflight_out)))
        tasks.append(
            self.schedule_broadcast_sys_topic('messages/inflight/in',
                                              int_to_bytes_str(inflight_in)))
        tasks.append(
            self.schedule_broadcast_sys_topic('messages/inflight/out',
                                              int_to_bytes_str(inflight_out)))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'messages/inflight/stored', int_to_bytes_str(messages_stored)))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'messages/publish/received',
                int_to_bytes_str(self._stats[STAT_PUBLISH_RECEIVED])))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'messages/publish/sent',
                int_to_bytes_str(self._stats[STAT_PUBLISH_SENT])))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'messages/retained/count',
                int_to_bytes_str(len(self.context.retained_messages))))
        tasks.append(
            self.schedule_broadcast_sys_topic(
                'messages/subscriptions/count',
                int_to_bytes_str(subscriptions_count)))

        # Wait until broadcasting tasks end
        while tasks and tasks[0].done():
            tasks.popleft()
        # Reschedule
        sys_interval = int(self.context.config['sys_interval'])
        self.context.logger.debug("Broadcasting $SYS topics")
        self.sys_handle = self.context.loop.call_later(
            sys_interval, self.broadcast_dollar_sys_topics)
예제 #2
0
파일: broker.py 프로젝트: hexdump42/hbmqtt
    def broadcast_dollar_sys_topics(self):
        """
        Broadcast dynamic $SYS topics updates and reschedule next execution depending on 'sys_interval' config
        parameter.
        """

        # Update stats
        uptime = datetime.now() - self._stats[STAT_UPTIME]
        client_connected = sum(1 for k, session in self._sessions.items() if session.transitions.state == 'connected')
        if client_connected > self._stats[STAT_CLIENTS_MAXIMUM]:
            self._stats[STAT_CLIENTS_MAXIMUM] = client_connected
        client_disconnected = sum(1 for k, session in self._sessions.items() if session.transitions.state == 'disconnected')
        inflight_in = 0
        inflight_out = 0
        messages_stored = 0
        for k, session in self._sessions.items():
            inflight_in += session.inflight_in_count
            inflight_out += session.inflight_out_count
            messages_stored += session.retained_messages_count
        messages_stored += len(self._global_retained_messages)
        subscriptions_count = 0
        for topic in self._subscriptions:
            subscriptions_count += len(self._subscriptions[topic])

        # Broadcast updates
        tasks = [
            self._broadcast_sys_topic('load/bytes/received', int_to_bytes_str(self._stats[STAT_BYTES_RECEIVED])),
            self._broadcast_sys_topic('load/bytes/sent', int_to_bytes_str(self._stats[STAT_BYTES_SENT])),
            self._broadcast_sys_topic('messages/received', int_to_bytes_str(self._stats[STAT_MSG_RECEIVED])),
            self._broadcast_sys_topic('messages/sent', int_to_bytes_str(self._stats[STAT_MSG_SENT])),
            self._broadcast_sys_topic('time', str(datetime.now()).encode('utf-8')),
            self._broadcast_sys_topic('uptime', int_to_bytes_str(int(uptime.total_seconds()))),
            self._broadcast_sys_topic('uptime/formated', str(uptime).encode('utf-8')),
            self._broadcast_sys_topic('clients/connected', int_to_bytes_str(client_connected)),
            self._broadcast_sys_topic('clients/disconnected', int_to_bytes_str(client_disconnected)),
            self._broadcast_sys_topic('clients/maximum', int_to_bytes_str(self._stats[STAT_CLIENTS_MAXIMUM])),
            self._broadcast_sys_topic('clients/total', int_to_bytes_str(client_connected + client_disconnected)),
            self._broadcast_sys_topic('messages/inflight', int_to_bytes_str(inflight_in + inflight_out)),
            self._broadcast_sys_topic('messages/inflight/in', int_to_bytes_str(inflight_in)),
            self._broadcast_sys_topic('messages/inflight/out', int_to_bytes_str(inflight_out)),
            self._broadcast_sys_topic('messages/inflight/stored', int_to_bytes_str(messages_stored)),
            self._broadcast_sys_topic('messages/publish/received', int_to_bytes_str(self._stats[STAT_PUBLISH_RECEIVED])),
            self._broadcast_sys_topic('messages/publish/sent', int_to_bytes_str(self._stats[STAT_PUBLISH_SENT])),
            self._broadcast_sys_topic('messages/retained/count', int_to_bytes_str(len(self._global_retained_messages))),
            self._broadcast_sys_topic('messages/subscriptions/count', int_to_bytes_str(subscriptions_count)),
        ]

        # Wait until broadcasting tasks end
        if len(tasks) > 0:
            asyncio.wait(tasks)
        # Reschedule
        sys_interval = int(self.config['sys_interval'])
        self.logger.debug("Broadcasting $SYS topics")
        self.sys_handle = self._loop.call_later(sys_interval, self.broadcast_dollar_sys_topics)
예제 #3
0
    async def broadcast_dollar_sys_topics(self):
        """
        Broadcast dynamic $SYS topics updates and reschedule next execution depending on 'sys_interval' config
        parameter.
        """

        # Update stats
        uptime = datetime.now() - self._stats[STAT_START_TIME]
        client_connected = self._stats[STAT_CLIENTS_CONNECTED]
        client_disconnected = self._stats[STAT_CLIENTS_DISCONNECTED]
        inflight_in = 0
        inflight_out = 0
        messages_stored = 0
        for session in self.context.sessions:
            inflight_in += session.inflight_in_count
            inflight_out += session.inflight_out_count
            messages_stored += session.retained_messages_count
        messages_stored += len(self.context.retained_messages)
        subscriptions_count = 0
        for topic in self.context.subscriptions:
            subscriptions_count += len(self.context.subscriptions[topic])

        # Broadcast updates
        await self._broadcast_sys_topic(
            'load/bytes/received',
            int_to_bytes_str(self._stats[STAT_BYTES_RECEIVED]))
        await self._broadcast_sys_topic(
            'load/bytes/sent', int_to_bytes_str(self._stats[STAT_BYTES_SENT]))
        await self._broadcast_sys_topic(
            'messages/received',
            int_to_bytes_str(self._stats[STAT_MSG_RECEIVED]))
        await self._broadcast_sys_topic(
            'messages/sent', int_to_bytes_str(self._stats[STAT_MSG_SENT]))
        await self._broadcast_sys_topic('time',
                                        str(datetime.now()).encode('utf-8'))
        await self._broadcast_sys_topic(
            'uptime', int_to_bytes_str(int(uptime.total_seconds())))
        await self._broadcast_sys_topic('uptime/formated',
                                        str(uptime).encode('utf-8'))
        await self._broadcast_sys_topic('clients/connected',
                                        int_to_bytes_str(client_connected))
        await self._broadcast_sys_topic('clients/disconnected',
                                        int_to_bytes_str(client_disconnected))
        await self._broadcast_sys_topic(
            'clients/maximum',
            int_to_bytes_str(self._stats[STAT_CLIENTS_MAXIMUM]))
        await self._broadcast_sys_topic(
            'clients/total',
            int_to_bytes_str(client_connected + client_disconnected))
        await self._broadcast_sys_topic(
            'messages/inflight', int_to_bytes_str(inflight_in + inflight_out))
        await self._broadcast_sys_topic('messages/inflight/in',
                                        int_to_bytes_str(inflight_in))
        await self._broadcast_sys_topic('messages/inflight/out',
                                        int_to_bytes_str(inflight_out))
        await self._broadcast_sys_topic('messages/inflight/stored',
                                        int_to_bytes_str(messages_stored))
        await self._broadcast_sys_topic(
            'messages/publish/received',
            int_to_bytes_str(self._stats[STAT_PUBLISH_RECEIVED]))
        await self._broadcast_sys_topic(
            'messages/publish/sent',
            int_to_bytes_str(self._stats[STAT_PUBLISH_SENT]))
        await self._broadcast_sys_topic(
            'messages/retained/count',
            int_to_bytes_str(len(self.context.retained_messages)))
        await self._broadcast_sys_topic('messages/subscriptions/count',
                                        int_to_bytes_str(subscriptions_count))
예제 #4
0
    def broadcast_dollar_sys_topics(self):
        """
        Broadcast dynamic $SYS topics updates and reschedule next execution depending on 'sys_interval' config
        parameter.
        """

        # Update stats
        uptime = datetime.now() - self._stats[STAT_START_TIME]
        client_connected = self._stats[STAT_CLIENTS_CONNECTED]
        client_disconnected = self._stats[STAT_CLIENTS_DISCONNECTED]
        inflight_in = 0
        inflight_out = 0
        messages_stored = 0
        for session in self.context.sessions:
            inflight_in += session.inflight_in_count
            inflight_out += session.inflight_out_count
            messages_stored += session.retained_messages_count
        messages_stored += len(self.context.retained_messages)
        subscriptions_count = 0
        for topic in self.context.subscriptions:
            subscriptions_count += len(self.context.subscriptions[topic])

        # Broadcast updates
        tasks = deque()
        tasks.append(self.schedule_broadcast_sys_topic('load/bytes/received', int_to_bytes_str(self._stats[STAT_BYTES_RECEIVED])))
        tasks.append(self.schedule_broadcast_sys_topic('load/bytes/sent', int_to_bytes_str(self._stats[STAT_BYTES_SENT])))
        tasks.append(self.schedule_broadcast_sys_topic('messages/received', int_to_bytes_str(self._stats[STAT_MSG_RECEIVED])))
        tasks.append(self.schedule_broadcast_sys_topic('messages/sent', int_to_bytes_str(self._stats[STAT_MSG_SENT])))
        tasks.append(self.schedule_broadcast_sys_topic('time', str(datetime.now()).encode('utf-8')))
        tasks.append(self.schedule_broadcast_sys_topic('uptime', int_to_bytes_str(int(uptime.total_seconds()))))
        tasks.append(self.schedule_broadcast_sys_topic('uptime/formated', str(uptime).encode('utf-8')))
        tasks.append(self.schedule_broadcast_sys_topic('clients/connected', int_to_bytes_str(client_connected)))
        tasks.append(self.schedule_broadcast_sys_topic('clients/disconnected', int_to_bytes_str(client_disconnected)))
        tasks.append(self.schedule_broadcast_sys_topic('clients/maximum', int_to_bytes_str(self._stats[STAT_CLIENTS_MAXIMUM])))
        tasks.append(self.schedule_broadcast_sys_topic('clients/total', int_to_bytes_str(client_connected + client_disconnected)))
        tasks.append(self.schedule_broadcast_sys_topic('messages/inflight', int_to_bytes_str(inflight_in + inflight_out)))
        tasks.append(self.schedule_broadcast_sys_topic('messages/inflight/in', int_to_bytes_str(inflight_in)))
        tasks.append(self.schedule_broadcast_sys_topic('messages/inflight/out', int_to_bytes_str(inflight_out)))
        tasks.append(self.schedule_broadcast_sys_topic('messages/inflight/stored', int_to_bytes_str(messages_stored)))
        tasks.append(self.schedule_broadcast_sys_topic('messages/publish/received', int_to_bytes_str(self._stats[STAT_PUBLISH_RECEIVED])))
        tasks.append(self.schedule_broadcast_sys_topic('messages/publish/sent', int_to_bytes_str(self._stats[STAT_PUBLISH_SENT])))
        tasks.append(self.schedule_broadcast_sys_topic('messages/retained/count', int_to_bytes_str(len(self.context.retained_messages))))
        tasks.append(self.schedule_broadcast_sys_topic('messages/subscriptions/count', int_to_bytes_str(subscriptions_count)))

        # Wait until broadcasting tasks end
        while tasks and tasks[0].done():
            tasks.popleft()
        # Reschedule
        sys_interval = int(self.context.config['sys_interval'])
        self.context.logger.debug("Broadcasting $SYS topics")
        self.sys_handle = self.context.loop.call_later(sys_interval, self.broadcast_dollar_sys_topics)
예제 #5
0
    def broadcast_dollar_sys_topics(self):
        """
        Broadcast dynamic $SYS topics updates and reschedule next execution depending on 'sys_interval' config
        parameter.
        """

        # Update stats
        uptime = datetime.now() - self._stats[STAT_UPTIME]
        client_connected = sum(1 for k, session in self._sessions.items()
                               if session.transitions.state == 'connected')
        if client_connected > self._stats[STAT_CLIENTS_MAXIMUM]:
            self._stats[STAT_CLIENTS_MAXIMUM] = client_connected
        client_disconnected = sum(
            1 for k, session in self._sessions.items()
            if session.transitions.state == 'disconnected')
        inflight_in = 0
        inflight_out = 0
        messages_stored = 0
        for k, session in self._sessions.items():
            inflight_in += session.inflight_in_count
            inflight_out += session.inflight_out_count
            messages_stored += session.retained_messages_count
        messages_stored += len(self._global_retained_messages)
        subscriptions_count = 0
        for topic in self._subscriptions:
            subscriptions_count += len(self._subscriptions[topic])

        # Broadcast updates
        tasks = [
            self._broadcast_sys_topic(
                'load/bytes/received',
                int_to_bytes_str(self._stats[STAT_BYTES_RECEIVED])),
            self._broadcast_sys_topic(
                'load/bytes/sent',
                int_to_bytes_str(self._stats[STAT_BYTES_SENT])),
            self._broadcast_sys_topic(
                'messages/received',
                int_to_bytes_str(self._stats[STAT_MSG_RECEIVED])),
            self._broadcast_sys_topic(
                'messages/sent', int_to_bytes_str(self._stats[STAT_MSG_SENT])),
            self._broadcast_sys_topic('time',
                                      str(datetime.now()).encode('utf-8')),
            self._broadcast_sys_topic(
                'uptime', int_to_bytes_str(int(uptime.total_seconds()))),
            self._broadcast_sys_topic('uptime/formated',
                                      str(uptime).encode('utf-8')),
            self._broadcast_sys_topic('clients/connected',
                                      int_to_bytes_str(client_connected)),
            self._broadcast_sys_topic('clients/disconnected',
                                      int_to_bytes_str(client_disconnected)),
            self._broadcast_sys_topic(
                'clients/maximum',
                int_to_bytes_str(self._stats[STAT_CLIENTS_MAXIMUM])),
            self._broadcast_sys_topic(
                'clients/total',
                int_to_bytes_str(client_connected + client_disconnected)),
            self._broadcast_sys_topic(
                'messages/inflight',
                int_to_bytes_str(inflight_in + inflight_out)),
            self._broadcast_sys_topic('messages/inflight/in',
                                      int_to_bytes_str(inflight_in)),
            self._broadcast_sys_topic('messages/inflight/out',
                                      int_to_bytes_str(inflight_out)),
            self._broadcast_sys_topic('messages/inflight/stored',
                                      int_to_bytes_str(messages_stored)),
            self._broadcast_sys_topic(
                'messages/publish/received',
                int_to_bytes_str(self._stats[STAT_PUBLISH_RECEIVED])),
            self._broadcast_sys_topic(
                'messages/publish/sent',
                int_to_bytes_str(self._stats[STAT_PUBLISH_SENT])),
            self._broadcast_sys_topic(
                'messages/retained/count',
                int_to_bytes_str(len(self._global_retained_messages))),
            self._broadcast_sys_topic('messages/subscriptions/count',
                                      int_to_bytes_str(subscriptions_count)),
        ]

        # Wait until broadcasting tasks end
        if len(tasks) > 0:
            asyncio.wait(tasks)
        # Reschedule
        sys_interval = int(self.config['sys_interval'])
        self.logger.debug("Broadcasting $SYS topics")
        self.sys_handle = self._loop.call_later(
            sys_interval, self.broadcast_dollar_sys_topics)