實現(xiàn)功能
- 讀取用戶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協(xié)議中對remainingLength的限制在256m,如此大的數(shù)據(jù),如果發(fā)到服務端,如果數(shù)量再一多,再優(yōu)秀的mqtt broker都扛不住,所以它還提供了自定義最大消息長度,以保證broker穩(wěn)定運行,以及攻擊,是一個阻攔。 在MqttMessageDecoder中,來判斷最大消息長度,來限制連接。
LicenseConnectionLimiter是它讀取license之后,根據(jù)license限制做的一個連接數(shù)限制,以及連接數(shù)占最大連接數(shù)限制的報警百分比限制。具體的license讀取、license驗證、license加密這一塊由于不屬于本章內(nèi)容,所以不做過多細節(jié)介紹,并且由于它是商用軟件,所以細節(jié)以后也不會不做透漏,請各位朋友諒解;因為本系列只是協(xié)助大家了解企業(yè)級的broker如何實現(xiàn)的,它考慮的思路是什么,而不是破壞別人知識產(chǎn)權的。