-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds FeatureSwitchAsync attribute and handler (#2419)
- Loading branch information
Showing
14 changed files
with
466 additions
and
173 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/Paramore.Brighter/FeatureSwitch/Attributes/FeatureSwitchAsyncAttribute.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#region Licence | ||
/* The MIT License (MIT) | ||
Copyright © 2014 Ian Cooper <ian_hammond_cooper@yahoo.co.uk> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the “Software”), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. */ | ||
|
||
#endregion | ||
|
||
using System; | ||
using Paramore.Brighter.FeatureSwitch.Handlers; | ||
|
||
namespace Paramore.Brighter.FeatureSwitch.Attributes | ||
{ | ||
/// <summary> | ||
/// Class FeatureSwitchAttribute. | ||
/// This attribute supports adding feature switches on a handler. It is intended to allow control over which handlers can be used at | ||
/// run-time using <see cref="FeatureSwitchStatus.Config"/> or compile time using <see cref="FeatureSwitchStatus.On"/> and <see cref="FeatureSwitchStatus.Off"/>. | ||
/// </summary> | ||
public class FeatureSwitchAsyncAttribute : RequestHandlerAttribute | ||
{ | ||
private readonly Type _handler; | ||
private readonly FeatureSwitchStatus _status; | ||
|
||
/// <summary> | ||
/// Initialises a new instance of the <see cref="FeatureSwitchAttribute"/> class. | ||
/// </summary> | ||
/// <param name="handler">The handler to feature switch</param> | ||
/// <param name="status">The status of the feature switch</param> | ||
/// <param name="step">The step.</param> | ||
/// <param name="timing">The timing.</param> | ||
public FeatureSwitchAsyncAttribute(Type handler, FeatureSwitchStatus status, int step, HandlerTiming timing = HandlerTiming.Before) : base(step, timing) | ||
{ | ||
_handler = handler; | ||
_status = status; | ||
} | ||
|
||
/// <summary> | ||
/// Initialises the paramers | ||
/// </summary> | ||
/// <returns>System.Object[]</returns> | ||
public override object[] InitializerParams() | ||
{ | ||
return new object[] { _handler, _status }; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the type of the handler | ||
/// </summary> | ||
/// <returns></returns> | ||
public override Type GetHandlerType() | ||
{ | ||
return typeof(FeatureSwitchHandlerAsync<>); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Paramore.Brighter/FeatureSwitch/Handlers/FeatureSwitchHandlerAsync.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#region Licence | ||
/* The MIT License (MIT) | ||
Copyright © 2014 Ian Cooper <ian_hammond_cooper@yahoo.co.uk> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the “Software”), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. */ | ||
|
||
#endregion | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Paramore.Brighter.FeatureSwitch.Attributes; | ||
|
||
namespace Paramore.Brighter.FeatureSwitch.Handlers | ||
{ | ||
/// <summary> | ||
/// Class FeatureSwitchHandler. | ||
/// The handler is injected into the pipeline if the <see cref="FeatureSwitchAttribute"/> is applied | ||
/// </summary> | ||
/// <typeparam name="TRequest">The type of the request</typeparam> | ||
public class FeatureSwitchHandlerAsync<TRequest> : RequestHandlerAsync<TRequest> where TRequest : class, IRequest | ||
{ | ||
private Type _handler; | ||
private FeatureSwitchStatus _status; | ||
|
||
/// <summary> | ||
/// Initializes from attribute parameters. | ||
/// </summary> | ||
/// <param name="initializerList">The initializer list.</param> | ||
public override void InitializeFromAttributeParams(params object[] initializerList) | ||
{ | ||
_handler = (Type) initializerList[0]; | ||
_status = (FeatureSwitchStatus) initializerList[1]; | ||
} | ||
|
||
/// <summary> | ||
/// Checks the status of the feature switch and either stops passes the command on to the next handler | ||
/// or stops execution of the feature switched handler. | ||
/// </summary> | ||
/// <param name="command">The command.</param> | ||
/// <returns>TRequest.</returns> | ||
public override async Task<TRequest> HandleAsync(TRequest command, CancellationToken cancellationToken = default) | ||
{ | ||
var featureEnabled = _status; | ||
|
||
if (featureEnabled is FeatureSwitchStatus.Config) | ||
{ | ||
featureEnabled = Context.FeatureSwitches?.StatusOf(_handler) ?? FeatureSwitchStatus.On; | ||
} | ||
|
||
return featureEnabled is FeatureSwitchStatus.Off | ||
? command | ||
: await base.HandleAsync(command, cancellationToken); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Paramore.Brighter.Core.Tests/FeatureSwitch/TestDoubles/MyCommandAsync.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace Paramore.Brighter.Core.Tests.FeatureSwitch.TestDoubles; | ||
|
||
public class MyCommandAsync : Command | ||
{ | ||
public MyCommandAsync() : base(Guid.NewGuid()) | ||
{ | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.