Class: Async::Deadline

Inherits:
Object
  • Object
show all
Defined in:
lib/async/deadline.rb

Overview

Represents a deadline timeout with decrementing remaining time. Includes an efficient representation for zero (non-blocking) timeouts.

Defined Under Namespace

Modules: Zero

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remaining) ⇒ Deadline

Create a new deadline with the specified remaining time.



45
46
47
48
# File 'lib/async/deadline.rb', line 45

def initialize(remaining)
	@remaining = remaining
	@start = Clock.now
end

Class Method Details

.start(timeout) ⇒ Object

Create a deadline for the given timeout.



33
34
35
36
37
38
39
40
41
# File 'lib/async/deadline.rb', line 33

def self.start(timeout)
	if timeout.nil?
		nil
	elsif timeout <= 0
		Zero
	else
		self.new(timeout)
	end
end

Instance Method Details

#expired?Boolean

Check if the deadline has expired.

Returns:

  • (Boolean)


66
67
68
# File 'lib/async/deadline.rb', line 66

def expired?
	self.remaining <= 0
end

#remainingObject

Get the remaining time, updating internal state. Each call to this method advances the internal clock and reduces the remaining time by the elapsed duration since the last call.



54
55
56
57
58
59
60
61
62
# File 'lib/async/deadline.rb', line 54

def remaining
	now = Clock.now
	delta = now - @start
	@start = now
	
	@remaining -= delta
	
	return @remaining
end