<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Fabric;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.ServiceFabric.Services.Communication.Client;
using Microsoft.AspNet.Http;
using System.Fabric.Query;
using System.Timers;
using Microsoft.Extensions.Logging;
using System.Threading;
using Newtonsoft.Json;

namespace ServiceRouter.ServiceDiscovery
{
    /// <summary>
    /// The resolver handles requests to the gateway
    /// mapping these to internal service fabric services
    /// accross the cluster and returning an http endpoint 
    /// which traffic can route too. 
    /// 
    /// Updates to the available services happen on a timer. 
    /// </summary>
    public class Resolver : IDisposable
    {
        private const int SERVICE_LOCATION_CACHE_EXPIRY_SECONDS = 30;
        private const int CACHE_REFRESH_TIME_SECONDS = 20;
        private const string DEFAULT_LISTENER_NAME = "";

        private readonly SimpleEndpointResolverClientFactory EndpointResolver = new SimpleEndpointResolverClientFactory();
        private readonly MemoryCache ServiceCache = new MemoryCache(new MemoryCacheOptions());
        private readonly MemoryCacheEntryOptions MemoryCacheEntryOptions = new MemoryCacheEntryOptions
        {
            AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(SERVICE_LOCATION_CACHE_EXPIRY_SECONDS)
        };

        private FabricClient fabClient { get; set; }
        private ILogger logger { get; set; }
        private System.Timers.Timer cacheUpdateTimer { get; set; }


        public Resolver(FabricClient fabClient, ILoggerFactory loggerFactory)
        {
            this.fabClient = fabClient;
            this.logger = loggerFactory.CreateLogger("Service Resolver");

            PopulateServiceCache();

            cacheUpdateTimer = new System.Timers.Timer(TimeSpan.FromSeconds(CACHE_REFRESH_TIME_SECONDS).TotalMilliseconds);
            cacheUpdateTimer.Elapsed += CacheUpdateTimer_UpdateServiceCache;
        }

        private void PopulateServiceCache()
        {
            DiscoverClusterServices().ContinueWith(task =>
            {
                if (task.IsCompleted && !task.IsFaulted)
                {
                    AddServicesToCache(task.Result);
                }<commit_msg>Fixed caching bug in the resolver.<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Fabric;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.ServiceFabric.Services.Communication.Client;
using Microsoft.AspNet.Http;
using System.Fabric.Query;
using System.Timers;
using Microsoft.Extensions.Logging;
using System.Threading;
using Newtonsoft.Json;

namespace ServiceRouter.ServiceDiscovery
{
    /// <summary>
    /// The resolver handles requests to the gateway
    /// mapping these to internal service fabric services
    /// accross the cluster and returning an http endpoint 
    /// which traffic can route too. 
    /// 
    /// Updates to the available services happen on a timer. 
    /// </summary>
    public class Resolver : IDisposable
    {
        private const int SERVICE_LOCATION_CACHE_EXPIRY_SECONDS = 30;
        private const int CACHE_REFRESH_TIME_SECONDS = 20;
        private const string DEFAULT_LISTENER_NAME = "";

        private readonly SimpleEndpointResolverClientFactory EndpointResolver = new SimpleEndpointResolverClientFactory();
        private readonly MemoryCache ServiceCache = new MemoryCache(new MemoryCacheOptions());
        private readonly MemoryCacheEntryOptions MemoryCacheEntryOptions = new MemoryCacheEntryOptions
        {
            AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(SERVICE_LOCATION_CACHE_EXPIRY_SECONDS)
        };

        private FabricClient fabClient { get; set; }
        private ILogger logger { get; set; }
        private System.Timers.Timer cacheUpdateTimer { get; set; }


        public Resolver(FabricClient fabClient, ILoggerFactory loggerFactory)
        {
            this.fabClient = fabClient;
            this.logger = loggerFactory.CreateLogger("Service Resolver");

            PopulateServiceCache();

            cacheUpdateTimer = new System.Timers.Timer(TimeSpan.FromSeconds(CACHE_REFRESH_TIME_SECONDS).TotalMilliseconds);
            cacheUpdateTimer.Elapsed += CacheUpdateTimer_UpdateServiceCache;
            cacheUpdateTimer.Start();
        }

        private void PopulateServiceCache()
        {
            DiscoverClusterServices().ContinueWith(task =>
            {
                if (task.IsCompleted && !task.IsFaulted)
                {
                    AddServicesToCache(task.Result);
                }
{'fraction_non_alphanumeric': 0.07280647168637212, 'fraction_numerical': 0.0016594067620825555, 'mean_word_length': 2.9818331957060282, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '824635', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1187, 'n_words': 298}