MQTT

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

2017-10-22  本文已影响15人  西安PP

实现功能

类图

这里写图片描述

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());
        }
    }
}


MQTT交流群:221405150

RocketMQ交流群:10648794

NewSQL交流群:153575008


上一篇 下一篇

猜你喜欢

热点阅读