throttle.pl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 3 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, see <http://www.gnu.org/licenses/>.
  16. # Limits the number of parallel Oddmuse instances to
  17. # $InstanceThrottleLimit by keeping track of the process ids in
  18. # $InstanceThrottleDir
  19. use strict;
  20. use v5.10;
  21. AddModuleDescription('throttle.pl', 'Limit Number Of Instances Running');
  22. use File::Glob ':glob';
  23. our ($q, $DataDir);
  24. our ($InstanceThrottleDir, $InstanceThrottleLimit);
  25. $InstanceThrottleDir = $DataDir."/pids"; # directory for pid files
  26. $InstanceThrottleLimit = 2; # maximum number of parallel processes
  27. *OldDoSurgeProtection = \&DoSurgeProtection;
  28. *DoSurgeProtection = \&NewDoSurgeProtection;
  29. *OldDoBrowseRequest = \&DoBrowseRequest;
  30. *DoBrowseRequest = \&NewDoBrowseRequest;
  31. sub NewDoSurgeProtection {
  32. DoInstanceThrottle();
  33. CreatePidFile();
  34. OldDoSurgeProtection();
  35. }
  36. sub NewDoBrowseRequest {
  37. OldDoBrowseRequest();
  38. RemovePidFile();
  39. }
  40. # limit the script to a maximum of $InstanceThrottleLimit instances
  41. sub DoInstanceThrottle {
  42. my @pids = Glob($InstanceThrottleDir."/*");
  43. # Go over all pids: validate each pid by sending signal 0, unlink
  44. # pidfile if pid does not exist and return 0. Count the number of
  45. # zeros (= removed files = zombies) with grep.
  46. my $zombies = grep /^0$/,
  47. (map {/(\d+)$/ and kill 0,$1 or Unlink($_) and 0} @pids);
  48. if (scalar(@pids)-$zombies >= $InstanceThrottleLimit) {
  49. ReportError(Ts('Too many instances. Only %s allowed.',
  50. $InstanceThrottleLimit),
  51. '503 Service Unavailable',
  52. undef,
  53. $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.')));
  54. }
  55. }
  56. sub CreatePidFile {
  57. CreateDir($InstanceThrottleDir);
  58. my $data = $q->request_method . ' ' . $q->url(-path_info=>1) . "\n";
  59. foreach my $param ($q->param) {
  60. next if $param eq 'pwd';
  61. $data .= "Param " . $param . "=" . $q->param($param) . "\n";
  62. }
  63. WriteStringToFile("$InstanceThrottleDir/$$", $data);
  64. }
  65. sub RemovePidFile {
  66. my $file = "$InstanceThrottleDir/$$";
  67. # not fatal
  68. Unlink($file);
  69. }