Guest User

AttackCoro.pm

a guest
Jan 26th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.19 KB | None | 0 0
  1. package AttackCoro;
  2. use 5.10.1;
  3. use strict;
  4. use warnings;
  5. use base "Exporter";
  6. our @EXPORT = qw/
  7. watcher coro pool sleep
  8. /;
  9. our @EXPORT_OK = @EXPORT;
  10. use Coro;
  11. use Coro::AnyEvent;
  12. use Coro::LWP;
  13. use AnyEvent;
  14. sub sleep($)
  15. {
  16. my($s) = @_;
  17. Coro::AnyEvent::sleep($s);
  18. return;
  19. }
  20. sub watcher()
  21. {
  22. return AnyEvent->timer(
  23. interval => 2,
  24. cb => sub {
  25. my $time = time;
  26. map {
  27. say "Attack: thread #$_->{id} ('$_->{desc}') terminated."
  28. if $_->{debug};
  29. $_->cancel(undef);
  30. } grep {
  31. $_->{timeout_at} && $time >= $_->{timeout_at}
  32. } reverse Coro::State::list;
  33. },
  34. );
  35. }
  36. sub coro($$;$)
  37. {
  38. my($sub, $arg, $options_) = @_;
  39. $options_ ||= {};
  40. my $options = {
  41. debug => 0,
  42. desc => "anon",
  43. timeout => 0,
  44. eval => 0,
  45. ready => 0,
  46. join => 0,
  47. %$options_,
  48. };
  49. state $id = 1;
  50. my $coro; $coro = Coro->new(sub {
  51. $coro->{id} = $id++;
  52. $coro->{desc} = $options->{desc};
  53. $coro->{timeout_at} = time + $options->{timeout}
  54. if $options->{timeout};
  55. $coro->{debug} = $options->{debug};
  56. say "Attack: thread #$coro->{id} ('$coro->{desc}') started."
  57. if $options->{debug};
  58. my $result;
  59. if($options->{eval}) {
  60. eval { $result = $sub->($arg) };
  61. if($@ && $options->{debug}) {
  62. chomp $@;
  63. say "Attack: thread #$coro->{id} ('$coro->{desc}') died: $@";
  64. }
  65. } else {
  66. $result = $sub->($arg);
  67. }
  68. say "Attack: thread #$coro->{id} ('$coro->{desc}') finished."
  69. if $options->{debug};
  70. return $result // undef;
  71. });
  72. $coro->ready if $options->{ready} || $options->{join};
  73. return $options->{join} ? $coro->join : $coro;
  74. }
  75. sub pool($$;$)
  76. {
  77. my($sub, $args, $options_) = @_;
  78. $options_ ||= {};
  79. my $options = {
  80. debug => 0,
  81. desc => "anon",
  82. ready => 0,
  83. join => 0,
  84. limit => 0,
  85. %$options_,
  86. };
  87. say "Attack: create pool '$options->{desc}' with ".@$args." threads."
  88. if $options->{debug};
  89. my $pool = async
  90. {
  91. my @results;
  92. while(my @args_ = ($options->{limit} > 0 ? splice @$args, 0, $options->{limit} : splice @$args))
  93. {
  94. my @coros = map {
  95. coro($sub, $_, {
  96. %$options,
  97. desc => "$options->{desc} pool",
  98. ready => 0,
  99. join => 0,
  100. });
  101. } @args_;
  102. push @results, map { $_->join } map { $_->ready; $_ } @coros;
  103. }
  104. return @results;
  105. };
  106. $pool->ready if $options->{ready} || $options->{join};
  107. return $options->{join} ? $pool->join : $pool;
  108. }
  109. 2;
Advertisement
Add Comment
Please, Sign In to add comment