|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using OpenFeature.Model; |
| 8 | + |
| 9 | +namespace OpenFeature |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// This class manages the execution of hooks. |
| 13 | + /// </summary> |
| 14 | + /// <typeparam name="T">type of the evaluation detail provided to the hooks</typeparam> |
| 15 | + internal partial class HookRunner<T> |
| 16 | + { |
| 17 | + private readonly ImmutableList<Hook> _hooks; |
| 18 | + |
| 19 | + private readonly List<HookContext<T>> _hookContexts; |
| 20 | + |
| 21 | + private EvaluationContext _evaluationContext; |
| 22 | + |
| 23 | + private readonly ILogger _logger; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Construct a hook runner instance. Each instance should be used for the execution of a single evaluation. |
| 27 | + /// </summary> |
| 28 | + /// <param name="hooks"> |
| 29 | + /// The hooks for the evaluation, these should be in the correct order for the before evaluation stage |
| 30 | + /// </param> |
| 31 | + /// <param name="evaluationContext"> |
| 32 | + /// The initial evaluation context, this can be updates as the hooks execute |
| 33 | + /// </param> |
| 34 | + /// <param name="sharedHookContext"> |
| 35 | + /// Contents of the initial hook context excluding the evaluation context and hook data |
| 36 | + /// </param> |
| 37 | + /// <param name="logger">Client logger instance</param> |
| 38 | + public HookRunner(ImmutableList<Hook> hooks, EvaluationContext evaluationContext, |
| 39 | + SharedHookContext<T> sharedHookContext, |
| 40 | + ILogger logger) |
| 41 | + { |
| 42 | + this._evaluationContext = evaluationContext; |
| 43 | + this._logger = logger; |
| 44 | + this._hooks = hooks; |
| 45 | + this._hookContexts = new List<HookContext<T>>(hooks.Count); |
| 46 | + for (var i = 0; i < hooks.Count; i++) |
| 47 | + { |
| 48 | + // Create hook instance specific hook context. |
| 49 | + // Hook contexts are instance specific so that the mutable hook data is scoped to each hook. |
| 50 | + this._hookContexts.Add(sharedHookContext.ToHookContext(evaluationContext)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Execute before hooks. |
| 56 | + /// </summary> |
| 57 | + /// <param name="hints">Optional hook hints</param> |
| 58 | + /// <param name="cancellationToken">Cancellation token which can cancel hook operations</param> |
| 59 | + /// <returns>Context with any modifications from the before hooks</returns> |
| 60 | + public async Task<EvaluationContext> TriggerBeforeHooksAsync(IImmutableDictionary<string, object>? hints, |
| 61 | + CancellationToken cancellationToken = default) |
| 62 | + { |
| 63 | + var evalContextBuilder = EvaluationContext.Builder(); |
| 64 | + evalContextBuilder.Merge(this._evaluationContext); |
| 65 | + |
| 66 | + for (var i = 0; i < this._hooks.Count; i++) |
| 67 | + { |
| 68 | + var hook = this._hooks[i]; |
| 69 | + var hookContext = this._hookContexts[i]; |
| 70 | + |
| 71 | + var resp = await hook.BeforeAsync(hookContext, hints, cancellationToken) |
| 72 | + .ConfigureAwait(false); |
| 73 | + if (resp != null) |
| 74 | + { |
| 75 | + evalContextBuilder.Merge(resp); |
| 76 | + this._evaluationContext = evalContextBuilder.Build(); |
| 77 | + for (var j = 0; j < this._hookContexts.Count; j++) |
| 78 | + { |
| 79 | + this._hookContexts[j] = this._hookContexts[j].WithNewEvaluationContext(this._evaluationContext); |
| 80 | + } |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + this.HookReturnedNull(hook.GetType().Name); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return this._evaluationContext; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Execute the after hooks. These are executed in opposite order of the before hooks. |
| 93 | + /// </summary> |
| 94 | + /// <param name="evaluationDetails">The evaluation details which will be provided to the hook</param> |
| 95 | + /// <param name="hints">Optional hook hints</param> |
| 96 | + /// <param name="cancellationToken">Cancellation token which can cancel hook operations</param> |
| 97 | + public async Task TriggerAfterHooksAsync(FlagEvaluationDetails<T> evaluationDetails, |
| 98 | + IImmutableDictionary<string, object>? hints, |
| 99 | + CancellationToken cancellationToken = default) |
| 100 | + { |
| 101 | + // After hooks run in reverse. |
| 102 | + for (var i = this._hooks.Count - 1; i >= 0; i--) |
| 103 | + { |
| 104 | + var hook = this._hooks[i]; |
| 105 | + var hookContext = this._hookContexts[i]; |
| 106 | + await hook.AfterAsync(hookContext, evaluationDetails, hints, cancellationToken) |
| 107 | + .ConfigureAwait(false); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Execute the error hooks. These are executed in opposite order of the before hooks. |
| 113 | + /// </summary> |
| 114 | + /// <param name="exception">Exception which triggered the error</param> |
| 115 | + /// <param name="hints">Optional hook hints</param> |
| 116 | + /// <param name="cancellationToken">Cancellation token which can cancel hook operations</param> |
| 117 | + public async Task TriggerErrorHooksAsync(Exception exception, |
| 118 | + IImmutableDictionary<string, object>? hints, CancellationToken cancellationToken = default) |
| 119 | + { |
| 120 | + // Error hooks run in reverse. |
| 121 | + for (var i = this._hooks.Count - 1; i >= 0; i--) |
| 122 | + { |
| 123 | + var hook = this._hooks[i]; |
| 124 | + var hookContext = this._hookContexts[i]; |
| 125 | + try |
| 126 | + { |
| 127 | + await hook.ErrorAsync(hookContext, exception, hints, cancellationToken) |
| 128 | + .ConfigureAwait(false); |
| 129 | + } |
| 130 | + catch (Exception e) |
| 131 | + { |
| 132 | + this.ErrorHookError(hook.GetType().Name, e); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Execute the finally hooks. These are executed in opposite order of the before hooks. |
| 139 | + /// </summary> |
| 140 | + /// <param name="evaluationDetails">The evaluation details which will be provided to the hook</param> |
| 141 | + /// <param name="hints">Optional hook hints</param> |
| 142 | + /// <param name="cancellationToken">Cancellation token which can cancel hook operations</param> |
| 143 | + public async Task TriggerFinallyHooksAsync(FlagEvaluationDetails<T> evaluationDetails, |
| 144 | + IImmutableDictionary<string, object>? hints, |
| 145 | + CancellationToken cancellationToken = default) |
| 146 | + { |
| 147 | + // Finally hooks run in reverse |
| 148 | + for (var i = this._hooks.Count - 1; i >= 0; i--) |
| 149 | + { |
| 150 | + var hook = this._hooks[i]; |
| 151 | + var hookContext = this._hookContexts[i]; |
| 152 | + try |
| 153 | + { |
| 154 | + await hook.FinallyAsync(hookContext, evaluationDetails, hints, cancellationToken) |
| 155 | + .ConfigureAwait(false); |
| 156 | + } |
| 157 | + catch (Exception e) |
| 158 | + { |
| 159 | + this.FinallyHookError(hook.GetType().Name, e); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + [LoggerMessage(100, LogLevel.Debug, "Hook {HookName} returned null, nothing to merge back into context")] |
| 165 | + partial void HookReturnedNull(string hookName); |
| 166 | + |
| 167 | + [LoggerMessage(103, LogLevel.Error, "Error while executing Error hook {HookName}")] |
| 168 | + partial void ErrorHookError(string hookName, Exception exception); |
| 169 | + |
| 170 | + [LoggerMessage(104, LogLevel.Error, "Error while executing Finally hook {HookName}")] |
| 171 | + partial void FinallyHookError(string hookName, Exception exception); |
| 172 | + } |
| 173 | +} |
0 commit comments