public RequestInstance GetInstanceToExecute(RequestInstance requestInstance) { ThreadCounts availableCounts = _threading.GetAvailableThreads(); ThreadCounts activeCounts = _maxThreads.Subtract(availableCounts); int active = activeCounts.Greatest(); if (_count == 0 && active < _activeThreadsBeforeRemoteRequestsQueue) { return(requestInstance); } bool isLocal = requestInstance.IsLocal; if (_count >= _queueLengthBeforeIncomingRequestsRejected) { requestInstance.Reject(); return(null); } requestInstance.Defer(); QueueInstance(requestInstance, isLocal); if (active < _activeThreadsBeforeRemoteRequestsQueue) { return(DequeueInstance(false)); } if (active < _activeThreadsBeforeLocalRequestsQueue) { return(DequeueInstance(true)); } ScheduleExecuteIfNeeded(); return(null); }
private void ExecuteIfNeeded(object state) { Interlocked.Decrement(ref _scheduled); if (_stopping) { return; } if (_count == 0) { return; } ThreadCounts available = _threading.GetAvailableThreads(); int active = _maxThreads.WorkerThreads - available.WorkerThreads; if (active >= _activeThreadsBeforeLocalRequestsQueue) { return; } bool localOnly = active >= _activeThreadsBeforeRemoteRequestsQueue; RequestInstance instance = DequeueInstance(localOnly); if (instance == null) { return; } ScheduleExecuteIfNeeded(); instance.Execute(); }
public TestTheadingServices() { MaxThreads = new ThreadCounts(short.MaxValue, 1000); MinThreads = new ThreadCounts(8, 4); AvailableThreads = MaxThreads; Callbacks = new List<Tuple<WaitCallback, object>>(); Timers = new List<Tuple<TimeSpan, Action>>(); }
public RequestQueue(ThrottlingOptions options) { if (options == null) { throw new ArgumentNullException("options"); } _threading = options.ThreadingServices; _activeThreadsBeforeLocalRequestsQueue = options.ActiveThreadsBeforeLocalRequestsQueue; _activeThreadsBeforeRemoteRequestsQueue = options.ActiveThreadsBeforeRemoteRequestsQueue; _queueLengthBeforeIncomingRequestsRejected = options.QueueLengthBeforeIncomingRequestsRejected; _maxThreads = _threading.GetMaxThreads(); _executeIfNeeded = ExecuteIfNeeded; }
private void ScheduleExecuteIfNeeded() { if (_stopping) { return; } if (_count == 0) { return; } if (_scheduled >= 2) { return; } ThreadCounts available = _threading.GetAvailableThreads(); int active = _maxThreads.WorkerThreads - available.WorkerThreads; if (active >= _activeThreadsBeforeLocalRequestsQueue) { return; } Interlocked.Increment(ref _scheduled); _threading.QueueCallback(_executeIfNeeded, null); }