Skip to content

Commit 76ad720

Browse files
committed
fix: コマンドの結果が断片化してしまうのを防止する
1 parent c099ba0 commit 76ad720

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/CloudForecast/Data/Redis.pm

+20-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ sysinfo {
4747
$c->ledge_get('sysinfo') || [];
4848
};
4949

50+
sub read_result {
51+
my $client = shift;
52+
my $prefix = $client->read(1);
53+
while ($prefix !~ /\r\n/) {
54+
my $tmp = $client->read(1);
55+
die "could not retrieve whole string" if $tmp == '';
56+
$prefix = $prefix . $tmp;
57+
}
58+
my ($length, $buffer) = ( split /\r\n/, $prefix, 2 );
59+
$length =~ s/\$//g;
60+
while (length($buffer) != $length + 2) { # prefixed length terminated by CRLF
61+
my $tmp = $client->read(1);
62+
die "could not retrieve whole string" if $tmp == '';
63+
$buffer = $buffer . $tmp;
64+
}
65+
die "string is not finished CRLF" if $buffer !~ /\r\n\z/;
66+
$buffer =~ s/\r\n\z//g;
67+
return length($buffer) == $length ? $buffer : undef;
68+
}
5069

5170
fetcher {
5271
my $c = shift;
@@ -56,7 +75,7 @@ fetcher {
5675

5776
my $client = CloudForecast::TinyClient->new($host,$port,3.5);
5877
$client->write("info\r\n",1);
59-
my $raw_stats = $client->read(1);
78+
my $raw_stats = read_result($client);
6079
die "could not retrieve status from $host:$port" unless $raw_stats;
6180

6281
my %stats;

0 commit comments

Comments
 (0)