MQTT---HiveMQ源码详解(十一)Netty-Throttling
2017-03-11 00:02
671 查看
源博客地址:http://blog.csdn.net/pipinet123
它的流量限制是使用GlobalTrafficShapingHandler去完成的,由GlobalTrafficShapingHandlerProvider来提供。
GlobalTrafficShapingHandlerProvider通过使用ThrottlingConfigurationService读取用户配置,来限制incoming和outgoing。
同时给ShutdownRegistry注册一个GlobalTrafficShapingExecutorShutdown,以当broker关闭时,能够关闭GlobalTrafficShapingHandler中的ScheduledExecutorService。
由于MQTT协议中对remainingLength的限制在256m,如此大的数据,如果发到服务端,如果数量再一多,再优秀的mqtt broker都扛不住,所以它还提供了自定义最大消息长度,以保证broker稳定运行,以及攻击,是一个阻拦。 在MqttMessageDecoder中,来判断最大消息长度,来限制连接。
LicenseConnectionLimiter是它读取license之后,根据license限制做的一个连接数限制,以及连接数占最大连接数限制的报警百分比限制。具体的license读取、license验证、license加密这一块由于不属于本章内容,所以不做过多细节介绍,并且由于它是商用软件,所以细节以后也不会不做透漏,请各位朋友谅解;因为本系列只是协助大家了解企业级的broker如何实现的,它考虑的思路是什么,而不是破坏别人知识产权的。
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如何实现的,它考虑的思路是什么,而不是破坏别人知识产权的。
相关文章推荐
- MQTT---HiveMQ源码详解(十三)Netty-MQTT消息、事件处理(源码举例解读)
- MQTT---HiveMQ源码详解(八)Netty-WebSocket
- MQTT---HiveMQ源码详解(十)Netty-Statistics
- MQTT---HiveMQ源码详解(五)Netty-启动与Listeners加载
- MQTT---HiveMQ源码详解(七)Netty-SSL/NoSSL
- MQTT---HiveMQ源码详解(六)Netty-Handlers总览
- MQTT---HiveMQ源码详解(十二)Netty-MQTT消息、事件处理(流程)
- MQTT---HiveMQ源码详解(九)Netty-Codec
- MQTT---HiveMQ源码详解(二十一)完结篇
- MQTT---HiveMQ源码详解(二)结构与启动
- MQTT---HiveMQ源码详解(十八)Cluster-kryo与Serializer
- MQTT---HiveMQ源码详解(十六)TopicTree
- MQTT---HiveMQ源码详解(十五)Persistence-Cluster/Single
- MQTT---HiveMQ源码详解(三)配置加载
- MQTT---HiveMQ源码详解(十七)Cluster-Consistent Hashing Ring & Node Lifecycle
- MQTT---HiveMQ源码详解(一)概览
- MQTT---HiveMQ源码详解(二十)Cluster-Replicate/VectorClock
- MQTT---HiveMQ源码详解(十四)Persistence-LocalPersistence
- MQTT---HiveMQ源码详解(十九)Cluster-Request/Response
- MQTT---HiveMQ源码详解(四)插件加载