Sure ,

A little context first. I am writing SNMP Trap handling framework. I have started with Net-SNMP's snmptrapd with embedded perl interpreter and I have written the first version of the framework and some heavy duty trap handlers for it. Then I realized that it leaks like hell - 1G RAM for 2-3 hours. And it is not my fault because it leaks with empty handlers a lot of memory too

The next step was to write my own trap-daemon based on SNMP_Session.pm and BER.pm. It take me one morning the daemon and the afternoon to port and tune the existing trap-handlers.

So now the architecture is quite configurable and pluggable but is modeled upon the initial implementation. It consists of one central process that at the start loads the configured modules and then fires the appropriate handlers for the received traps.

All the trap handlers inherit from one class "Trap" that makes simple decoding of the trap and manages the live-cycle of the handler. I have decided to spawn new process for each handler and to communicate via pipes with it. In this way I could get lazy forking and initialization of the handlers and to manage situations like the death of some workers. So for now I have code that goes like this:

sub new { my $type = shift; my $class = ref $type || $type; my $self = { trap => [], cfg => shift }; bless $self, $class; return sub { $self->receiver(@_) }; } sub _default_receiver { my ($self,$vars) = @_; my $fd; if ( my $pid = open($fd, "|-" )){ # Fork $fd->autoflush(); $__PACKAGE__::to_die{$pid}=$self; $self->{receiver} = sub { # Replace receiver in parent my ($self1,$v)= @_; store_fd($v,$fd); # Send }; $self->receiver($vars); # The first trap }else{ $self->init(); # Init child here while ( my $v = fd_retrieve( \*STDIN ) ) { my $r = {}; ... do some work here .... push @{$self->{trap}},$r; $self->handler(); } $self->destroy(); } }

The constructor return a closure that will be run for each received trap. The idea of the "dynamic method rewriting" is that the "receiver"(_default_receiver) will fork new process only the first time and then replace itself with "receiver" (the closure) that just feeds the created worker with data. If this process dies we deleting the "receiver", so the next time it will call the _default_receiver and will fork new process. (SIGCHLD handler not shown in the code).

This version uses the AUTOLOAD approach suggested by salva. I am not coping the code of AUOTLOAD sub that looks quite like the code he suggested

I am open to all kinds of suggestions and advises.

Thanks in advance


In reply to Re^2: replace object methods at runtime by karavelov
in thread replace object methods at runtime by karavelov

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.