From 2bfa3f2724ba762cf0b88de2d1acf119f5ce46c2 Mon Sep 17 00:00:00 2001 From: vnugent Date: Fri, 21 Jun 2024 17:07:01 -0400 Subject: latest server changes + better validation --- .../src/FBMCacheClient.cs | 2 +- .../src/MemoryCache.cs | 2 +- .../src/MemoryCacheConfig.cs | 19 ++++------ .../src/RemoteBackedMemoryCache.cs | 2 +- .../src/VNCacheConfig.cs | 10 +++--- .../src/VnCacheClientConfig.cs | 41 ++++++++-------------- 6 files changed, 30 insertions(+), 46 deletions(-) (limited to 'plugins/VNLib.Data.Caching.Providers.VNCache') diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs index 28ccdcd..af12b32 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs @@ -112,7 +112,7 @@ namespace VNLib.Data.Caching.Providers.VNCache private FBMCacheClient(VnCacheClientConfig config, ILogProvider? debugLog, PluginBase? plugin) : base(config) { //Validate config - (config as IOnConfigValidation).Validate(); + (config as IOnConfigValidation).OnValidate(); _config = config; diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs index 0c1a2b5..f2af461 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs @@ -75,7 +75,7 @@ namespace VNLib.Data.Caching.Providers.VNCache private MemoryCache(MemoryCacheConfig config, bool isDebug, ILogProvider? log, BucketLocalManagerFactory? factory) : base(config) { //Validate config - config.Validate(); + config.OnValidate(); if (isDebug) { diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs index 5e53af7..177ba04 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Data.Caching.Providers.VNCache @@ -25,6 +25,8 @@ using System; using System.Text.Json.Serialization; +using VNLib.Plugins.Extensions.Loading.Configuration; + namespace VNLib.Data.Caching.Providers.VNCache { /// @@ -45,19 +47,12 @@ namespace VNLib.Data.Caching.Providers.VNCache public uint BucketSize { get; set; } = 5000; /// - public override void Validate() + public override void OnValidate() { - base.Validate(); - - if (TableSize == 0) - { - throw new ArgumentException("You must specify a cache bucket table size", "buckets"); - } + base.OnValidate(); - if (BucketSize == 0) - { - throw new ArgumentException("You must specify the maxium number of entires allowed in each bucket ", "bucket_size"); - } + Validate.Assert(TableSize > 0, "You must specify a number of cache buckets"); + Validate.Assert(BucketSize > 0, "You must specify a 'bucket_size'"); } } } \ No newline at end of file diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs index ddc6c4b..340d23c 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs @@ -86,7 +86,7 @@ namespace VNLib.Data.Caching.Providers.VNCache ArgumentNullException.ThrowIfNull(memCache); ArgumentNullException.ThrowIfNull(backingStore); - memCache.Validate(); + memCache.OnValidate(); /* * If no buffer factory was supplied, we can create one, but it has to be diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs index 24008b3..1911649 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs @@ -26,6 +26,7 @@ using System; using System.Text.Json.Serialization; using VNLib.Plugins.Extensions.Loading; +using VNLib.Plugins.Extensions.Loading.Configuration; namespace VNLib.Data.Caching.Providers.VNCache { @@ -91,12 +92,11 @@ namespace VNLib.Data.Caching.Providers.VNCache [JsonPropertyName("max_object_size")] public virtual uint MaxBlobSize { get; set; } = 16 * 1024; - public virtual void Validate() + public virtual void OnValidate() { - if (MaxBlobSize < 16) - { - throw new ArgumentException("You must configure a maximum object size", "max_object_size"); - } + + Validate.Range2(MaxBlobSize, 16, uint.MaxValue, "You must configure a maximum object size"); + } /// diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs index 0d6cd34..6ee410c 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs @@ -26,6 +26,8 @@ using System; using System.Linq; using System.Text.Json.Serialization; +using VNLib.Plugins.Extensions.Loading.Configuration; + namespace VNLib.Data.Caching.Providers.VNCache { /// @@ -100,40 +102,27 @@ namespace VNLib.Data.Caching.Providers.VNCache } /// - public override void Validate() + public override void OnValidate() { - base.Validate(); + base.OnValidate(); - if (!DiscoveryIntervalSeconds.HasValue || DiscoveryIntervalSeconds.Value < 1) - { - throw new ArgumentException("You must specify a discovery interval period greater than 0", "retry_interval_sec"); - } + Validate.Assert(DiscoveryIntervalSeconds.HasValue, "A discovery interval is required"); + Validate.Range(DiscoveryIntervalSeconds.Value, 1, int.MaxValue); - //Allow a 0 timeout to disable timeouts, not recommended, but allowed - if (!RequestTimeoutSeconds.HasValue || RequestTimeoutSeconds.Value < 0) - { - throw new ArgumentException("You must specify a positive integer FBM message timoeut", "request_timeout_sec"); - } + Validate.Assert(RequestTimeoutSeconds.HasValue, "A request timeout is required"); + Validate.Range(RequestTimeoutSeconds.Value, 1, int.MaxValue); - //Validate initial nodes - if (InitialNodes == null || InitialNodes.Length == 0) - { - throw new ArgumentException("You must specify at least one initial peer", "initial_peers"); - } + Validate.NotNull(InitialNodes, "You must specify at least one initial cache node to connect to"); + Validate.Assert(InitialNodes.Length > 0, "You must specify at least one initial cache node to connect to"); //Validate initial nodes foreach (Uri peer in GetInitialNodeUris()) { - if (!peer.IsAbsoluteUri) - { - throw new ArgumentException("You must specify an absolute URI for each initial node", "initial_nodes"); - } - - //Verify http connection - if (peer.Scheme != Uri.UriSchemeHttp && peer.Scheme != Uri.UriSchemeHttps) - { - throw new ArgumentException("You must specify an HTTP or HTTPS URI for each initial node", "initial_nodes"); - } + Validate.Assert(peer.IsAbsoluteUri, "You must specify an absolute URI for each initial node"); + Validate.Assert( + peer.Scheme == Uri.UriSchemeHttp || peer.Scheme == Uri.UriSchemeHttps, + message: "You must specify an HTTP or HTTPS URI for each initial node" + ); } } } -- cgit