summaryrefslogtreecommitdiffstats
path: root/www/index.cgi
blob: 401eb8f304ca2dd9c63d4a17ee191fb3477182e2 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl

# A very simple load balancing script:
# If more than $nlim hits in under $tlim seconds, redirect to $mirror.
#
# 2002-01-24 Björn Stenberg <bjorn@haxx.se>

# redirect is triggered by more than:
$nlim = 10; # accesses in...
$tlim = 10; # seconds
$mirror = "http://rockbox.sourceforge.net/rockbox/";

open FILE, "+<.load" or die "Can't open .load: $!";
flock FILE, LOCK_EX;
@a = <FILE>;
if ( scalar @a > $nlim ) {
    $first = shift @a;
}
else {
    $first = $a[0];
}
$now = time();
@a = ( @a, "$now\n" );
truncate FILE, 0;
seek FILE, 0, 0;
for ( @a ) {
    print FILE $_;
}
flock FILE, LOCK_UN;
close FILE;

$diff = $now - $first;
if ( $diff < $tlim ) {
    print "Location: $mirror\n\n";
}
else {
    print "Content-Type: text/html\n\n";
    open FILE, "<main.html" or die "Can't open main.html: $!\n";
    print <FILE>;
    close FILE;
}