| 1 | package Myriad::Subscription::Implementation::Redis; | ||
| 2 | |||
| 3 | 1 | ▶ | use Myriad::Class extends => qw(IO::Async::Notifier), does => [ |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 'Myriad::Role::Subscription' | ||
| 5 | 1 | ]; | |
| 6 | |||
| 7 | our $VERSION = '1.001'; # VERSION | ||
| 8 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||
| 9 | |||
| 10 | 1 | ▶ | use Myriad::Util::UUID; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 1 | ▶ | use constant MAX_ALLOWED_STREAM_LENGTH => 10_000; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | has $redis; | ||
| 15 | |||
| 16 | has $client_id; | ||
| 17 | |||
| 18 | # A list of all sources that emits events to Redis | ||
| 19 | # will need to keep track of them to block them when | ||
| 20 | # the stream size is more than what we think it should be | ||
| 21 | has @emitters; | ||
| 22 | |||
| 23 | # A list of all receivers that we should read items for | ||
| 24 | has @receivers; | ||
| 25 | |||
| 26 | has $should_shutdown; | ||
| 27 | |||
| 28 | BUILD { | ||
| 29 | $client_id = Myriad::Util::UUID::uuid(); | ||
| 30 | } | ||
| 31 | |||
| 32 | method configure (%args) { | ||
| 33 | $redis = delete $args{redis} if exists $args{redis}; | ||
| 34 | $self->next::method(%args); | ||
| 35 | } | ||
| 36 | |||
| 37 | async method create_from_source (%args) { | ||
| 38 | my $src = delete $args{source} or die 'need a source'; | ||
| 39 | my $service = delete $args{service} or die 'need a service'; | ||
| 40 | |||
| 41 | my $stream = "service.subscriptions.$service/$args{channel}"; | ||
| 42 | |||
| 43 | $log->tracef('Adding subscription source %s to handler', $stream); | ||
| 44 | push @emitters, { | ||
| 45 | stream => $stream, | ||
| 46 | source => $src, | ||
| 47 | max_len => $args{max_len} // MAX_ALLOWED_STREAM_LENGTH | ||
| 48 | }; | ||
| 49 | $src->unblocked->then($self->$curry::weak(async method { | ||
| 50 | # The streams will be checked later by "check_for_overflow" to avoid unblocking the source by mistake | ||
| 51 | # we will make "check_for_overflow" aware about this stream after the service has started | ||
| 52 | await $src->map($self->$curry::weak(method ($event) { | ||
| 53 | $log->tracef('Subscription source %s adding an event: %s',$stream, $event); | ||
| 54 | return $redis->xadd( | ||
| 55 | encode_utf8($stream) => '*', | ||
| 56 | data => encode_json_utf8($event), | ||
| 57 | ); | ||
| 58 | }))->ordered_futures( | ||
| 59 | low => 100, | ||
| 60 | high => 5000, | ||
| 61 | )->completed | ||
| 62 | ->on_fail($self->$curry::weak(method { | ||
| 63 | $log->warnf("Redis XADD command failed for stream %s", $stream); | ||
| 64 | $should_shutdown->fail( | ||
| 65 | "Failed to publish subscription data for $stream - " . shift | ||
| 66 | ) unless $should_shutdown->is_ready; | ||
| 67 | })); | ||
| 68 | return; | ||
| 69 | }))->retain; | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | async method create_from_sink (%args) { | ||
| 74 | my $sink = delete $args{sink} | ||
| 75 | or die 'need a sink'; | ||
| 76 | my $remote_service = $args{from} || $args{service}; | ||
| 77 | my $stream = "service.subscriptions.$remote_service/$args{channel}"; | ||
| 78 | $log->tracef('Adding subscription sink %s to handler', $stream); | ||
| 79 | push @receivers, { | ||
| 80 | key => $stream, | ||
| 81 | sink => $sink, | ||
| 82 | group_name => $args{service}, | ||
| 83 | group => 0, | ||
| 84 | }; | ||
| 85 | } | ||
| 86 | |||
| 87 | async method start { | ||
| 88 | $should_shutdown //= $self->loop->new_future(label => 'subscription::redis::shutdown'); | ||
| 89 | $log->tracef('Starting subscription handler client_id: %s', $client_id); | ||
| 90 | await $self->create_streams; | ||
| 91 | await Future->wait_any( | ||
| 92 | $should_shutdown->without_cancel, | ||
| 93 | $self->receive_items, | ||
| 94 | $self->check_for_overflow | ||
| 95 | ); | ||
| 96 | } | ||
| 97 | |||
| 98 | async method stop { | ||
| 99 | $should_shutdown->done unless $should_shutdown->is_ready; | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | async method create_group($receiver) { | ||
| 105 | unless ($receiver->{group}) { | ||
| 106 | await $redis->create_group($receiver->{key}, $receiver->{group_name}); | ||
| 107 | $receiver->{group} = 1; | ||
| 108 | } | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | async method receive_items { | ||
| 113 | $log->tracef('Start receiving from (%d) subscription sinks', scalar(@receivers)); | ||
| 114 | while (@receivers == 0) { | ||
| 115 | $log->tracef('No receivers, waiting for a few seconds'); | ||
| 116 | await $self->loop->delay_future(after => 5); | ||
| 117 | } | ||
| 118 | |||
| 119 | await &fmap_void($self->$curry::curry(async method ($rcv) { | ||
| 120 | my $stream = $rcv->{key}; | ||
| 121 | my $sink = $rcv->{sink}; | ||
| 122 | my $group_name = $rcv->{group_name}; | ||
| 123 | |||
| 124 | while (1) { | ||
| 125 | try { | ||
| 126 | await $self->create_group($rcv); | ||
| 127 | } catch ($e) { | ||
| 128 | $log->warnf('skipped subscription on stream %s because: %s will try again', $stream, $e); | ||
| 129 | await $self->loop->delay_future(after => 5); | ||
| 130 | next; | ||
| 131 | } | ||
| 132 | await $sink->unblocked; | ||
| 133 | my @events = await $redis->read_from_stream( | ||
| 134 | stream => $stream, | ||
| 135 | group => $group_name, | ||
| 136 | client => $client_id | ||
| 137 | ); | ||
| 138 | |||
| 139 | for my $event (@events) { | ||
| 140 | try { | ||
| 141 | my $event_data = decode_json_utf8($event->{data}->[1]); | ||
| 142 | $log->tracef('Passing event: %s | from stream: %s to subscription sink: %s', $event_data, $stream, $sink->label); | ||
| 143 | $sink->source->emit({ | ||
| 144 | data => $event_data | ||
| 145 | }); | ||
| 146 | } catch($e) { | ||
| 147 | $log->tracef( | ||
| 148 | 'An error happened while decoding event data for stream %s message: %s, error: %s', | ||
| 149 | $stream, | ||
| 150 | $event->{data}, | ||
| 151 | $e | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | |||
| 155 | await $redis->ack( | ||
| 156 | $stream, | ||
| 157 | $group_name, | ||
| 158 | $event->{id} | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | }), foreach => [@receivers], concurrent => scalar @receivers); | ||
| 163 | } | ||
| 164 | |||
| 165 | async method check_for_overflow () { | ||
| 166 | $log->tracef('Start checking overflow for (%d) subscription sources', scalar(@emitters)); | ||
| 167 | while (1) { | ||
| 168 | if(@emitters) { | ||
| 169 | my $emitter = shift @emitters; | ||
| 170 | push @emitters, $emitter; | ||
| 171 | try { | ||
| 172 | my $len = await $redis->stream_length($emitter->{stream}); | ||
| 173 | if ($len >= $emitter->{max_len}) { | ||
| 174 | unless ($emitter->{source}->is_paused) { | ||
| 175 | $emitter->{source}->pause; | ||
| 176 | $log->tracef('Paused subscription source on %s, length is %s, max allowed %s', $emitter->{stream}, $len, $emitter->{max_len}); | ||
| 177 | } | ||
| 178 | await $redis->cleanup( | ||
| 179 | stream => $emitter->{stream}, | ||
| 180 | limit => $emitter->{max_len} | ||
| 181 | ); | ||
| 182 | } else { | ||
| 183 | if($emitter->{source}->is_paused) { | ||
| 184 | $emitter->{source}->resume; | ||
| 185 | $log->infof('Resumed subscription source on %s, length is %s', $emitter->{stream}, $len); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } catch ($e) { | ||
| 189 | $log->warnf('An error ocurred while trying to check on stream %s status - %s', $emitter->{stream}, $e); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | # No need to run vigorously | ||
| 194 | await $self->loop->delay_future(after => 5) | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | async method create_streams() { | ||
| 199 | await Future->needs_all(map { $redis->create_stream($_->{stream}) } @emitters); | ||
| 200 | } | ||
| 201 | |||
| 202 | 1; | ||
| 203 | |||
| 204 | __END__ | ||