Пример #1
0
        public static void AddHystrixCommand <TService>(this IServiceCollection services, IHystrixCommandGroupKey groupKey, IConfiguration config)
            where TService : class
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (groupKey == null)
            {
                throw new ArgumentNullException(nameof(groupKey));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var strategy = HystrixPlugins.OptionsStrategy;
            var dynOpts  = strategy.GetDynamicOptions(config);

            var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey(groupKey.Name);
            var commandKey    = HystrixCommandKeyDefault.AsKey(typeof(TService).Name);

            IHystrixCommandOptions opts = new HystrixCommandOptions(commandKey, null, dynOpts)
            {
                GroupKey      = groupKey,
                ThreadPoolKey = threadPoolKey
            };

            opts.ThreadPoolOptions = new HystrixThreadPoolOptions(threadPoolKey, null, dynOpts);
            services.AddTransient <TService>((p) => (TService)ActivatorUtilities.CreateInstance(p, typeof(TService), opts));
        }
        public static HystrixCommandMetrics GetInstance(IHystrixCommandKey key, IHystrixCommandGroupKey commandGroup, IHystrixThreadPoolKey threadPoolKey, IHystrixCommandOptions properties)
        {
            // attempt to retrieve from cache first
            IHystrixThreadPoolKey nonNullThreadPoolKey;

            if (threadPoolKey == null)
            {
                nonNullThreadPoolKey = HystrixThreadPoolKeyDefault.AsKey(commandGroup.Name);
            }
            else
            {
                nonNullThreadPoolKey = threadPoolKey;
            }

            return(Metrics.GetOrAddEx(key.Name, (k) => new HystrixCommandMetrics(key, commandGroup, nonNullThreadPoolKey, properties, HystrixPlugins.EventNotifier)));
        }