Example #1
0
        public async Task <ConsulState> GetDependenciesAsync(ConsulDependencies dependencies)
        {
            var serviceTasks      = dependencies.Services.Select(GetServiceAsync).ToArray();
            var keyTasks          = dependencies.Keys.Select(GetKeyAsync).ToArray();
            var keyRecursiveTasks = dependencies.KeyPrefixes.Select(GetKeyRecursiveAsync).ToArray();

            await Task.WhenAll(serviceTasks.Cast <Task>().Concat(keyTasks).Concat(keyRecursiveTasks)).ConfigureAwait(false);

            var services = serviceTasks.Select(t => t.Result)
                           .Where(s => s != null)
                           .ToImmutableDictionary(s => s.Name);

            var keys = new KeyValueStore(keyTasks
                                         .Select(t => t.Result)
                                         .Where(k => k != null)
                                         .Concat(keyRecursiveTasks.SelectMany(t => t.Result)));

            var missingKeyPrefixes = dependencies.KeyPrefixes
                                     .Where(prefix => !keys.ContainsKeyStartingWith(prefix))
                                     .ToImmutableHashSet();

            return(new ConsulState(services, keys, missingKeyPrefixes));
        }
Example #2
0
        public bool TryUpdate(IEnumerable <KeyValueNode> kvNodes, out KeyValueStore updatedStore)
        {
            bool atLeastOneUpdate = false;
            var  leaves           = _leaves;

            foreach (var kvNode in kvNodes)
            {
                if (leaves.TryGetValue(kvNode.FullKey, out var existingNode) && existingNode.Equals(kvNode))
                {
                    continue;
                }
                atLeastOneUpdate = true;
                leaves           = leaves.SetItem(kvNode.FullKey, kvNode);
            }

            if (atLeastOneUpdate)
            {
                updatedStore = new KeyValueStore(leaves);
                return(true);
            }

            updatedStore = null;
            return(false);
        }