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

源博客地址: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如何实现的,它考虑的思路是什么,而不是破坏别人知识产权的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值