Welcome! Log In Create A New Profile

Advanced

Re[4]: Reading from a named pipe / fifo

Max
January 21, 2012 08:56PM
21 января 2012, 21:41 от "takigama" <nginx-forum@nginx.us>:
>
> Personally, the way i'd do it is to write a bit of php (or python, perl,
> etc) called via fast cgi [...]

Thanks for the suggestion, but using FastCGI is not an option for
many reasons. The most elegant solution I could come up with
without hacking nginx involves embedded Perl like this:

===<FIFOReader.pm>===
use nginx;

sub handler {
my $r = shift;
alarm 5;
open(FIFO, "</path/to/fifo") or return 444;
while (<FIFO>) {
$r->print($_);
}
$r->rflush();
close(FIFO);
return OK;
}
===</FIFOReader.pm>===

===<nginx.conf>===
http {
perl_modules perl.modules/;
perl_require FIFOReader.pm;

server {
location ~ ^/readfifo {
ssi on;
perl FIFOReader::handler;
}
}
}
===</nginx.conf>===

===<readfifo>===
<!-- # perl sub="FIFOReader::handler" -->
===</readfifo>===

FIFOReader.pm is located in $nginx_conf_dir/perl.modules/
readfifo is located in $document_root/

In the FIFOReader Perl module I first set the alarm to allow
the blocking open() call 5 seconds to open the FIFO. After
5 seconds the blocking open() call gets interrupted and
nginx returns code 444, which tears down the connection
without sending any kind of response code, so there is
no need to check for response codes - the client gets
either nothing or the data from the FIFO.

If the blocking open() call succeeds before the timeout,
the alarm is turned off and the data is read from
the FIFO and passed on to the client, after which the
FIFO is closed and the OK code is returned by nginx.

There is no additional alarm to prevent read timeouts
(before while (<FIFO>)) because that is left up to the
client by design.

Another side benefit of this approach is that it doesn't
require additional processes (such as FastCGI wrappers),
so there is no need to supervise additional processes to
make sure they're around. The embedded Perl module
will work for as long as nginx is running.

I have started working on a FIFO module to allow per-location
FIFO opening and reading. If I get it to production quality I'll
post my patches to the list.

Max
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Reading from a named pipe / fifo

Max January 20, 2012 04:38PM

Re: Reading from a named pipe / fifo

Brian Akins January 20, 2012 05:22PM

Re[2]: Reading from a named pipe / fifo

Max January 21, 2012 11:30AM

Re: Re[2]: Reading from a named pipe / fifo

takigama January 21, 2012 12:40PM

Re[4]: Reading from a named pipe / fifo

Max January 21, 2012 08:56PM

Re[4]: Reading from a named pipe / fifo (corrected)

Max January 21, 2012 08:58PM

Re[4]: Reading from a named pipe / fifo (final version)

Max January 21, 2012 09:06PM

Re: Reading from a named pipe / fifo (final version)

Brian Akins January 21, 2012 09:28PM

Re[2]: Reading from a named pipe / fifo (final version)

Max January 21, 2012 10:38PM

Re: Reading from a named pipe / fifo (final version)

Brian Akins January 22, 2012 03:52PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 250
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready