8.14
1.3.7 Interval
Source code at interval.rkt
An interval sets the minimum time in between requests from an agent.
The interval is measured from the last time a request was processed.
(struct interval ([last-active-time #:mutable])) (define (make-interval) (measure (interval 0)))
An agent can be active only once every 100 milliseconds. The interval calculates the delay required until the next request time.
(test-case: "delay calculated" (check-= (delay 0 1000) 0 0.001) (check-= (delay 1000 1000) 0.1 0.001) (check-= (delay 1000 1050) 0.05 0.001) (check-= (delay 1000 1100) 0 0.001))
The delay is calculated as the difference from now to the last active time plus 100 milliseconds. The return value is in seconds.
(define interval-milliseconds 100.0)
(define (delay last-active-time now) (/ (max (+ last-active-time interval-milliseconds (- now)) 0.0) 1000.0))
The interval uses the current-inexact-milliseconds function to measure the delay from the last active time. The last active time is updated.
(define ((measure interval)) (sleep (delay (interval-last-active-time interval) (current-inexact-milliseconds))) (set-interval-last-active-time! interval (current-inexact-milliseconds)))