MQTT---HiveMQ源碼詳解(十一)Netty-Throttling

實現功能

  • 讀取用戶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交流群:221405150

RocketMQ交流群:10648794

NewSQL交流群:153575008


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容