Last active
November 10, 2015 15:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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