Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Limit Socket (throttle) to send specific ammount of packets every second

by kcott (Archbishop)
on Sep 22, 2016 at 08:10 UTC ( [id://1172349]=note: print w/replies, xml ) Need Help??

in reply to Limit Socket (throttle) to send specific ammount of packets every second

G'day Lucas,

Welcome to the Monastery.

Firstly, you can put long tracts of code within <readmore>...</readmore> or <spoiler>...</spoiler> tags: see "Writeup Formatting Tips" for details.

Unfortunately, without seeing the surrounding code, it's difficult to tell what additional code will be appropriate. There may well be better solutions!

The following demonstrates how you might achieve what you ask using an alarm (actually, it uses Time::HiRes's ualarm for finer granularity).

#!/usr/bin/env perl -l use strict; use warnings; use Time::HiRes qw{ualarm time}; my $timeout = 1_000_000; #microseconds my $limit = 3; my @packets = (0 .. 11); print time; { my $local_limit = $limit; local $SIG{ALRM} = sub { print time; $local_limit = $limit; ualarm $timeout; }; ualarm $timeout; while (1) { next unless $local_limit-- > 0; last unless @packets; my $line = shift @packets; ualarm 1 if $line == 6; print $line; } }

Here's a sample run:

1474530606.74812 0 1 2 1474530607.75322 3 4 5 1474530608.75744 6 1474530608.75752 7 8 9 1474530609.75927 10 11


  • I've set $limit to 3; you'll want 100 here.
  • I've used an array (@packets) instead of a file. Where I have:
    last unless @packets; my $line = shift @packets;
    You'll want something like:
    last if eof $fh; my $line = <$fh>;
    See eof.
  • The line
    ualarm 1 if $line == 6;
    is a bit of a kludge. It's intended to show what happens if you read less than $limit packets in the allotted time.

See also: "perlipc -- Perl interprocess communication".

Update: s/last unless eof/last if eof/

— Ken

Replies are listed 'Best First'.
Re^2: Limit Socket (throttle) to send specific ammount of packets every second
by Lucas Rey (Sexton) on Sep 23, 2016 at 10:18 UTC
    Thank you kcott, this is a very interesting approch. I'll try asap!!!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1172349]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-05-24 09:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found