in reply to Re: replace object methods at runtime
in thread replace object methods at runtime
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: replace object methods at runtime
by BrowserUk (Patriarch) on Jun 25, 2008 at 13:41 UTC | |
by karavelov (Monk) on Jun 25, 2008 at 17:53 UTC | |
|
Re^3: replace object methods at runtime
by salva (Canon) on Jun 25, 2008 at 11:12 UTC |