Re: Time::RateLimit

2002-05-29 Thread Jay Soffian

 "WRW" == William R Ward  writes:

WRW> [EMAIL PROTECTED] (Fglock) writes:
>> Jay Soffian wrote:
>> >   calibrate();# takes a second or two to complete
>> 
>> You could calibrate when installing the module, maybe.

WRW> For years when calling rand() you had to remember to first call
WRW> srand() to initialize the random-number generator.  Then a few
WRW> versions of Perl ago, that requirement was removed - rand() will
WRW> automatically initialize it if it has not already been done explicitly
WRW> with srand().  You might do well to follow the example.

WRW> Also "calibrate" is a pretty generic term that could easily conflict
WRW> with a subroutine in the script that loads your module.
WRW> calibrate_rate() might be less likely to cause problems.

Good points. Turns out I don't need the calibrate function at all
though, so I'll just remove it.

j.



Re: Proposed CPAN submission - Time::RateLimit

2002-05-29 Thread Jay Soffian

 "IK" == Ilmari Karonen <[EMAIL PROTECTED]> writes:

IK> Isn't this rather needlessly complex?

Nope, not needlessly.

IK> Essentially the same results can be achieved by simply counting
IK> the (possibly inaccurate) time taken by sleep() as part of the
IK> next iteration.  Like this:

  use Time::HiRes qw(time sleep);   # optional, for subsecond precision
  my $step = 0.1;   # ten iterations per second

  my $t = time;
  while (1) {
  # do stuff...
  } continue {
  $t += $step;  # watch out for rounding errors!
  my $delay = time - $t;
  sleep $delay if $delay > 0;
  }

I think you meant $t - time;

IK> This is guaranteed to maintain the correct average rate regardless
IK> of any variations in the execution time of the loop, and will
IK> automatically compensate for any systematic errors in sleep().
IK> Non-systematic errors, of course, are unavoidable, but are
IK> guaranteed not to propagate.

Ah, but I don't want to maintain the average rate in all cases. This
code was written to limit packets being sent out over the network. If
the packet sending code falls behind significantly for some reason
(say because someone paused the process), I don't want the code to run
through the loop as quickly as possible till it brings the average
rate back up. Rather, the code gives up if it has fallen too far
behind and resets the rate (I'll make how far behind it can fall
configurable).

j.



Re: Proposed CPAN submission - Time::RateLimit

2002-05-29 Thread Jay Soffian

 "BVA" == Bruce Van Allen <[EMAIL PROTECTED]> writes:

>> I think you meant $t - time;

BVA> No, I'm fairly sure it works best the way Ilmari wrote it...

I ran his code. It didn't work. I looked at it, switched it to time - $t, 
it worked.

BVA> How about:

   use Time::HiRes qw(time sleep);   # optional, for subsecond precision
   my $step   = 0.1; # ten iterations per second
   my $max_delay  = 5.0;# trigger for reset

   my $t = time;
   while (1) {
   # do stuff...
   } continue {
   $t += $step;  # watch out for rounding errors!
   my $delay = time - $t;
   $delay > $max_delay and &reset; # exit loop to reset
   $delay > 0 and sleep $delay
   }

That has side-effects and I don't think it does the same thing.

j.