MQTT---HiveMQ源码详解(十一)Netty-Throttling

来源:互联网 发布:什么是小非农数据 编辑:程序博客网 时间:2024/04/25 19:04

源博客地址:http://blog.csdn.net/pipinet123


MQTT交流群:221405150


实现功能

  • 读取用户throttling配置,对broker的连接、消息进行限制,以保证broker在最健康、最效率的时间去serve

类图

这里写图片描述

  • 它的流量限制是使用GlobalTrafficShapingHandler去完成的,由GlobalTrafficShapingHandlerProvider来提供。
public class GlobalTrafficShapingHandlerProvider        implements Provider<GlobalTrafficShapingHandler> {    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalTrafficShapingHandlerProvider.class);    private final ShutdownRegistry shutdownRegistry;    private final ThrottlingConfigurationService throttlingConfigurationService;    @Inject    GlobalTrafficShapingHandlerProvider(ShutdownRegistry shutdownRegistry,                                        ThrottlingConfigurationService throttlingConfigurationService) {        this.shutdownRegistry = shutdownRegistry;        this.throttlingConfigurationService = throttlingConfigurationService;    }    @Override    public GlobalTrafficShapingHandler get() {        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("global-traffic-shaping-executor-%d").build();        ScheduledExecutorService globalTrafficShapingExecutorService = Executors.newSingleThreadScheduledExecutor(threadFactory);        GlobalTrafficShapingExecutorShutdown shutdown = new GlobalTrafficShapingExecutorShutdown(globalTrafficShapingExecutorService);        this.shutdownRegistry.register(shutdown);        long incomingLimit = this.throttlingConfigurationService.incomingLimit();        LOGGER.debug("Throttling incoming traffic to {} B/s", incomingLimit);        long outgoingLimit = this.throttlingConfigurationService.outgoingLimit();        LOGGER.debug("Throttling outgoing traffic to {} B/s", outgoingLimit);        GlobalTrafficShapingHandler handler = new GlobalTrafficShapingHandler(globalTrafficShapingExecutorService, outgoingLimit, incomingLimit, 1000L);        ValueChangedCallback changedCallback = createValueChangedCallback(handler);        this.throttlingConfigurationService.incomingLimitChanged(changedCallback);        this.throttlingConfigurationService.outgoingLimitChanged(changedCallback);        return handler;    }    @NotNull    private ValueChangedCallback<Long> createValueChangedCallback(GlobalTrafficShapingHandler handler) {        return new ThrottlingTrafficLimitValueChangedCallback(handler, this.throttlingConfigurationService);    }    private static class ThrottlingTrafficLimitValueChangedCallback            implements ValueChangedCallback<Long> {        private final GlobalTrafficShapingHandler globalTrafficShapingHandler;        private final ThrottlingConfigurationService throttlingConfigurationService;        public ThrottlingTrafficLimitValueChangedCallback(GlobalTrafficShapingHandler globalTrafficShapingHandler,                                                          ThrottlingConfigurationService throttlingConfigurationService) {            this.globalTrafficShapingHandler = globalTrafficShapingHandler;            this.throttlingConfigurationService = throttlingConfigurationService;        }        @Override        public void valueChanged(Long newValue) {            this.globalTrafficShapingHandler.configure(                    this.throttlingConfigurationService.outgoingLimit(),                    this.throttlingConfigurationService.incomingLimit());        }    }}
  • GlobalTrafficShapingHandlerProvider通过使用ThrottlingConfigurationService读取用户配置,来限制incoming和outgoing。

  • 同时给ShutdownRegistry注册一个GlobalTrafficShapingExecutorShutdown,以当broker关闭时,能够关闭GlobalTrafficShapingHandler中的ScheduledExecutorService。

  • 由于MQTT协议中对remainingLength的限制在256m,如此大的数据,如果发到服务端,如果数量再一多,再优秀的mqtt broker都扛不住,所以它还提供了自定义最大消息长度,以保证broker稳定运行,以及攻击,是一个阻拦。 在MqttMessageDecoder中,来判断最大消息长度,来限制连接。

  • LicenseConnectionLimiter是它读取license之后,根据license限制做的一个连接数限制,以及连接数占最大连接数限制的报警百分比限制。具体的license读取、license验证、license加密这一块由于不属于本章内容,所以不做过多细节介绍,并且由于它是商用软件,所以细节以后也不会不做透漏,请各位朋友谅解;因为本系列只是协助大家了解企业级的broker如何实现的,它考虑的思路是什么,而不是破坏别人知识产权的。

0 0
原创粉丝点击