throttle.pl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (C) 2004, 2006 Alex Schroeder <alex@emacswiki.org>
  2. # 2004 Sebastian Blatt <sblatt@havens.de>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the
  16. # Free Software Foundation, Inc.
  17. # 59 Temple Place, Suite 330
  18. # Boston, MA 02111-1307 USA
  19. # Limits the number of parallel Oddmuse instances to
  20. # $InstanceThrottleLimit by keeping track of the process ids in
  21. # $InstanceThrottleDir
  22. use strict;
  23. use v5.10;
  24. AddModuleDescription('throttle.pl', 'Limit Number Of Instances Running');
  25. use File::Glob ':glob';
  26. our ($q, $DataDir);
  27. our ($InstanceThrottleDir, $InstanceThrottleLimit);
  28. $InstanceThrottleDir = $DataDir."/pids"; # directory for pid files
  29. $InstanceThrottleLimit = 2; # maximum number of parallel processes
  30. *OldDoSurgeProtection = \&DoSurgeProtection;
  31. *DoSurgeProtection = \&NewDoSurgeProtection;
  32. *OldDoBrowseRequest = \&DoBrowseRequest;
  33. *DoBrowseRequest = \&NewDoBrowseRequest;
  34. sub NewDoSurgeProtection {
  35. DoInstanceThrottle();
  36. CreatePidFile();
  37. OldDoSurgeProtection();
  38. }
  39. sub NewDoBrowseRequest {
  40. OldDoBrowseRequest();
  41. RemovePidFile();
  42. }
  43. # limit the script to a maximum of $InstanceThrottleLimit instances
  44. sub DoInstanceThrottle {
  45. my @pids = Glob($InstanceThrottleDir."/*");
  46. # Go over all pids: validate each pid by sending signal 0, unlink
  47. # pidfile if pid does not exist and return 0. Count the number of
  48. # zeros (= removed files = zombies) with grep.
  49. my $zombies = grep /^0$/,
  50. (map {/(\d+)$/ and kill 0,$1 or Unlink($_) and 0} @pids);
  51. if (scalar(@pids)-$zombies >= $InstanceThrottleLimit) {
  52. ReportError(Ts('Too many instances. Only %s allowed.',
  53. $InstanceThrottleLimit),
  54. '503 Service Unavailable',
  55. undef,
  56. $q->p(T('Please try again later. Perhaps somebody is running maintenance or doing a long search. Unfortunately the site has limited resources, and so we must ask you for a bit of patience.')));
  57. }
  58. }
  59. sub CreatePidFile {
  60. CreateDir($InstanceThrottleDir);
  61. my $data = $q->request_method . ' ' . $q->url(-path_info=>1) . "\n";
  62. foreach my $param ($q->param) {
  63. next if $param eq 'pwd';
  64. $data .= "Param " . $param . "=" . $q->param($param) . "\n";
  65. }
  66. WriteStringToFile("$InstanceThrottleDir/$$", $data);
  67. }
  68. sub RemovePidFile {
  69. my $file = "$InstanceThrottleDir/$$";
  70. # not fatal
  71. Unlink($file);
  72. }