git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC/PATCHv2 2/2] gitweb: Add support for FastCGI, using CGI::Fast

From
Jakub Narebski <jnareb@gmail.com>
Date
May 8, 2010, 07:59 UTC
Message-ID
<201005080959.01800.jnareb@gmail.com>
In-Reply-To
<1273236845-6523-3-git-send-email-jnareb@gmail.com>
From: Sam Vilain <sam.vilain@catalyst.net.nz>

Former run() subroutine got renamed to run_request(). The new run() subroutine can run multiple requests at once if run as FastCGI script.

To run gitweb as FastCGI script you must specify '--fastcgi' / '-f' command line option to gitweb, otherwise it runs as an ordinary CGI script.

[jn: cherry picked from 56d7d436644ab296155a697552ea1345f2701620
 in http://utsl.gen.nz/gitweb/?p=gitweb which was originally based
 on v264 (2326acfa95ac86a53804ca8eeeb482c2f9265e34) by Kay Sievers;
 updated to reflect current gitweb code]
TODO: update 'gitweb/README' and/or 'gitweb/INSTALL' files.
Signed-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
Changes since v1:
* Fix $pre_dispatch_hook -> $post_dispatch_hook typo.
* Leave DONE_GITWEB label in run_request() subroutine.  This way "HTTP
  exceptions" thrown using die_error(), such as '404 Not Found', would
  correctly end current request, instead of exiting FCGI script.
  Note that in original patch by Sam Vilain "HTTP exceptions" would
  not run $post_dispatch_hook.
 gitweb/gitweb.perl |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 52 insertions(+), 2 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 41bf992..9a3eaf5 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -1012,7 +1012,7 @@ sub dispatch {
 	$actions{$action}->();
 }
 
-sub run {
+sub run_request {
 	our $t0 = [Time::HiRes::gettimeofday()]
 		if defined $t0;
 
@@ -1036,7 +1036,57 @@ sub run {
  DONE_GITWEB:
 	1;
 }
-our $cgi = CGI->new();
+
+our $is_last_request = sub { 1 };
+our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
+our $CGI = 'CGI';
+our $cgi;
+sub evaluate_argv {
+	return unless (@ARGV);
+
+	require Getopt::Long;
+	Getopt::Long::GetOptions(
+		'fastcgi|fcgi|f' => sub {
+			require CGI::Fast;
+			our $CGI = 'CGI::Fast';
+
+			my $request_number = 0;
+			# let each child service 100 requests
+			our $is_last_request = sub { ++$request_number > 100 };
+		},
+		'nproc|n=i' => sub {
+			my ($arg, $val) = @_;
+			return unless eval { require FCGI::ProcManager; 1; };
+			my $proc_manager = FCGI::ProcManager->new({
+				n_processes => $val,
+			});
+			our $pre_listen_hook    = sub { $proc_manager->pm_manage()        };
+			our $pre_dispatch_hook  = sub { $proc_manager->pm_pre_dispatch()  };
+			our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
+		},
+	);
+}
+
+sub run {
+	evaluate_argv();
+
+	$pre_listen_hook->()
+		if $pre_listen_hook;
+
+ REQUEST:
+	while ($cgi = $CGI->new()) {
+		$pre_dispatch_hook->()
+			if $pre_dispatch_hook;
+
+		run_request();
+
+		$post_dispatch_hook->()
+			if $post_dispatch_hook;
+
+		last REQUEST if ($is_last_request->());
+	}
+}
+
 run();
 
 ## ======================================================================
-- 
1.7.0.1
Previous: Jakub NarebskiNext: Jakub Narebski
Message 4 of 30 in “gitweb: Add support for running gitweb as FastCGI script”
  1. 0/2 gitweb: Add support for running gitweb as FastCGI scriptJakub Narebski, May 7, 2010
  2. 1/2 gitweb: Put all per-connection code in run() subroutineJakub Narebski, May 7, 2010
  3. 2/2 gitweb: Add support for FastCGI, using CGI::FastJakub Narebski, May 7, 2010
  4. 2/2 gitweb: Add support for FastCGI, using CGI::FastJakub Narebski, May 8, 2010
  5. Jakub NarebskiMay 8, 2010
  6. Eric WongMay 9, 2010
  7. Ævar Arnfjörð BjarmasonMay 9, 2010
  8. Jakub NarebskiMay 9, 2010
  9. Peter VereshaginMay 9, 2010
  10. Jakub NarebskiMay 9, 2010
  11. Peter VereshaginMay 10, 2010
  12. Jakub NarebskiMay 10, 2010
  13. Peter VereshaginMay 11, 2010
  14. Petr BaudisMay 11, 2010
  15. Jakub NarebskiMay 11, 2010
  16. Peter VereshaginMay 11, 2010
  17. Jakub NarebskiMay 11, 2010
  18. Peter VereshaginMay 13, 2010
  19. Ævar Arnfjörð BjarmasonMay 13, 2010
  20. Peter VereshaginMay 14, 2010
  21. Jakub NarebskiMay 14, 2010
  22. Peter VereshaginMay 14, 2010
  23. Jakub NarebskiMay 14, 2010
  24. Jakub NarebskiMay 14, 2010
  25. Peter VereshaginMay 15, 2010
  26. Jakub NarebskiMay 15, 2010
  27. Peter VereshaginMay 16, 2010
  28. Jakub NarebskiMay 18, 2010
  29. Petr BaudisMay 16, 2010
  30. Petr BaudisMay 15, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.