mirror of https://github.com/nealey/irc-bot
Tweak for timers with no fd activity
Turns out the timeout value for select() and friends is an upper boundary. once was being run as many as 12 times in a tight loop before a timer expired. I added a fudge factor of 0.001 seconds, and that seems to have fixed it for most cases.
This commit is contained in:
parent
eeab6fc319
commit
9f058248ba
12
dispatch.ml
12
dispatch.ml
|
@ -21,6 +21,14 @@ type t = {
|
||||||
timers : Timer.t ref;
|
timers : Timer.t ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(* select(), poll(), and epoll() treat timeout as an upper bound of time
|
||||||
|
to wait. This fudge factor helps ensure that given no FD activity,
|
||||||
|
this isn't run in a tight loop as a timer approaches. This value was
|
||||||
|
determined experimentally on a 1.25GHz G4 PPC to work most of the
|
||||||
|
time. Your mileage may vary. *)
|
||||||
|
|
||||||
|
let timeout_fudge = 0.001
|
||||||
|
|
||||||
let to_epoll = function
|
let to_epoll = function
|
||||||
| Input -> Epoll.In
|
| Input -> Epoll.In
|
||||||
| Priority -> Epoll.Priority
|
| Priority -> Epoll.Priority
|
||||||
|
@ -105,7 +113,7 @@ let once d =
|
||||||
let timeout =
|
let timeout =
|
||||||
try
|
try
|
||||||
let (time, _) = Timer.min_elt !(d.timers) in
|
let (time, _) = Timer.min_elt !(d.timers) in
|
||||||
let delta = (time -. now) in
|
let delta = (time -. now +. timeout_fudge) in
|
||||||
max delta 0.0
|
max delta 0.0
|
||||||
with Not_found ->
|
with Not_found ->
|
||||||
(-1.0)
|
(-1.0)
|
||||||
|
@ -129,5 +137,3 @@ let rec run d =
|
||||||
once d;
|
once d;
|
||||||
run d
|
run d
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
7
tests.ml
7
tests.ml
|
@ -276,9 +276,10 @@ let unit_tests =
|
||||||
assert_equal ~printer:string_of_float 0.0 !last_timer;
|
assert_equal ~printer:string_of_float 0.0 !last_timer;
|
||||||
|
|
||||||
Dispatch.modify d b [Dispatch.Input];
|
Dispatch.modify d b [Dispatch.Input];
|
||||||
while !last_timer = 0.0 do
|
Dispatch.once d;
|
||||||
Dispatch.once d
|
if (!last_timer = 0.0) then
|
||||||
done;
|
(* Give it one chance *)
|
||||||
|
Dispatch.once d;
|
||||||
assert_equal ~printer:string_of_float time !last_timer;
|
assert_equal ~printer:string_of_float time !last_timer;
|
||||||
|
|
||||||
Dispatch.modify d b [Dispatch.Input; Dispatch.Output];
|
Dispatch.modify d b [Dispatch.Input; Dispatch.Output];
|
||||||
|
|
Loading…
Reference in New Issue