|
| 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 Microsoft.Extensions.Logging.Abstractions; |
| 8 | +using OpenFeature.Model; |
| 9 | + |
| 10 | +namespace OpenFeature; |
| 11 | + |
| 12 | +internal partial class HookRunner<T> |
| 13 | +{ |
| 14 | + private readonly ImmutableList<Hook> _hooks; |
| 15 | + |
| 16 | + private readonly List<HookContext<T>> _hookContexts; |
| 17 | + |
| 18 | + private EvaluationContext _evaluationContext; |
| 19 | + |
| 20 | + private readonly ILogger _logger; |
| 21 | + |
| 22 | + public HookRunner(ImmutableList<Hook>? hooks, EvaluationContext evaluationContext, |
| 23 | + SharedHookContext<T> sharedHookContext, |
| 24 | + ILogger? logger = null) |
| 25 | + { |
| 26 | + this._evaluationContext = evaluationContext; |
| 27 | + this._logger = logger ?? NullLogger<FeatureClient>.Instance; |
| 28 | + this._hooks = hooks ?? throw new ArgumentNullException(nameof(hooks)); |
| 29 | + this._hookContexts = new List<HookContext<T>>(hooks.Count); |
| 30 | + for (var i = 0; i < hooks.Count; i++) |
| 31 | + { |
| 32 | + this._hookContexts.Add(sharedHookContext.ToHookContext(evaluationContext)); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public async Task<EvaluationContext> TriggerBeforeHooksAsync(FlagEvaluationOptions? options, |
| 37 | + CancellationToken cancellationToken = default) |
| 38 | + { |
| 39 | + var evalContextBuilder = EvaluationContext.Builder(); |
| 40 | + evalContextBuilder.Merge(this._evaluationContext); |
| 41 | + |
| 42 | + for (var i = 0; i < this._hooks.Count; i++) |
| 43 | + { |
| 44 | + var hook = this._hooks[i]; |
| 45 | + var hookContext = this._hookContexts[i]; |
| 46 | + |
| 47 | + var resp = await hook.BeforeAsync(hookContext, options?.HookHints, cancellationToken).ConfigureAwait(false); |
| 48 | + if (resp != null) |
| 49 | + { |
| 50 | + evalContextBuilder.Merge(resp); |
| 51 | + this._evaluationContext = evalContextBuilder.Build(); |
| 52 | + for (var j = 0; j < this._hookContexts.Count; j++) |
| 53 | + { |
| 54 | + this._hookContexts[j] = this._hookContexts[j].WithNewEvaluationContext(this._evaluationContext); |
| 55 | + } |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + this.HookReturnedNull(hook.GetType().Name); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return this._evaluationContext; |
| 64 | + } |
| 65 | + |
| 66 | + public async Task TriggerAfterHooksAsync(FlagEvaluationDetails<T> evaluationDetails, |
| 67 | + FlagEvaluationOptions? options, |
| 68 | + CancellationToken cancellationToken = default) |
| 69 | + { |
| 70 | + // After hooks run in reverse. |
| 71 | + for (var i = this._hooks.Count - 1; i >= 0; i--) |
| 72 | + { |
| 73 | + var hook = this._hooks[i]; |
| 74 | + var hookContext = this._hookContexts[i]; |
| 75 | + await hook.AfterAsync(hookContext, evaluationDetails, options?.HookHints, cancellationToken) |
| 76 | + .ConfigureAwait(false); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public async Task TriggerErrorHooksAsync(Exception exception, |
| 81 | + FlagEvaluationOptions? options, CancellationToken cancellationToken = default) |
| 82 | + { |
| 83 | + // Error hooks run in reverse. |
| 84 | + for (var i = this._hooks.Count - 1; i >= 0; i--) |
| 85 | + { |
| 86 | + var hook = this._hooks[i]; |
| 87 | + var hookContext = this._hookContexts[i]; |
| 88 | + try |
| 89 | + { |
| 90 | + await hook.ErrorAsync(hookContext, exception, options?.HookHints, cancellationToken) |
| 91 | + .ConfigureAwait(false); |
| 92 | + } |
| 93 | + catch (Exception e) |
| 94 | + { |
| 95 | + this.ErrorHookError(hook.GetType().Name, e); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public async Task TriggerFinallyHooksAsync(FlagEvaluationDetails<T> evaluation, FlagEvaluationOptions? options, |
| 101 | + CancellationToken cancellationToken = default) |
| 102 | + { |
| 103 | + // Finally hooks run in reverse |
| 104 | + for (var i = this._hooks.Count - 1; i >= 0; i--) |
| 105 | + { |
| 106 | + var hook = this._hooks[i]; |
| 107 | + var hookContext = this._hookContexts[i]; |
| 108 | + try |
| 109 | + { |
| 110 | + await hook.FinallyAsync(hookContext, evaluation, options?.HookHints, cancellationToken) |
| 111 | + .ConfigureAwait(false); |
| 112 | + } |
| 113 | + catch (Exception e) |
| 114 | + { |
| 115 | + this.FinallyHookError(hook.GetType().Name, e); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + [LoggerMessage(100, LogLevel.Debug, "Hook {HookName} returned null, nothing to merge back into context")] |
| 121 | + partial void HookReturnedNull(string hookName); |
| 122 | + |
| 123 | + [LoggerMessage(103, LogLevel.Error, "Error while executing Error hook {HookName}")] |
| 124 | + partial void ErrorHookError(string hookName, Exception exception); |
| 125 | + |
| 126 | + [LoggerMessage(104, LogLevel.Error, "Error while executing Finally hook {HookName}")] |
| 127 | + partial void FinallyHookError(string hookName, Exception exception); |
| 128 | +} |
0 commit comments