From 539899052ab759d967ab11ffd0b72f17ea674d62 Mon Sep 17 00:00:00 2001 From: erickson Date: Tue, 7 Jun 2005 21:10:50 +0000 Subject: [PATCH] Stateless code, round 1... git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@333 9efc2488-bf62-4759-914b-345cdb29e865 --- src/perlmods/OpenSRF/AppSession.pm | 77 ++++++++++++++++++------ src/perlmods/OpenSRF/DomainObject/oilsMessage.pm | 5 ++ src/perlmods/OpenSRF/System.pm | 14 ----- src/perlmods/OpenSRF/UnixServer.pm | 18 ++++-- src/perlmods/OpenSRF/Utils/SettingsClient.pm | 5 ++ 5 files changed, 83 insertions(+), 36 deletions(-) diff --git a/src/perlmods/OpenSRF/AppSession.pm b/src/perlmods/OpenSRF/AppSession.pm index 4f408ff..7ece06f 100644 --- a/src/perlmods/OpenSRF/AppSession.pm +++ b/src/perlmods/OpenSRF/AppSession.pm @@ -98,8 +98,6 @@ sub server_build { return undef unless ($sess_id and $remote_id and $service); - my $config_client = OpenSRF::Utils::SettingsClient->new(); - if ( my $thingy = $class->find($sess_id) ) { $thingy->remote_id( $remote_id ); $logger->debug( "AppSession returning existing session $sess_id", DEBUG ); @@ -113,8 +111,11 @@ sub server_build { # " Session ID [$sess_id], remote_id [$remote_id]"); $logger->debug("Attempting to build a client session as ". "a server Session ID [$sess_id], remote_id [$remote_id]", ERROR ); + return undef; } + my $config_client = OpenSRF::Utils::SettingsClient->new(); + my $stateless = $config_client->config_value("apps", $service, "stateless"); #my $max_requests = $conf->$service->max_requests; my $max_requests = $config_client->config_value("apps",$service,"max_requests"); @@ -135,6 +136,7 @@ sub server_build { max_requests => $max_requests, session_threadTrace => 0, service => $service, + stateless => $stateless, } => $class; return $_CACHE{$sess_id} = $self; @@ -193,6 +195,11 @@ sub get_app_targets { return @targets; } +sub stateless { + my $self = shift; + if($self) {return $self->{stateless};} +} + # When we're a client and we want to connect to a remote service # create( $app, username => $user, secret => $passwd ); # OR @@ -203,19 +210,30 @@ sub create { my $app = shift; + + if( my $thingy = OpenSRF::AppSession->find_client( $app ) ) { - $logger->debug( "AppSession returning existing client session for $app", DEBUG ); + $logger->debug( + "AppSession returning existing client session for $app", DEBUG ); return $thingy; } else { $logger->debug( "AppSession creating new client session for $app", DEBUG ); } + my $stateless = 0; + my $c = OpenSRF::Utils::SettingsClient->new(); + # we can get an infinite loop if we're grabbing the settings and we + # need the settings to grab the settings... + if($app ne "opensrf.settings" || $c->has_config()) { + $stateless = $c->config_value("apps", $app, "stateless"); + } my $sess_id = time . rand( $$ ); while ( $class->find($sess_id) ) { $sess_id = time . rand( $$ ); } + my ($r_id) = get_app_targets($app); my $peer_handle = OpenSRF::Transport::PeerHandle->retrieve("client"); @@ -235,6 +253,7 @@ sub create { orig_remote_id => $r_id, peer_handle => $peer_handle, session_threadTrace => 0, + stateless => $stateless, } => $class; $self->client_cache(); @@ -273,17 +292,20 @@ sub connect { if ( ref( $self ) and $self->state && $self->state == CONNECTED ) { - $logger->transport("ABC AppSession already connected", DEBUG ); + $logger->transport("AppSession already connected", DEBUG ); } else { - $logger->transport("ABC AppSession not connected, connecting..", DEBUG ); + $logger->transport("AppSession not connected, connecting..", DEBUG ); } return $self if ( ref( $self ) and $self->state && $self->state == CONNECTED ); + my $app = shift; my $api_level = shift; $api_level = 1 unless (defined $api_level); $self = $class->create($app, @_) if (!ref($self)); + + return undef unless ($self); $self->{api_level} = $api_level; @@ -388,8 +410,13 @@ sub disconnect { } } + if( $self->stateless and $self->state != CONNECTED ) { + $self->reset; + return; + } + unless( $self->state == DISCONNECTED ) { - $self->send('DISCONNECT', "") if ($self->endpoint == CLIENT);; + $self->send('DISCONNECT', "") if ($self->endpoint == CLIENT); $self->state( DISCONNECTED ); } @@ -433,7 +460,6 @@ sub send { my $self = shift; my @payload_list = @_; # this is a Domain Object - return unless ($self and $self->{peer_handle}); $logger->debug( "In send", INTERNAL ); @@ -465,8 +491,9 @@ sub send { } } - if( $msg_type eq "CONNECT" ) { $connecting++; } - + if( $msg_type eq "CONNECT" ) { + $connecting++; + } if( $payload ) { $logger->debug( "Payload is ".$payload->toString, INTERNAL ); @@ -507,16 +534,30 @@ sub send { if ($self->endpoint == CLIENT and ! $disconnect) { $self->queue_wait(0); - unless ($self->state == CONNECTED || ($self->state == CONNECTING && $connecting )) { - my $v = $self->connect(); - if( ! $v ) { - $logger->debug( "Unable to connect to remote service in AppSession::send()", ERROR ); - return undef; - } - if( ref($v) and $v->can("class") and $v->class->isa( "OpenSRF::EX" ) ) { - return $v; + + + if($self->stateless && $self->state != CONNECTED) { + $self->reset; + $logger->debug("AppSession is stateless in send", DEBUG ); + } + + if( !$self->stateless and $self->state != CONNECTED ) { + + $logger->debug( "Sending connect before request 1", INTERNAL ); + + unless (($self->state == CONNECTING && $connecting )) { + $logger->debug( "Sending connect before request 2", INTERNAL ); + my $v = $self->connect(); + if( ! $v ) { + $logger->debug( "Unable to connect to remote service in AppSession::send()", ERROR ); + return undef; + } + if( ref($v) and $v->can("class") and $v->class->isa( "OpenSRF::EX" ) ) { + return $v; + } } } + } $logger->debug( "AppSession sending doc: " . $doc->toString(), INTERNAL ); @@ -943,7 +984,7 @@ sub gather { my $self = shift; my $finish = shift; $self->wait_complete; - my $resp = $self->recv( timeout => 20 ); + my $resp = $self->recv( timeout => 60 ); if( $self->failed() ) { throw OpenSRF::EX::ERROR ($self->failed()->stringify()); diff --git a/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm b/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm index b33d5a0..f6edbc8 100644 --- a/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm +++ b/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm @@ -203,6 +203,11 @@ sub do_server { if ($session->state == $session->CONNECTING()) { + if($mtype ne "CONNECT" and $session->stateless) { + $log->debug("Got message Stateless", DEBUG); + return 1; #pass the message up the stack + } + # the transport layer thinks this is a new connection. is it? unless ($mtype eq 'CONNECT') { $log->error("Connection seems to be mangled: Got $mtype instead of CONNECT"); diff --git a/src/perlmods/OpenSRF/System.pm b/src/perlmods/OpenSRF/System.pm index 47efa0c..f932d73 100644 --- a/src/perlmods/OpenSRF/System.pm +++ b/src/perlmods/OpenSRF/System.pm @@ -90,20 +90,6 @@ sub _unixserver { sub _listener { my( $app ) = @_; - -=head - my $service = $app; - my $username = $app; - my $password = "jkjkasdf"; - my $jserver = "elroy"; - my $port = "5222"; - my $hostname = "elroy.pls-hq.org"; - my $unix_file = "/pines/var/sock/$app" . "_unix.sock"; - my $router = 'router\@elroy/router'; - return "exec(\"/pines/cvs/ILS/OpenSRF/src/forker/oils_inbound $service $username " . - "$password $jserver $port $hostname $unix_file $router\")"; -=cut - return "OpenSRF::Transport::Listener->new( '$app' )->initialize()->listen()"; } diff --git a/src/perlmods/OpenSRF/UnixServer.pm b/src/perlmods/OpenSRF/UnixServer.pm index b25e3ce..9688b82 100644 --- a/src/perlmods/OpenSRF/UnixServer.pm +++ b/src/perlmods/OpenSRF/UnixServer.pm @@ -1,7 +1,7 @@ package OpenSRF::UnixServer; use strict; use warnings; use base qw/OpenSRF/; -use OpenSRF::EX; +use OpenSRF::EX qw(:try); use OpenSRF::Utils::Logger qw(:level); use OpenSRF::Transport::PeerHandle; use OpenSRF::Application; @@ -88,15 +88,25 @@ sub process_request { if(!ref($app_session)) { $logger->transport( "Did not receive AppSession from transport handler, returning...", WARN ); - $0 =~ s/\*//g; + $0 = $orig; return; } + if($app_session->stateless and $app_session->state != $app_session->CONNECTED()){ + $logger->debug("Exiting keepalive for stateless session / orig = $orig"); + $app_session->kill_me; + $0 = $orig; + return; + } + + my $client = OpenSRF::Utils::SettingsClient->new(); my $keepalive = $client->config_value("apps", $self->app(), "keepalive"); my $req_counter = 0; - while( $app_session and $app_session->state and $app_session->state != $app_session->DISCONNECTED() and + while( $app_session and + $app_session->state and + $app_session->state != $app_session->DISCONNECTED() and $app_session->find( $app_session->session_id ) ) { @@ -115,7 +125,7 @@ sub process_request { $app_session->state( $app_session->DISCONNECTED() ); last; } - + } my $x = 0; diff --git a/src/perlmods/OpenSRF/Utils/SettingsClient.pm b/src/perlmods/OpenSRF/Utils/SettingsClient.pm index 2568172..9609904 100755 --- a/src/perlmods/OpenSRF/Utils/SettingsClient.pm +++ b/src/perlmods/OpenSRF/Utils/SettingsClient.pm @@ -19,6 +19,11 @@ sub set_cache { if(defined($val)) { $we_cache = $val; } } +sub has_config { + if($host_config) { return 1; } + return 0; +} + # ------------------------------------ # utility method for grabbing config info -- 2.11.0