Fix timers after suspend/resume/pause
We use timers to decide when to query and delete expired messages or when to perform signed key rotations. Internally, timers are counters that get updated when the CPU ticks, so if the CPU sleeps, the timer will stop counting, and start again after it wakes up, ignoring the intervening passage of wall clock time. To fix this, without having to query the database or other potentially high overhead operations too often, use an interval to frequently check the wall clock time. If time jumps forward, trigger a global event so other listeners can update their possibly-inaccurate timers. https://stackoverflow.com/questions/6346849/what-happens-to-settimeout-when-the-computer-goes-to-sleep https://stackoverflow.com/questions/4079115/can-any-desktop-browsers-detect-when-the-computer-resumes-from-sleep // FREEBIEpull/749/head
							parent
							
								
									31b9311f62
								
							
						
					
					
						commit
						25ee61d3cb
					
				@ -0,0 +1,25 @@
 | 
			
		||||
/*
 | 
			
		||||
 * vim: ts=4:sw=4:expandtab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
;(function () {
 | 
			
		||||
    'use strict';
 | 
			
		||||
 | 
			
		||||
    var lastTime;
 | 
			
		||||
    var interval = 1000;
 | 
			
		||||
    function checkTime() {
 | 
			
		||||
      var currentTime = Date.now();
 | 
			
		||||
      if (currentTime > (lastTime + interval * 2)) {
 | 
			
		||||
          console.log('time travel detected!');
 | 
			
		||||
          window.events.trigger('timetravel');
 | 
			
		||||
      }
 | 
			
		||||
      lastTime = currentTime;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    window.WallClockListener = {
 | 
			
		||||
      init: function() {
 | 
			
		||||
          lastTime = Date.now();
 | 
			
		||||
          setInterval(checkTime, 1000);
 | 
			
		||||
      }
 | 
			
		||||
    };
 | 
			
		||||
}());
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue