Esempio n. 1
0
        /// <summary>
        /// This is like Push except that it will delay the sending of the message until the ms interval timesout.
        /// </summary>
        /// <param name="ms">The number of milliseconds to wait before posting a TimeoutMessageObject onto the message queue.</param>
        /// <param name="state">An object that will be attached to the TimeoutMessageObject. null is a valid value.</param>
        /// <param name="includeStackTrace">This will include a stack trace of the caller of ScheduleTimeout method.  However, since this is very CPU intensive it should be turned off unless debugging.</param>
        /// <returns>A handle to identify the timeout.  This handle can be used to stop the timeout after it's been started.  It's useful for things like watch-dogs.</returns>
        public int ScheduleTimeout(int ms, object state, bool includeStackTrace = false)
        {
            if (_guard.EnterExecute())
            {
                try
                {
                    TimeoutInfo timeout = new TimeoutInfo()
                    {
                        timeoutState = state
                    };

                    if (includeStackTrace)
                    {
                        timeout.startedAt = System.Environment.StackTrace;
                    }

                    return(_pool.ScheduleTimeout(this, timeout, ms));
                }
                finally
                {
                    _guard.ExitExecute();
                }
            }
            else
            {
                return(-1);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Schedules a timeout event to occur in a given amount of time.
        /// </summary>
        /// <param name="receiver">The interface that will receive the timeout notification</param>
        /// <param name="state">An object that will be attached to the timeout event</param>
        /// <param name="due">The number of milliseconds that will elapse before the interface is notifies</param>
        /// <returns>A handle that identifies the timeout event</returns>
        public int ScheduleTimeout(ITimeoutReceiver receiver, object state, int due)
        {
            TimeoutInfo info = new TimeoutInfo()
            {
                State     = state,
                Receiver  = receiver,
                ExpiresAt = _utcNow().AddMilliseconds(due)
            };

            lock (_timeouts)
            {
                info.ID = _nextTimeoutId++;
                if (_nextTimeoutId >= int.MaxValue)
                {
                    _nextTimeoutId = 0;
                }

                // add the timeout in order
                var index = _timeouts.BinarySearch(info, info);
                if (index < 0)
                {
                    index = ~index;
                }
                _timeouts.Insert(index, info);
            }

            _recheckTimeout.Set();

            return(info.ID);
        }
Esempio n. 3
0
        public void Set(string timeoutKey, T callback, Action onTimeout)
        {
            long        startTime = TimeUtility.GetServerMilliseconds();
            TimeoutInfo info      = new TimeoutInfo(callback, startTime, onTimeout);

            m_TimeoutInfoDict.AddOrUpdate(timeoutKey, info, info);
        }
Esempio n. 4
0
        public bool Remove(string timeoutKey)
        {
            bool        ret  = false;
            TimeoutInfo info = null;

            ret = m_TimeoutInfoDict.TryRemove(timeoutKey, out info);
            return(ret);
        }
Esempio n. 5
0
 public SafeHandleEventAndSetResultAction(ISignalBus signalBus,
                                          TaskCompletionSource <SignalResult> tcs,
                                          IEnumerable <string> eventKeys,
                                          TimeSpan timeout,
                                          Action <SafeHandleEventAndSetResultAction> removeCallback)
 {
     _locker         = new object();
     TimeoutInfo     = new TimeoutInfo(this, DateTime.UtcNow, timeout);
     Handler         = SafeHandleEventAndSetResult;
     Tcs             = tcs;
     _signalBus      = signalBus;
     _eventKeys      = eventKeys;
     _removeCallback = removeCallback;
 }
Esempio n. 6
0
        public T Get(string timeoutKey)
        {
            TimeoutInfo info = null;

            m_TimeoutInfoDict.TryGetValue(timeoutKey, out info);
            if (info != null)
            {
                return(info.Callback);
            }
            else
            {
                return(default(T));
            }
        }
Esempio n. 7
0
 public SafeHandleEventAndSetResultAction(ISignalBus signalBus,
                                          TaskCompletionSource <SignalResult> tcs,
                                          IEnumerable <string> eventKeys,
                                          TimeSpan timeout)
 {
     _locker     = new object();
     TimeoutInfo = new TimeoutInfo(this, DateTime.UtcNow, timeout);
     Handler     = (sender, args) =>
     {
         SafeHandleEventAndSetResult(args.EventKey);
     };
     Tcs        = tcs;
     _signalBus = signalBus;
     _eventKeys = eventKeys;
 }
Esempio n. 8
0
 /// <summary>
 /// Stop the current timeout
 /// </summary>
 public void Stop()
 {
     if (this.info != null)
     {
         BrokerTracing.TraceEvent(TraceEventType.Verbose, 0, "[TimeoutManager] {0} Timeout stopped", this.name);
         lock (this.lockThis)
         {
             if (this.info != null)
             {
                 this.info          = null;
                 this.lastResetTime = DateTime.Now;
                 this.timer.Change(Timeout.Infinite, Timeout.Infinite);
             }
         }
     }
 }
Esempio n. 9
0
        /// <summary>
        /// Register the timeout
        /// If there's another timeout registered before, the timeout will be replaced
        /// </summary>
        /// <param name="span">time span to trigger the timeout</param>
        /// <param name="callback">callback for the timeout</param>
        /// <param name="state">indicate the async state</param>
        public void RegisterTimeout(int span, WaitCallback callback, object state)
        {
            BrokerTracing.TraceEvent(TraceEventType.Verbose, 0, "[TimeoutManager] {2} Timeout registered: Span = {0}, State = {1}", span, state, this.name);

            // Change the timer to prevent the timer trigger when updating info
            lock (this.lockThis)
            {
                this.timer.Change(Timeout.Infinite, Timeout.Infinite);
                this.info          = new TimeoutInfo();
                this.info.Span     = span;
                this.info.Callback = callback;
                this.info.State    = state;

                this.lastResetTime = DateTime.Now;
                this.timer.Change(span, span);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Callback when timeout triggers
        /// </summary>
        /// <param name="state">indicating the state</param>
        private void Callback(object state)
        {
            BrokerTracing.TraceEvent(TraceEventType.Verbose, 0, "[TimeoutManager] {0} Timeout triggered: State = {1}", this.name, state);
            TimeoutInfo info;

            lock (this.lockThis)
            {
                info = this.info;
                if (info == null)
                {
                    return;
                }

                if (DateTime.Now.Subtract(this.lastResetTime) < TimeSpan.FromMilliseconds(info.Span * 0.9))
                {
                    // If the time last from last reset is shorter than the real timeout (info.Span), it means that the callback is raised before the reset is called and should be returned with no action
                    BrokerTracing.TraceWarning("[TimeoutManager] The timer is already reset. Do no action and return. LastResetTime = {0}", this.lastResetTime);
                    return;
                }

                // Set the callback triggering flag
                this.callbackTriggering = true;
            }

            try
            {
                info.Callback(info.State);
            }
            catch (Exception e)
            {
                BrokerTracing.TraceError("[TimeoutManager] Callback rasied exception: {0}", e);
            }
            finally
            {
                // reset the callback triggering flag
                this.callbackTriggering = false;
            }
        }
Esempio n. 11
0
        public void Tick()
        {
            long          curTime    = TimeUtility.GetServerMilliseconds();
            List <string> deleteKeys = new List <string>();

            foreach (KeyValuePair <string, TimeoutInfo> kv in m_TimeoutInfoDict)
            {
                if (kv.Value != null && curTime - kv.Value.StartTime > DefaultTimeoutMS)
                {
                    if (kv.Value.OnTimeout != null)
                    {
                        kv.Value.OnTimeout();
                    }
                    deleteKeys.Add(kv.Key);
                }
            }
            TimeoutInfo info = null;

            foreach (string key in deleteKeys)
            {
                m_TimeoutInfoDict.TryRemove(key, out info);
            }
        }
Esempio n. 12
0
 public SafeHandleEventAndSetResultAction(ISignalBus signalBus,
                                          TaskCompletionSource<SignalResult> tcs,
                                          IEnumerable<string> eventKeys,
                                          TimeSpan timeout,
                                          Action<SafeHandleEventAndSetResultAction> removeCallback)
 {
     _locker = new object();
     TimeoutInfo = new TimeoutInfo(this, DateTime.UtcNow, timeout);
     Handler = SafeHandleEventAndSetResult;
     Tcs = tcs;
     _signalBus = signalBus;
     _eventKeys = eventKeys;
     _removeCallback = removeCallback;
 }
Esempio n. 13
0
 public SafeHandleEventAndSetResultAction(ISignalBus signalBus,
                                          TaskCompletionSource<SignalResult> tcs,
                                          IEnumerable<string> eventKeys,
                                          TimeSpan timeout)
 {
     _locker = new object();
     TimeoutInfo = new TimeoutInfo(this, DateTime.UtcNow, timeout);
     Handler = (sender, args) =>
     {
         SafeHandleEventAndSetResult(args.EventKey);
     };
     Tcs = tcs;
     _signalBus = signalBus;
     _eventKeys = eventKeys;
 }