Revert "LP#1709710 Count Perl chunk/bundle sizes in bytes"
authorDan Wells <dbw2@calvin.edu>
Fri, 15 Sep 2017 13:46:12 +0000 (09:46 -0400)
committerMike Rylander <miker@esilibrary.com>
Fri, 15 Sep 2017 14:48:10 +0000 (10:48 -0400)
This reverts commit be2b2645f7da0907366eb0c99b5d6c231c366b30.

"It's not necessary. we don't actually put 2+ byte chars in json strings
we publish.  we always \u-escape them. so bytes == chars for json we
publish.  So, it's not only unnecessary, but it's confusing for future
us'es."
--Mike Rylander, aka miker

Signed-off-by: Dan Wells <dbw2@calvin.edu>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
src/perl/lib/OpenSRF/AppSession.pm

index b90e52c..adcd4ab 100644 (file)
@@ -10,7 +10,6 @@ use OpenSRF::Utils::Config;
 use OpenSRF::EX;
 use OpenSRF;
 use Exporter;
-use Encode;
 use base qw/Exporter OpenSRF/;
 use Time::HiRes qw( time usleep );
 use POSIX ();
@@ -1058,8 +1057,12 @@ sub respond {
             # calculate chunk size based on an XML-escaped version of the message.
             # Example: If escaping doubles the length of the string then $ratio
             # will be 0.5 and we'll cut the chunk size for this message in half.
+            #
+            # Also, there is no need to worry about multi-byte characters at this
+            # stage, as they are already \u-escaped, so bytes==chars as far as
+            # this code is concerned.
 
-            my $raw_length = length(Encode::encode_utf8($str)); # count bytes
+            my $raw_length = length($str);
             my $escaped_length = $raw_length;
             $escaped_length += 11 * (() = ( $str =~ /"/g)); # 7 \s and &quot;
             $escaped_length += 4 * (() = ( $str =~ /&/g)); # &amp;
@@ -1072,8 +1075,7 @@ sub respond {
             }
 
             if ($raw_length > $chunk_size) { # send partials ("chunking")
-                my $num_bytes = length(Encode::encode_utf8($str));
-                for (my $i = 0; $i < $num_bytes; $i += $chunk_size) {
+                for (my $i = 0; $i < length($str); $i += $chunk_size) {
                     $response = new OpenSRF::DomainObject::oilsResult::Partial;
                     $response->content( substr($str, $i, $chunk_size) );
                     $self->session->send($type, $response, $self->threadTrace);
@@ -1091,8 +1093,7 @@ sub respond {
 
     if ($self->{max_bundle_count} > 0 or $self->{max_bundle_size} > 0) { # we are bundling, and we need to test the size or count
 
-        $self->{current_bundle_size} += length(
-            Encode::encode_utf8(OpenSRF::Utils::JSON->perl2JSON($response)));
+        $self->{current_bundle_size} += length(OpenSRF::Utils::JSON->perl2JSON($response));
         push @{$self->{current_bundle}}, $type, $response;  
         $self->{current_bundle_count}++;