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
- MQTT---HiveMQ源码详解(十一)Netty-Throttling
- MQTT---HiveMQ源码详解(八)Netty-WebSocket
- MQTT---HiveMQ源码详解(九)Netty-Codec
- MQTT---HiveMQ源码详解(十)Netty-Statistics
- MQTT---HiveMQ源码详解(五)Netty-启动与Listeners加载
- MQTT---HiveMQ源码详解(六)Netty-Handlers总览
- MQTT---HiveMQ源码详解(七)Netty-SSL/NoSSL
- MQTT---HiveMQ源码详解(二十一)完结篇
- MQTT---HiveMQ源码详解(十三)Netty-MQTT消息、事件处理(源码举例解读)
- MQTT---HiveMQ源码详解(十二)Netty-MQTT消息、事件处理(流程)
- MQTT---HiveMQ源码详解(一)概览
- MQTT---HiveMQ源码详解(三)配置加载
- MQTT---HiveMQ源码详解(四)插件加载
- MQTT---HiveMQ源码详解(十四)Persistence-LocalPersistence
- MQTT---HiveMQ源码详解(十六)TopicTree
- MQTT---HiveMQ源码详解(二)结构与启动
- MQTT---HiveMQ源码详解(外传)为什么使用Xodus
- MQTT---HiveMQ源码详解(十五)Persistence-Cluster/Single
- PAT甲级1098. Insertion or Heap Sort (25)
- linux 命令记录
- linux中fork()函数详解
- 解决git指令更新远程仓库github时每次都要输入用户名和密码问题
- mybatis resultMap 的日期字段无法获取时分秒的问题
- MQTT---HiveMQ源码详解(十一)Netty-Throttling
- vue vue-router vue-resource学习记录
- scrollView的一些功能
- 获取手机IP地址
- Error in GitHub
- 分布式和集群的区别
- C#简介
- Openstack Kilo Install All-in-one
- Codeforces 156B 想法