/// <summary> /// Initializes a new instance of the <see cref="HystrixThreadPoolMetrics"/> class. /// </summary> /// <param name="threadPoolKey">The key of the parent thread pool.</param> /// <param name="threadPool">The <see cref="ThreadPoolExecutor"/> of the parent thread pool.</param> /// <param name="properties">The properties of the parent thread pool.</param> private HystrixThreadPoolMetrics(HystrixThreadPoolKey threadPoolKey, ThreadPoolExecutor threadPool, IHystrixThreadPoolProperties properties) { this.threadPoolKey = threadPoolKey; this.threadPool = threadPool; this.properties = properties; this.counter = new HystrixRollingNumber(properties.MetricsRollingStatisticalWindowInMilliseconds, properties.MetricsRollingStatisticalWindowBuckets); }
/// <summary> /// Initializes a new instance of the <see cref="HystrixThreadPoolDefault"/> class. /// </summary> /// <param name="threadPoolKey">The key of this thread pool.</param> /// <param name="setter">The default properties of this thread pool.</param> public HystrixThreadPoolDefault(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolPropertiesSetter setter) { this.properties = HystrixPropertiesFactory.GetThreadPoolProperties(threadPoolKey, setter); this.queue = HystrixPlugins.Instance.ConcurrencyStrategy.GetBlockingQueue(this.properties.MaxQueueSize.Get()); this.threadPool = HystrixPlugins.Instance.ConcurrencyStrategy.GetThreadPool(threadPoolKey, this.properties.CoreSize, this.properties.CoreSize, this.properties.KeepAliveTime, this.queue); this.metrics = HystrixThreadPoolMetrics.GetInstance(threadPoolKey, this.threadPool, this.properties); HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForThreadPool(threadPoolKey, this.metrics, this.properties); }
/// <summary> /// Gets the <see cref="HystrixThreadPoolMetrics"/> instance for a given <see cref="HystrixThreadPoolKey"/>. /// If no metrics exists for the specified key, a new one will be created from the specified threadPool and setter. /// </summary> /// <param name="key">Key of the tracked thread pool.</param> /// <param name="threadPool">The thread pool executor of the tracked pool.</param> /// <param name="properties">The properties of the tracked pool.</param> /// <returns>A new or an existing thread pool metrics instance of the specified key.</returns> public static HystrixThreadPoolMetrics GetInstance(HystrixThreadPoolKey key, ThreadPoolExecutor threadPool, IHystrixThreadPoolProperties properties) { return Metrics.GetOrAdd(key, w => new HystrixThreadPoolMetrics(key, threadPool, properties)); }
/// <summary> /// Default Constructor /// </summary> /// <param name="firstTask">Task to run before entering run loop.</param> /// <param name="parentThreadPoolExecutor"><see cref="ThreadPoolExecutor"/> that controls this worker</param> internal Worker(ThreadPoolExecutor parentThreadPoolExecutor, IRunnable firstTask) { FirstTask = firstTask; _parentThreadPoolExecutor = parentThreadPoolExecutor; Thread = parentThreadPoolExecutor.ThreadFactory.NewThread(this); }
/// <summary> /// Obtains and ignores the next task that the <paramref name="executor"/> /// would otherwise execute, if one is immediately available, /// and then retries execution of task <paramref name="runnable"/>, /// unless the <paramref name="executor"/> is shut down, in which /// case task <paramref name="runnable"/> is instead discarded. /// </summary> /// <param name="runnable"> /// The <see cref="IRunnable"/> task requested to be executed. /// </param> /// <param name="executor"> /// The <see cref="ThreadPoolExecutor"/> attempting to execute this /// task. /// </param> public virtual void RejectedExecution(IRunnable runnable, ThreadPoolExecutor executor) { if (executor.IsShutdown) return; IRunnable head; executor.Queue.Poll(out head); executor.Execute(runnable); }
/// <summary> /// Does nothing, which has the effect of discarding task /// <paramref name="runnable"/>. /// </summary> /// <param name="runnable"> /// The <see cref="IRunnable"/> task requested to be executed. /// </param> /// <param name="executor"> /// The <see cref="ThreadPoolExecutor"/> attempting to execute this /// task. /// </param> public virtual void RejectedExecution(IRunnable runnable, ThreadPoolExecutor executor) { }
/// <summary> /// Always throws <see cref="RejectedExecutionException"/>. /// </summary> /// <param name="runnable"> /// The <see cref="IRunnable"/> task requested to be executed. /// </param> /// <param name="executor"> /// The <see cref="ThreadPoolExecutor"/> attempting to execute this task. /// </param> /// <exception cref="RejectedExecutionException"> /// Always thrown upon execution. /// </exception> public virtual void RejectedExecution(IRunnable runnable, ThreadPoolExecutor executor) { throw new RejectedExecutionException("IRunnable: " + runnable + " rejected from execution by ThreadPoolExecutor: " + executor); }
/// <summary> /// Executes task <paramref name="runnable"/> in the caller's /// thread, unless <paramref name="executor"/> has been shut down, /// in which case the task is discarded. /// /// <param name="executor">the executor attempting to execute this task</param> /// <param name="runnable">the runnable task requested to be executed</param> /// </summary> public void RejectedExecution(IRunnable runnable, ThreadPoolExecutor executor) { if (executor.IsShutdown) return; runnable.Run(); }