internal virtual void AdjustChannelCount(int channelCount, int requestCount)
{
if (channelCount == 0)
{
_logger.LogWarning("Channel count is 0. Skipping channel scaling.");
return;
}
var workPerChannel = requestCount / channelCount;
var canCreateChannel = channelCount < _channelConfig.MaxChannelCount;
var canCloseChannel = channelCount > 1;
_logger.LogDebug($"Begining channel scaling.\n Channel count: {channelCount}\n Work per channel: {workPerChannel}");
if (_channelConfig.EnableScaleUp && canCreateChannel && workPerChannel > _channelConfig.WorkThreshold)
{
CreateAndWireupAsync();
return;
}
if (_channelConfig.EnableScaleDown && canCloseChannel && requestCount == 0)
{
var toClose = _channels.Last.Value;
_logger.LogInformation($"Channel '{toClose.ChannelNumber}' will be closed in {_channelConfig.GracefulCloseInterval}.");
_channels.Remove(toClose);
Timer graceful = null;
graceful = new Timer(state =>
{
graceful?.Dispose();
toClose.Dispose();
}, null, _channelConfig.GracefulCloseInterval, new TimeSpan(-1));
}
}