@Override public void onAccepting(SelectableChannel channel) { throttleIfAnyThresholdIsExceeded(); }
JDiscServerConnector(ConnectorConfig config, Metric metric, Server server, ServerSocketChannel channelOpenedByActivator, ConnectionFactory... factories) { super(server, factories); this.channelOpenedByActivator = channelOpenedByActivator; this.tcpKeepAlive = config.tcpKeepAliveEnabled(); this.tcpNoDelay = config.tcpNoDelay(); this.metric = metric; this.connectorName = config.name(); this.listenPort = config.listenPort(); this.metricCtx = metric.createContext(createConnectorDimensions(listenPort, connectorName)); this.statistics = new ServerConnectionStatistics(); addBean(statistics); ConnectorConfig.Throttling throttlingConfig = config.throttling(); if (throttlingConfig.enabled()) { new ConnectionThrottler(this, throttlingConfig).registerWithConnector(); } }
private void throttleIfAnyThresholdIsExceeded() { synchronized (monitor) { if (isThrottling) return; List<String> reasons = getThrottlingReasons(); if (reasons.isEmpty()) return; log.warning(String.format("Throttling new connection. Reasons: %s", reasons)); isThrottling = true; if (connector.isAccepting()) { connector.setAccepting(false); } if (idleTimeout != null) { log.warning(String.format("Applying idle timeout to existing connections: timeout=%sms", idleTimeout)); connector.getConnectedEndPoints() .forEach(endPoint -> endPoint.setIdleTimeout(idleTimeout.toMillis())); } scheduler.schedule(this::unthrottleIfBelowThresholds, 1, TimeUnit.SECONDS); } }
private void unthrottleIfBelowThresholds() { synchronized (monitor) { if (!isThrottling) return; List<String> reasons = getThrottlingReasons(); if (!reasons.isEmpty()) { log.warning(String.format("Throttling continued. Reasons: %s", reasons)); scheduler.schedule(this::unthrottleIfBelowThresholds, 1, TimeUnit.SECONDS); return; } if (idleTimeout != null) { long originalTimeout = connector.getIdleTimeout(); log.info(String.format("Reverting idle timeout for existing connections: timeout=%sms", originalTimeout)); connector.getConnectedEndPoints() .forEach(endPoint -> endPoint.setIdleTimeout(originalTimeout)); } log.info("Throttling disabled - resource thresholds no longer exceeded"); if (!connector.isAccepting()) { connector.setAccepting(true); } isThrottling = false; } }