35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
using System.Collections.Concurrent;
|
|
using Json.Schema;
|
|
|
|
namespace NeonSprawl.Server;
|
|
|
|
/// <summary>
|
|
/// Thread-safe registration for <see cref="SchemaRegistry.Global"/> (JsonSchema.Net registry is not concurrent).
|
|
/// Used by content catalog loaders when parallel integration tests spin up multiple hosts.
|
|
/// </summary>
|
|
internal static class CatalogSchemaRegistry
|
|
{
|
|
private static readonly Lock RegisterLock = new();
|
|
private static readonly ConcurrentDictionary<string, JsonSchema> SchemasByPath =
|
|
new(StringComparer.Ordinal);
|
|
|
|
/// <summary>Parses <paramref name="schemaFilePath"/> once per process and registers it under a global lock.</summary>
|
|
public static JsonSchema GetOrRegisterFromFile(string schemaFilePath)
|
|
{
|
|
var fullPath = Path.GetFullPath(schemaFilePath);
|
|
if (SchemasByPath.TryGetValue(fullPath, out var cached))
|
|
return cached;
|
|
|
|
lock (RegisterLock)
|
|
{
|
|
if (SchemasByPath.TryGetValue(fullPath, out cached))
|
|
return cached;
|
|
|
|
var schema = JsonSchema.FromText(File.ReadAllText(fullPath));
|
|
SchemaRegistry.Global.Register(schema);
|
|
SchemasByPath[fullPath] = schema;
|
|
return schema;
|
|
}
|
|
}
|
|
}
|