Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonsmits/8371140c5f00f08db0fc to your computer and use it in GitHub Desktop.
Save ramonsmits/8371140c5f00f08db0fc to your computer and use it in GitHub Desktop.
Throttle NServiceBus throughput when a custom check fails using service control custom checks
using System;
using NServiceBus;
using NServiceBus.Logging;
using ServiceControl.Plugin.CustomChecks;
class ThrottleThroughputWhenCheckFails : PeriodicCheck
{
NServiceBus.Unicast.UnicastBus bus;
ILog Log = LogManager.GetLogger(typeof(IntegrationCustomCheck));
bool throttled;
int throttledLimit = 1;
int unthrottledLimit = 0;
public IntegrationCustomCheck(IBus bus)
: base("Custom check", "Integration", TimeSpan.FromSeconds(5))
{
this.bus = (NServiceBus.Unicast.UnicastBus)bus;
}
int count;
bool Check()
{
return count++ % 4 == 0;
}
public override CheckResult PerformCheck()
{
var result = Check();
Log.DebugFormat("Check: {0}", result);
Log.DebugFormat("Throttled: {0}", throttled);
var transport = bus.Transport;
if (result && throttled)
{
transport.ChangeMaximumMessageThroughputPerSecond(unthrottledLimit);
throttled = false;
Log.InfoFormat("Disabled throttling");
}
if (!result && !throttled)
{
transport.ChangeMaximumMessageThroughputPerSecond(throttledLimit);
throttled = true;
Log.InfoFormat("Enabled throttling");
}
if (result)
return CheckResult.Pass;
else
return CheckResult.Failed("Check failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment