embedded perl, patch

Alexandr Gomoliako zzz at zzz.org.ua
Wed Oct 26 22:47:50 UTC 2011


Hello all.

I've been working on embedded perl recently and have
some ideas to consider.

1. Allow request-independent asynchronous functions,
kind of like this:

    sub handler {
        my $r = shift;

        ngx_get_something "foo", sub {
            my $something = shift;

            $r->print($something);
            ...
        };
    }

2. Remove G_EVAL from callback
Mistake in perl handler is as fatal as segfault and
nginx should just let it croak. If anyone really
wants this, they can always use "eval {}".

3. Ditch multiplicity and clean things up a bit.
I can't find any reason to support multiplilicity,
especially with asynchronous model.


So, in patch there is a simple timer, couple of request
related functions and constsubs:

    sub handler {
        my $r = shift;

        $r->main_count_inc;

        ...

        ngx_timer 1, 0, sub {
            $r->print(...);

            $r->send_special(NGX_HTTP_LAST);
            $r->finalize_request(NGX_OK);
        };

        ...

        return NGX_DONE;
    }

Here is a simple example with two timers executing
in parallel (second and third) and others sequentually:

package hello;

use nginx;

sub handler {
    my $r = shift;

    $r->log_error(0, "handler");

    $r->discard_request_body;
    $r->main_count_inc;

    ngx_timer 1, 0, sub {
        $r->log_error(0, "first timer");

        ngx_timer 1, 0, sub {
            $r->log_error(0, "second timer");
        };

        ngx_timer 1, 0, sub {
            $r->log_error(0, "third timer");

            ngx_timer 1, 0, sub {
                $r->log_error(0, "last timer, sending response");

                $r->send_http_header("text/plain");
                $r->print("hello world\n");
                $r->send_special(NGX_HTTP_LAST);
                $r->finalize_request(NGX_OK);
            };
        };
    };

    return NGX_DONE;
}

1;


Any thoughts?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: nginx-perl-pre.patch
Type: text/x-patch
Size: 9015 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20111027/b155c075/attachment.bin>


More information about the nginx-devel mailing list