Bandwidth Control

Been browsing idly, and i happened to come across the "delay pool" feature offered by Squid. It was an interesting concept, and soon i was trying to setup something similar - some kind of "download control" - using the scripting plugin. It didn't turn out to be very difficult.

Here, instead of using the nsh SAF, the set_write_filter SAF is used to get the script executed during Output stage. As blocks of data get written to the client, the script gets executed for each block. The contents of the current data block is accessible to the script as a "magic" variable, _chunk.

Anyway as far as the script's logic is concerned, it's pretty simple - keep a tab on the current download speed always, and if it ever climbs beyond a particular max limit, delay the proceedings by sleeping for a particular amount of time. Crude, yes - but it works.

#
# Bandwidth control script
#

if $total == ""
then
        #
        # Ok this is the first time we are getting called for a particular download.
        # Set download rate limit to 50 kbps (or whatever), and note the start time.
        #
        total = 0
        if test $sn.client.ip == "192.168.\*"
        then
                # Higher limit for local network!
                maxkbps = 256
        else
                # And that's for the rest...
                maxkbps = 50
        fi
        tstart = millisecondsnow
fi

if $_chunk != ""
then
        #
        # Find the length of this particular chunk being passed to the client, calculate
        # the total size transferred, the total time elapsed, and find the current download
        # rate. If it is more than the max limit which we would like to impose, then sleep
        # the required amount of time before proceeding.
        #
        len = length $_chunk
        total = expr $total + $len
        tnow = millisecondsnow
        ttaken = expr $tnow - $tstart
        if test $ttaken > 0
        then
                kbps = expr $total / $ttaken
                if test $kbps >= $maxkbps
                then
                        tnew = expr $total / $maxkbps
                        tsleep = expr $tnew - $ttaken
                        millisleep $tsleep
                fi
        fi
else
        #
        # _chunk being null indicates that the download is over, and we are being called for
        # the last time for this particular request. Print some stats and get out.
        #
        tnow = millisecondsnow
        ttaken = expr $tnow - $tstart
        if test $ttaken > 0
        then
                kbps = expr $total / $ttaken
                log inform "served $rq.reqpb.uri at  $kbps kbytes per second"
        fi
fi

The script assumes 192.168.\* to be the local network, and if the client IP matches that, a higher download speed is allowed.

And of course, the obj.conf should be configured to setup the Filter stage accordingly:


Init fn="load-modules" shlib="/someplace/libnsh.so" funcs="nsh,set_write_filter" 

< Object name="default">
..
..
Output fn="set_write_filter" script="/somewhere/bandwidth.sh" 
..
..
< / Object>

Comments:

I found this site via Google. I current run Squid, but I am considering switching to Sun Web Proxy. I very much desire to continue using delay pools. They are working quite well for me. How may I modify your script to govern a given file type? For example, I throttle media files (mp3, avi, ogg) during business hours. Please advise. Regards, Joe Kotran

Posted by Joseph Kotran on gusht 01, 2006 at 08:34 PD PDT #

Joe, as i mentioned in the swforum thread, this is just an experimental project of mine - i wouldn't recommend using it for serious purposes. but the basic functionality that you need, i think it shouldn't be a big deal to write a small plugin which does only that. what platform are you on?

Posted by motor on gusht 02, 2006 at 12:38 MD PDT #

Please check this out:

http://blogs.sun.com/roller/page/motor?entry=bandwidth_control_continued

Posted by motor on gusht 02, 2006 at 07:09 MD PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

user13334066

Search

Archives
« prill 2014
DieHënMarMërEnjPreSht
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today