Björn Stenberg | d6023a7 | 2002-03-27 11:21:50 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # A very simple load balancing script: |
| 4 | # If more than $nlim hits in under $tlim seconds, redirect to $mirror. |
| 5 | # |
| 6 | # 2002-01-24 Björn Stenberg <bjorn@haxx.se> |
| 7 | |
| 8 | # redirect is triggered by more than: |
| 9 | $nlim = 10; # accesses in... |
| 10 | $tlim = 10; # seconds |
Björn Stenberg | 3cb3c75 | 2002-03-28 13:38:40 +0000 | [diff] [blame] | 11 | $mirror = "http://rockbox.sourceforge.net/rockbox/"; |
Björn Stenberg | d6023a7 | 2002-03-27 11:21:50 +0000 | [diff] [blame] | 12 | |
| 13 | open FILE, "+<.load" or die "Can't open .load: $!"; |
| 14 | flock FILE, LOCK_EX; |
| 15 | @a = <FILE>; |
| 16 | if ( scalar @a > $nlim ) { |
| 17 | $first = shift @a; |
| 18 | } |
| 19 | else { |
| 20 | $first = $a[0]; |
| 21 | } |
| 22 | $now = time(); |
| 23 | @a = ( @a, "$now\n" ); |
| 24 | truncate FILE, 0; |
| 25 | seek FILE, 0, 0; |
| 26 | for ( @a ) { |
| 27 | print FILE $_; |
| 28 | } |
| 29 | flock FILE, LOCK_UN; |
| 30 | close FILE; |
| 31 | |
| 32 | $diff = $now - $first; |
| 33 | if ( $diff < $tlim ) { |
| 34 | print "Location: $mirror\n\n"; |
| 35 | } |
| 36 | else { |
| 37 | print "Content-Type: text/html\n\n"; |
| 38 | open FILE, "<main.html" or die "Can't open main.html: $!\n"; |
| 39 | print <FILE>; |
| 40 | close FILE; |
| 41 | } |