« lib/Myriad/Storage/Implementation/Redis.pm
↑ Summary
lib/Myriad/Subscription/Implementation/Redis.pm »
| 1 | package Myriad::Subscription::Implementation::Memory; | ||
| 2 | |||
| 3 | 1 | ▶ | use Myriad::Class extends => 'IO::Async::Notifier', does => [ 'Myriad::Role::Subscription', 'Myriad::Util::Defer' ]; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.001'; # VERSION | ||
| 6 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||
| 7 | |||
| 8 | has $transport; | ||
| 9 | |||
| 10 | has $receivers; | ||
| 11 | |||
| 12 | has $should_shutdown = 0; | ||
| 13 | has $stopped; | ||
| 14 | |||
| 15 | # FIXME Need to update :Defer for Object::Pad | ||
| 16 | sub MODIFY_CODE_ATTRIBUTES { } | ||
| 17 | |||
| 18 | BUILD { | ||
| 19 | $receivers = []; | ||
| 20 | } | ||
| 21 | |||
| 22 | method receivers () { $receivers } | ||
| 23 | |||
| 24 | method _add_to_loop ($loop) { | ||
| 25 | $stopped = $loop->new_future(label => 'subscription::redis::stopped'); | ||
| 26 | } | ||
| 27 | |||
| 28 | method configure (%args) { | ||
| 29 | $transport = delete $args{transport} if $args{transport}; | ||
| 30 | $self->next::method(%args); | ||
| 31 | } | ||
| 32 | |||
| 33 | async method create_from_source (%args) { | ||
| 34 | my $src = delete $args{source} or die 'need a source'; | ||
| 35 | my $service = delete $args{service} or die 'need a service'; | ||
| 36 | my $channel_name = $service . '.' . $args{channel}; | ||
| 37 | await $transport->create_stream($channel_name); | ||
| 38 | |||
| 39 | $src->map(async sub { | ||
| 40 | my $message = shift; | ||
| 41 | await $transport->add_to_stream( | ||
| 42 | $channel_name, | ||
| 43 | $message->%* | ||
| 44 | ); | ||
| 45 | })->resolve->completed->retain; | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | async method create_from_sink (%args) { | ||
| 50 | my $sink = delete $args{sink} or die 'need a sink'; | ||
| 51 | my $remote_service = $args{from} || $args{service}; | ||
| 52 | my $service_name = $args{service}; | ||
| 53 | my $channel_name = $remote_service . '.' . $args{channel}; | ||
| 54 | |||
| 55 | push $receivers->@*, { | ||
| 56 | channel => $channel_name, | ||
| 57 | sink => $sink, | ||
| 58 | group_name => $service_name, | ||
| 59 | group => 0 | ||
| 60 | }; | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | async method create_group ($subscription) { | ||
| 65 | return if $subscription->{group}; | ||
| 66 | await $transport->create_consumer_group($subscription->{channel}, $subscription->{group_name}, 0, 0); | ||
| 67 | $subscription->{group} = 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | async method start { | ||
| 71 | while (1) { | ||
| 72 | await &fmap_void($self->$curry::curry(async method ($subscription) { | ||
| 73 | await $self->create_group($subscription); | ||
| 74 | try { | ||
| 75 | $log->tracef('Sink blocked state: %s', $subscription->{sink}->unblocked->state); | ||
| 76 | await Future->wait_any( | ||
| 77 | $self->loop->timeout_future(after => 0.5), | ||
| 78 | $subscription->{sink}->unblocked, | ||
| 79 | ); | ||
| 80 | } catch { | ||
| 81 | $log->tracef("skipped stream %s because sink is blocked", $subscription->{channel}); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | my $messages = await $transport->read_from_stream_by_consumer( | ||
| 86 | $subscription->{channel}, | ||
| 87 | $subscription->{group_name}, | ||
| 88 | 'consumer' | ||
| 89 | ); | ||
| 90 | for my $event_id (sort keys $messages->%*) { | ||
| 91 | $subscription->{sink}->emit($messages->{$event_id}); | ||
| 92 | await $transport->ack_message($subscription->{channel}, $subscription->{group_name}, $event_id); | ||
| 93 | } | ||
| 94 | |||
| 95 | if($should_shutdown) { | ||
| 96 | $stopped->done; | ||
| 97 | last; | ||
| 98 | } | ||
| 99 | }), foreach => [ $receivers->@* ], concurrent => 8); | ||
| 100 | await $self->loop->delay_future(after => 0.1); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | async method stop { | ||
| 105 | $should_shutdown = 1; | ||
| 106 | await $stopped; | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ | ||