Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88221db

Browse files
committed
fix for not shared variables in response callback. it fixes numerous calls to rpc.
1 parent 3b471a4 commit 88221db

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

perl/rpc_client.pl

+20-7
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,27 @@ ($)
2626
my $result = $channel->declare_queue(exclusive => 1);
2727
my $callback_queue = $result->{method_frame}->{queue};
2828

29-
sub on_response {
30-
my $var = shift;
31-
my $body = $var->{body}->{payload};
32-
if ($corr_id eq $var->{header}->{correlation_id}) {
33-
$cv->send($body);
34-
}
29+
sub on_response_cb {
30+
my %a = (
31+
condvar => undef,
32+
correlation_id => undef,
33+
@_
34+
);
35+
return sub {
36+
my $var = shift;
37+
my $body = $var->{body}->{payload};
38+
if ($a{correlation_id} eq $var->{header}->{correlation_id}) {
39+
$a{condvar}->send($body);
40+
}
41+
};
3542
}
3643

3744
$channel->consume(
3845
no_ack => 1,
39-
on_consume => \&on_response,
46+
on_consume => on_response_cb(
47+
condvar => $cv,
48+
correlation_id => $corr_id,
49+
),
4050
);
4151

4252
$channel->publish(
@@ -55,3 +65,6 @@ ($)
5565
my $response = fibonacci_rpc(30);
5666
print " [.] Got $response\n";
5767

68+
print " [x] Requesting fib(32)\n";
69+
$response = fibonacci_rpc(32);
70+
print " [.] Got $response\n";

0 commit comments

Comments
 (0)