Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for federated stitching. (#2471)
- Loading branch information
1 parent
27c0fac
commit 3d6a617
Showing
64 changed files
with
2,030 additions
and
522 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
src/HotChocolate/Core/src/Execution/Configuration/DefaultRequestExecutorOptionsMonitor.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,139 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace HotChocolate.Execution.Configuration | ||
{ | ||
internal sealed class DefaultRequestExecutorOptionsMonitor | ||
: IRequestExecutorOptionsMonitor | ||
, IDisposable | ||
{ | ||
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); | ||
private readonly IOptionsMonitor<RequestExecutorFactoryOptions> _optionsMonitor; | ||
private readonly IRequestExecutorOptionsProvider[] _optionsProviders; | ||
private readonly ConcurrentDictionary<NameString, INamedRequestExecutorFactoryOptions> | ||
_options = new ConcurrentDictionary<NameString, INamedRequestExecutorFactoryOptions>(); | ||
private readonly List<IDisposable> _disposables = new List<IDisposable>(); | ||
private readonly List<Action<RequestExecutorFactoryOptions, string>> _listeners = | ||
new List<Action<RequestExecutorFactoryOptions, string>>(); | ||
private bool _initialized; | ||
private bool _disposed; | ||
|
||
public DefaultRequestExecutorOptionsMonitor( | ||
IOptionsMonitor<RequestExecutorFactoryOptions> optionsMonitor, | ||
IEnumerable<IRequestExecutorOptionsProvider> optionsProviders) | ||
{ | ||
_optionsMonitor = optionsMonitor; | ||
_optionsProviders = optionsProviders.ToArray(); | ||
} | ||
|
||
public async ValueTask<RequestExecutorFactoryOptions> GetAsync( | ||
NameString schemaName, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
await InitializeAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
RequestExecutorFactoryOptions options = _optionsMonitor.Get(schemaName); | ||
|
||
if (_options.TryGetValue(schemaName, out INamedRequestExecutorFactoryOptions? o)) | ||
{ | ||
o.Configure(options); | ||
} | ||
|
||
return options; | ||
} | ||
|
||
private async ValueTask InitializeAsync(CancellationToken cancellationToken) | ||
{ | ||
if (!_initialized) | ||
{ | ||
await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
if (!_initialized) | ||
{ | ||
foreach (IRequestExecutorOptionsProvider provider in _optionsProviders) | ||
{ | ||
_disposables.Add(provider.OnChange(OnChange)); | ||
|
||
IEnumerable<NamedRequestExecutorFactoryOptions> allOptions = | ||
await provider.GetOptionsAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
foreach (NamedRequestExecutorFactoryOptions options in allOptions) | ||
{ | ||
_options[options.SchemaName] = options; | ||
} | ||
} | ||
|
||
_initialized = true; | ||
} | ||
|
||
_semaphore.Release(); | ||
} | ||
} | ||
|
||
public IDisposable OnChange(Action<RequestExecutorFactoryOptions, string> listener) => | ||
new Session(this, listener); | ||
|
||
private void OnChange(INamedRequestExecutorFactoryOptions changes) | ||
{ | ||
_options[changes.SchemaName] = changes; | ||
|
||
RequestExecutorFactoryOptions options = _optionsMonitor.Get(changes.SchemaName); | ||
changes.Configure(options); | ||
|
||
lock (_listeners) | ||
{ | ||
foreach (Action<RequestExecutorFactoryOptions, string> listener in _listeners) | ||
{ | ||
listener.Invoke(options, changes.SchemaName); | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_disposed) | ||
{ | ||
_semaphore.Dispose(); | ||
|
||
foreach (IDisposable disposable in _disposables) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
|
||
_disposed = true; | ||
} | ||
} | ||
|
||
private class Session : IDisposable | ||
{ | ||
private readonly DefaultRequestExecutorOptionsMonitor _monitor; | ||
private readonly Action<RequestExecutorFactoryOptions, string> _listener; | ||
|
||
public Session( | ||
DefaultRequestExecutorOptionsMonitor monitor, | ||
Action<RequestExecutorFactoryOptions, string> listener) | ||
{ | ||
lock (monitor._listeners) | ||
{ | ||
_monitor = monitor; | ||
_listener = listener; | ||
monitor._listeners.Add(listener); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
lock (_monitor._listeners) | ||
{ | ||
_monitor._listeners.Remove(_listener); | ||
} | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/HotChocolate/Core/src/Execution/Configuration/IRequestExecutorOptionsMonitor.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,32 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace HotChocolate.Execution.Configuration | ||
{ | ||
/// <summary> | ||
/// Used for notifications when <see cref="RequestExecutorFactoryOptions"/> instances change. | ||
/// </summary> | ||
public interface IRequestExecutorOptionsMonitor | ||
{ | ||
/// <summary> | ||
/// Returns a configured <see cref="RequestExecutorFactoryOptions"/> | ||
/// instance with the given name. | ||
/// </summary> | ||
ValueTask<RequestExecutorFactoryOptions> GetAsync( | ||
NameString schemaName, | ||
CancellationToken cancellationToken); | ||
|
||
/// <summary> | ||
/// Registers a listener to be called whenever a named | ||
/// <see cref="RequestExecutorFactoryOptions"/> changes. | ||
/// </summary> | ||
/// <param name="listener"> | ||
/// The action to be invoked when <see cref="RequestExecutorFactoryOptions"/> has changed. | ||
/// </param> | ||
/// <returns> | ||
/// An <see cref="IDisposable"/> which should be disposed to stop listening for changes. | ||
/// </returns> | ||
IDisposable OnChange(Action<RequestExecutorFactoryOptions, string> listener); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/HotChocolate/Core/src/Execution/Configuration/OnRequestExecutorCreatedAction.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,27 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace HotChocolate.Execution.Configuration | ||
{ | ||
public readonly struct OnRequestExecutorCreatedAction | ||
{ | ||
public OnRequestExecutorCreatedAction( | ||
Action<IRequestExecutor> action) | ||
{ | ||
Action = action ?? throw new ArgumentNullException(nameof(action)); | ||
AsyncAction = default; | ||
} | ||
|
||
public OnRequestExecutorCreatedAction( | ||
Func<IRequestExecutor, CancellationToken, ValueTask> asyncAction) | ||
{ | ||
Action = default; | ||
AsyncAction = asyncAction ?? throw new ArgumentNullException(nameof(asyncAction)); | ||
} | ||
|
||
public Action<IRequestExecutor>? Action { get; } | ||
|
||
public Func<IRequestExecutor, CancellationToken, ValueTask>? AsyncAction { get; } | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/HotChocolate/Core/src/Execution/Configuration/OnRequestExecutorEvictedAction.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,27 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace HotChocolate.Execution.Configuration | ||
{ | ||
public readonly struct OnRequestExecutorEvictedAction | ||
{ | ||
public OnRequestExecutorEvictedAction( | ||
Action<IRequestExecutor> action) | ||
{ | ||
Action = action ?? throw new ArgumentNullException(nameof(action)); | ||
AsyncAction = default; | ||
} | ||
|
||
public OnRequestExecutorEvictedAction( | ||
Func<IRequestExecutor, CancellationToken, ValueTask> asyncAction) | ||
{ | ||
Action = default; | ||
AsyncAction = asyncAction ?? throw new ArgumentNullException(nameof(asyncAction)); | ||
} | ||
|
||
public Action<IRequestExecutor>? Action { get; } | ||
|
||
public Func<IRequestExecutor, CancellationToken, ValueTask>? AsyncAction { get; } | ||
} | ||
} |
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.