/// <summary> /// Initializes a new instance of the <see cref="DefaultHealthCheckService" /> class. /// </summary> /// <param name="serviceCacheManager">The service cache manager.</param> public DefaultHealthCheckService(IServiceCacheManager serviceCacheManager) { var timeSpan = TimeSpan.FromSeconds(10); _serviceCacheManager = serviceCacheManager; _timer = new Timer(async s => { await Check(_dictionary.ToArray().Select(i => i.Value)); RemoveUnhealthyAddress(_dictionary.ToArray().Select(i => i.Value).Where(m => m.UnhealthyTimes >= 6)); }, null, timeSpan, timeSpan); //去除监控。 _serviceCacheManager.Removed += (s, e) => { Remove(e.Cache.CacheEndpoint); }; //重新监控。 _serviceCacheManager.Created += async(s, e) => { var keys = e.Cache.CacheEndpoint.Select(cacheEndpoint => { return(new ValueTuple <string, int>(cacheEndpoint.Host, cacheEndpoint.Port)); }); await Check(_dictionary.Where(i => keys.Contains(i.Key)).Select(i => i.Value)); }; //重新监控。 _serviceCacheManager.Changed += async(s, e) => { var keys = e.Cache.CacheEndpoint.Select(cacheEndpoint => { return(new ValueTuple <string, int>(cacheEndpoint.Host, cacheEndpoint.Port)); }); await Check(_dictionary.Where(i => keys.Contains(i.Key)).Select(i => i.Value)); }; }
/// <summary> /// get cache endpoint as an asynchronous operation. /// </summary> /// <param name="serviceCacheManager">The service cache manager.</param> /// <param name="cacheId">The cache identifier.</param> /// <returns>Task<IEnumerable<CacheEndpoint>>.</returns> public static async Task <IEnumerable <CacheEndpoint> > GetCacheEndpointAsync( this IServiceCacheManager serviceCacheManager, string cacheId) { var caches = await serviceCacheManager.GetCachesAsync(); return(caches.Where(p => p.CacheDescriptor.Id == cacheId).Select(p => p.CacheEndpoint).FirstOrDefault()); }
/// <summary> /// get cache descriptor as an asynchronous operation. /// </summary> /// <param name="serviceCacheManager">The service cache manager.</param> /// <returns>Task<IEnumerable<CacheDescriptor>>.</returns> public static async Task <IEnumerable <CacheDescriptor> > GetCacheDescriptorAsync( this IServiceCacheManager serviceCacheManager) { var caches = await serviceCacheManager.GetCachesAsync(); return(caches.Select(p => p.CacheDescriptor)); }
public DefaultAddressResolver(IHealthCheckService healthCheckService, ILogger <DefaultAddressResolver> logger, IServiceCacheManager serviceCacheManager) { _healthCheckService = healthCheckService; _logger = logger; _serviceCacheManager = serviceCacheManager; _serviceCacheManager.Changed += ServiceCacheManager_Removed; _serviceCacheManager.Removed += ServiceCacheManager_Removed; _serviceCacheManager.Created += ServiceCacheManager_Add; }
protected AbstractService(IServiceProvider serviceProvider) { ServiceProvider = serviceProvider; Logger = serviceProvider.GetRequiredService <ILoggerFactory>().CreateLogger(GetType()); Options = serviceProvider.GetRequiredService <IOptions <ServiceOptions> >(); if (Options.Value.EnableCache) { CacheManager = serviceProvider.GetService <IServiceCacheManager>() ?? new MemoryServiceCacheManager(); } }
public ConfigurationWatchProvider(CPlatformContainer serviceProvider, ILogger <ConfigurationWatchProvider> logger, IServiceCacheManager serviceCacheManager) { if (serviceProvider.IsRegistered <IConfigurationWatchManager>()) { serviceProvider.GetInstances <IConfigurationWatchManager>().Register(this); } _logger = logger; _cachingProvider = AppConfig.Configuration.Get <CachingProvider>(); _serviceCacheManager = serviceCacheManager; _serviceCacheManager.Changed += ServiceCacheManager_Removed; _serviceCacheManager.Removed += ServiceCacheManager_Removed; _serviceCacheManager.Created += ServiceCacheManager_Add; }
public static async Task <ServiceCache> GetAsync(this IServiceCacheManager serviceCacheManager, string cacheId) { return((await serviceCacheManager.GetCachesAsync()).SingleOrDefault(i => i.CacheDescriptor.Id == cacheId)); }