From 56e65d1e6fb4ee72b28b4e008b9461d5bac55b8d Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 24 Feb 2014 15:14:19 -0500 Subject: [PATCH] LP#1612771: Perl max_chunk_size additions * Added missing max_chunk_size method to AppSession * Copy API max_chunk_size value into the handler AppRequest * Fix error where no-chunking resulted in empty responses Signed-off-by: Bill Erickson Signed-off-by: Galen Charlton --- src/perl/lib/OpenSRF/AppSession.pm | 39 ++++++++++++------- src/perl/lib/OpenSRF/Application.pm | 3 +- .../lib/OpenSRF/DomainObject/oilsResponse.pm | 16 ++++++-- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/perl/lib/OpenSRF/AppSession.pm b/src/perl/lib/OpenSRF/AppSession.pm index 7454970..ba3aa54 100644 --- a/src/perl/lib/OpenSRF/AppSession.pm +++ b/src/perl/lib/OpenSRF/AppSession.pm @@ -892,6 +892,13 @@ sub max_bundle_size { return $self->{max_bundle_size}; } +sub max_chunk_size { + my $self = shift; + my $value = shift; + $self->{max_chunk_size} = $value if (defined($value)); + return $self->{max_chunk_size}; +} + sub recv_timeout { my $self = shift; my $timeout = shift; @@ -1034,27 +1041,31 @@ sub respond { my $msg = shift; return unless ($self and $self->session and !$self->complete); - my $type = 'RESULT'; my $response; if (ref($msg) && UNIVERSAL::isa($msg, 'OpenSRF::DomainObject::oilsResponse')) { $response = $msg; $type = 'STATUS' if UNIVERSAL::isa($response, 'OpenSRF::DomainObject::oilsStatus'); - } elsif ($self->max_chunk_size > 0) { # we might need to chunk - my $str = OpenSRF::Utils::JSON->perl2JSON($msg); - if (length($str) > $self->max_chunk_size) { # send partials ("chunking") - for (my $i = 0; $i < length($str); $i += $self->max_chunk_size) { - $response = new OpenSRF::DomainObject::oilsResult::Partial; - $response->content( substr($str, $i, $self->max_chunk_size) ); - $self->session->send($type, $response, $self->threadTrace); + + } else { + + if ($self->max_chunk_size > 0) { # we might need to chunk + my $str = OpenSRF::Utils::JSON->perl2JSON($msg); + if (length($str) > $self->max_chunk_size) { # send partials ("chunking") + for (my $i = 0; $i < length($str); $i += $self->max_chunk_size) { + $response = new OpenSRF::DomainObject::oilsResult::Partial; + $response->content( substr($str, $i, $self->max_chunk_size) ); + $self->session->send($type, $response, $self->threadTrace); + } + # This triggers reconstruction on the remote end + $response = new OpenSRF::DomainObject::oilsResult::PartialComplete; + return $self->session->send($type, $response, $self->threadTrace); } - # This triggers reconstruction on the remote end - $response = new OpenSRF::DomainObject::oilsResult::PartialComplete; - return $self->session->send($type, $response, $self->threadTrace); - } else { - $response = new OpenSRF::DomainObject::oilsResult; - $response->content( $msg ); } + + # message failed to exceed max chunk size OR chunking disabled + $response = new OpenSRF::DomainObject::oilsResult; + $response->content($msg); } if ($self->{max_bundle_count} > 0 or $self->{max_bundle_size} > 0) { # we are bundling, and we need to test the size or count diff --git a/src/perl/lib/OpenSRF/Application.pm b/src/perl/lib/OpenSRF/Application.pm index 9749a1d..8aec164 100644 --- a/src/perl/lib/OpenSRF/Application.pm +++ b/src/perl/lib/OpenSRF/Application.pm @@ -65,7 +65,7 @@ sub max_chunk_size { my $self = shift; return 0 unless ref($self); return $self->{max_chunk_size} if (defined($self->{max_chunk_size})); - return 2 * $self->max_bundle_size; + return 104858; # 1/10 MB } sub api_name { @@ -182,6 +182,7 @@ sub handler { my $appreq = OpenSRF::AppRequest->new( $session ); $appreq->max_bundle_size( $coderef->max_bundle_size ); $appreq->max_bundle_count( $coderef->max_bundle_count ); + $appreq->max_chunk_size( $coderef->max_chunk_size ); $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", INTERNAL ); if( $in_request ) { diff --git a/src/perl/lib/OpenSRF/DomainObject/oilsResponse.pm b/src/perl/lib/OpenSRF/DomainObject/oilsResponse.pm index 25d8f50..307eb57 100644 --- a/src/perl/lib/OpenSRF/DomainObject/oilsResponse.pm +++ b/src/perl/lib/OpenSRF/DomainObject/oilsResponse.pm @@ -13,7 +13,8 @@ BEGIN { STATUS_NOTFOUND STATUS_NOTALLOWED STATUS_TIMEOUT STATUS_INTERNALSERVERERROR STATUS_NOTIMPLEMENTED STATUS_VERSIONNOTSUPPORTED STATUS_REDIRECTED - STATUS_EXPFAILED STATUS_COMPLETE/; + STATUS_EXPFAILED STATUS_COMPLETE STATUS_PARTIAL + STATUS_NOCONTENT/; %EXPORT_TAGS = ( status => [ qw/STATUS_CONTINUE STATUS_OK STATUS_ACCEPTED @@ -21,7 +22,8 @@ BEGIN { STATUS_NOTFOUND STATUS_NOTALLOWED STATUS_TIMEOUT STATUS_INTERNALSERVERERROR STATUS_NOTIMPLEMENTED STATUS_VERSIONNOTSUPPORTED STATUS_REDIRECTED - STATUS_EXPFAILED STATUS_COMPLETE/ ], + STATUS_EXPFAILED STATUS_COMPLETE STATUS_PARTIAL + STATUS_NOCONTENT/ ], ); } @@ -284,7 +286,10 @@ package OpenSRF::DomainObject::oilsResult::Partial; use OpenSRF::DomainObject::oilsResponse qw/:status/; use base 'OpenSRF::DomainObject::oilsResult'; use vars qw/$status $statusCode/; -OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResult', name => 'OpenSRF::DomainObject::oilsResult::Partial', type => 'hash' ); +OpenSRF::Utils::JSON->register_class_hint( + hint => 'osrfResultPartial', + name => 'OpenSRF::DomainObject::oilsResult::Partial', + type => 'hash'); $status = 'Partial Response'; @@ -324,7 +329,10 @@ package OpenSRF::DomainObject::oilsResult::PartialComplete; use OpenSRF::DomainObject::oilsResponse qw/:status/; use base 'OpenSRF::DomainObject::oilsResult'; use vars qw/$status $statusCode/; -OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResult', name => 'OpenSRF::DomainObject::oilsResult::Partial', type => 'hash' ); +OpenSRF::Utils::JSON->register_class_hint( + hint => 'osrfResultPartialComplete', + name => 'OpenSRF::DomainObject::oilsResult::PartialComplete', + type => 'hash'); $status = 'Partial Response Finalized'; -- 2.43.2