| 1 | package Myriad::Transport::Memory; | ||
| 2 | |||
| 3 | our $VERSION = '1.001'; # VERSION | ||
| 4 | # AUTHORTIY | ||
| 5 | |||
| 6 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Myriad::Transport::Memory - In-Memory data layer that mimics Redis behaviour.. | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | my $transport = Myriad::Transport::Memory->new(); | ||
| 16 | |||
| 17 | $transport->publish('channel_name', 'event...'); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | 5 | ▶ | use Ryu::Async; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | |||
| 25 | 5 | ▶ | use Myriad::Class extends => qw(IO::Async::Notifier); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 5 | ▶ | use Myriad::Exception::Builder category => 'memory_transport'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | |||
| 28 | =head1 Exceptions | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | =head2 StreamNotFound | ||
| 33 | |||
| 34 | Thrown when the operation requires the stream to be | ||
| 35 | created beforehand but the stream is not currently available. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | declare_exception 'StreamNotFound' => ( | ||
| 40 | message => 'The given stream does not exist' | ||
| 41 | ); | ||
| 42 | |||
| 43 | =head2 GroupExists | ||
| 44 | |||
| 45 | Thrown when the user is trying to re-create a group | ||
| 46 | but the operation doesn't allow that. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | declare_exception 'GroupExists' => ( | ||
| 51 | message => 'The given group name already exists' | ||
| 52 | ); | ||
| 53 | |||
| 54 | =head2 GroupNotFound | ||
| 55 | |||
| 56 | Thrown when the operation requires the group to be | ||
| 57 | exist but it's not. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | declare_exception 'GroupNotFound' => ( | ||
| 62 | message => 'The given group does not exist' | ||
| 63 | ); | ||
| 64 | |||
| 65 | has $ryu; | ||
| 66 | has $streams; | ||
| 67 | has $channels; | ||
| 68 | |||
| 69 | BUILD { | ||
| 70 | $streams = {}; | ||
| 71 | $channels = {}; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head2 create_stream | ||
| 75 | |||
| 76 | Creats an empty stream. | ||
| 77 | |||
| 78 | =over 4 | ||
| 79 | |||
| 80 | =item * C<stream_name> - The name of the stream. | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | async method create_stream ($stream_name) { | ||
| 87 | return if $streams->{$stream_name}; | ||
| 88 | $streams->{$stream_name} = {current_id => 0, data => {}}; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 stream_info | ||
| 92 | |||
| 93 | Return stream details | ||
| 94 | |||
| 95 | =over 4 | ||
| 96 | |||
| 97 | =item * C<stream_name> - The name of the stream. | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | async method stream_info($stream_name) { | ||
| 104 | return $streams->{$stream_name} | ||
| 105 | // Myriad::Exception::Transport::Memory::StreamNotFound->throw(reason => 'Stream should exist before creating new consumer group'); | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 add_to_stream | ||
| 109 | |||
| 110 | Adds a new item to a stream, if the stream doesn't exist it'll be created. | ||
| 111 | |||
| 112 | =over 4 | ||
| 113 | |||
| 114 | =item * C<stream_name> - The name of the stream. | ||
| 115 | |||
| 116 | =item * C<data> - A perl hash that contains the user data. | ||
| 117 | |||
| 118 | =back | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | async method add_to_stream ($stream_name, %data) { | ||
| 123 | my ($id, $stream) = (0, undef); | ||
| 124 | if ($stream = $streams->{$stream_name}) { | ||
| 125 | $id = ++$stream->{current_id} if $stream->{data}->%*; | ||
| 126 | } else { | ||
| 127 | await $self->create_stream($stream_name); | ||
| 128 | } | ||
| 129 | |||
| 130 | $streams->{$stream_name}->{data}->{$id} = { data => \%data }; | ||
| 131 | return $id; | ||
| 132 | } | ||
| 133 | |||
| 134 | =head2 create_consumer_group | ||
| 135 | |||
| 136 | Creates a consumer group for a given stream. | ||
| 137 | |||
| 138 | =over 4 | ||
| 139 | |||
| 140 | =item * C<stream_name> - The name of the stream. | ||
| 141 | |||
| 142 | =item * C<group_name> - The name of the group that is going to be created. | ||
| 143 | |||
| 144 | =item * C<offset> - If set the group will see this value as the first message in the stream. | ||
| 145 | |||
| 146 | =item * C<make_stream> - If set and the stream doesn't exist it'll be created. | ||
| 147 | |||
| 148 | =back | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | async method create_consumer_group ($stream_name, $group_name, $offset = 0, $make_stream = 0) { | ||
| 153 | await $self->create_stream($stream_name) if $make_stream; | ||
| 154 | my $stream = $streams->{$stream_name} // Myriad::Exception::Transport::Memory::StreamNotFound->throw(reason => 'Stream should exist before creating new consumer group'); | ||
| 155 | $stream->{groups}->{$group_name} = {pendings => {}, cursor => $offset} unless exists $stream->{groups}{$group_name}; | ||
| 156 | } | ||
| 157 | |||
| 158 | =head2 read_from_stream | ||
| 159 | |||
| 160 | Read elements from the stream. | ||
| 161 | |||
| 162 | This operation is stateless you can re-read the same message | ||
| 163 | as long as it exists in the stream. | ||
| 164 | |||
| 165 | =over 4 | ||
| 166 | |||
| 167 | =item * C<stream_name> - The name of the stream. | ||
| 168 | |||
| 169 | =item * C<offset> - The number of messages to skip. | ||
| 170 | |||
| 171 | =item * C<count> - The limit of messages to be received. | ||
| 172 | |||
| 173 | =back | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | async method read_from_stream ($stream_name, $offset = 0 , $count = 50) { | ||
| 178 | my $stream = $streams->{$stream_name} // return (); | ||
| 179 | return { | ||
| 180 | map { $_ => $stream->{data}->{$_}->{data} } ($offset..$offset+$count - 1) | ||
| 181 | }; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | =head2 read_from_stream_by_consumer | ||
| 186 | |||
| 187 | Read elements from the stream for the given group. | ||
| 188 | |||
| 189 | This operation is stateful if the message read by a consumer "A" | ||
| 190 | it won't be available for consumer "B" also consumer "A" won't be able | ||
| 191 | to re-read the message using this call. | ||
| 192 | |||
| 193 | This is not exaclty how Redis works but it covers our need at the moment. | ||
| 194 | |||
| 195 | =over 4 | ||
| 196 | |||
| 197 | =item * C<stream_name> - The name of the stream should exist before calling this sub. | ||
| 198 | |||
| 199 | =item * C<group_name> - The name of the group should exist before callingg this sub. | ||
| 200 | |||
| 201 | =item * C<consumer_name> - The current consumer name, will be used to keep tracking of pendign messages. | ||
| 202 | |||
| 203 | =item * C<offset> - If given the consumer can skip the given number of messages. | ||
| 204 | |||
| 205 | =item * C<count> - The limit of messages to be received. | ||
| 206 | |||
| 207 | =back | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | async method read_from_stream_by_consumer ($stream_name, $group_name, $consumer_name, $offset = 0, $count = 50) { | ||
| 212 | my ($stream, $group) = $self->get_stream_group($stream_name, $group_name); | ||
| 213 | my $group_offset = $offset + $group->{cursor}; | ||
| 214 | my %messages; | ||
| 215 | my $read_count = 0; | ||
| 216 | for my $i ($group_offset..$group_offset+$count - 1) { | ||
| 217 | if (my $message = $stream->{data}->{$i}) { | ||
| 218 | $messages{$i} = $message->{data}; | ||
| 219 | $group->{pendings}->{$i} = {since => time, consumer => $consumer_name, delivery_count => 0}; | ||
| 220 | $read_count++; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | $group->{cursor} += $offset + $read_count; | ||
| 225 | |||
| 226 | return \%messages; | ||
| 227 | } | ||
| 228 | |||
| 229 | =head2 pending_stream_by_consumer | ||
| 230 | |||
| 231 | Read pending elements from the stream for the given group. | ||
| 232 | |||
| 233 | This operation will return messages consumed but not yet acknowledged only. | ||
| 234 | It will return items regardless of their consumer. | ||
| 235 | |||
| 236 | =over 4 | ||
| 237 | |||
| 238 | =item * C<stream_name> - The name of the stream should exist before calling this sub. | ||
| 239 | |||
| 240 | =item * C<group_name> - The name of the group should exist before callingg this sub. | ||
| 241 | |||
| 242 | =item * C<consumer_name> - The current consumer name, will be used to keep tracking of pendign messages. | ||
| 243 | |||
| 244 | =item * C<offset> - If given the consumer can skip the given number of messages. | ||
| 245 | |||
| 246 | =item * C<count> - The limit of messages to be received. | ||
| 247 | |||
| 248 | =back | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | async method pending_stream_by_consumer ($stream_name, $group_name, $consumer_name, $offset = 0, $count = 10) { | ||
| 253 | my ($stream, $group); | ||
| 254 | try { | ||
| 255 | ($stream, $group) = $self->get_stream_group($stream_name, $group_name); | ||
| 256 | } catch ($e) { | ||
| 257 | return {}; | ||
| 258 | } | ||
| 259 | my %messages; | ||
| 260 | for my $i (keys $group->{pendings}->%*) { | ||
| 261 | if (my $message = $stream->{data}->{$i}) { | ||
| 262 | $messages{$i} = $message->{data}; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | return \%messages; | ||
| 267 | } | ||
| 268 | =head2 ack_message | ||
| 269 | |||
| 270 | Remove a message from the pending list. | ||
| 271 | |||
| 272 | It's safe to call this method multiple time even if the message doesn't exist. | ||
| 273 | |||
| 274 | =over 4 | ||
| 275 | |||
| 276 | =item * C<stream_name> - The name of the stream. | ||
| 277 | |||
| 278 | =item * C<group_name> - The name of group that has this message as pending. | ||
| 279 | |||
| 280 | =item * C<message_id> - The Id of the message that we want to acknowledge. | ||
| 281 | |||
| 282 | =back | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | async method ack_message ($stream_name, $group_name, $message_id) { | ||
| 287 | my ($stream, $group) = $self->get_stream_group($stream_name, $group_name); | ||
| 288 | delete $group->{pendings}->{$message_id}; | ||
| 289 | } | ||
| 290 | |||
| 291 | =head2 claim_message | ||
| 292 | |||
| 293 | Re-assign a message to another consumer. | ||
| 294 | |||
| 295 | It'll return the full message. | ||
| 296 | |||
| 297 | =over 4 | ||
| 298 | |||
| 299 | =item * C<stream_name> - The name of the stream. | ||
| 300 | |||
| 301 | =item * C<group_name> - The name of the group that has the message as pending. | ||
| 302 | |||
| 303 | =item * C<consumer_name> - The name of the new consumer. | ||
| 304 | |||
| 305 | =item * C<message_id> - The id of the message to be claimed. | ||
| 306 | |||
| 307 | =back | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | async method claim_message ($stream_name, $group_name, $consumer_name, $message_id) { | ||
| 312 | my ($stream, $group) = $self->get_stream_group($stream_name, $group_name); | ||
| 313 | if (my $info = $group->{pendings}->{$message_id}) { | ||
| 314 | $info = {since => time, consumer => $consumer_name, delivery_count => $info->{delivery_count}++}; | ||
| 315 | return $stream->{data}->{$message_id}->%*; | ||
| 316 | } else { | ||
| 317 | return (); | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | =head2 publish | ||
| 322 | |||
| 323 | Publish a message, if no consumer exists the message will be lost. | ||
| 324 | |||
| 325 | =over 4 | ||
| 326 | |||
| 327 | =item * C<channel_name> - The name of the channel that the message will be published to. | ||
| 328 | |||
| 329 | =item * C<message> - A scalar that is going to be published. | ||
| 330 | |||
| 331 | =back | ||
| 332 | |||
| 333 | =cut | ||
| 334 | |||
| 335 | async method publish ($channel_name, $message) { | ||
| 336 | my $subscribers = $channels->{$channel_name}; | ||
| 337 | |||
| 338 | for my $subscriber ($subscribers->@*) { | ||
| 339 | $subscriber->emit($message); | ||
| 340 | } | ||
| 341 | |||
| 342 | return length $subscribers; | ||
| 343 | } | ||
| 344 | |||
| 345 | =head2 subscribe | ||
| 346 | |||
| 347 | Subscribe to a channel by optaining a L<Ryu::Source> that'll receive events. | ||
| 348 | |||
| 349 | =over 4 | ||
| 350 | |||
| 351 | =item * C<channel_name> - The name of the channel. | ||
| 352 | |||
| 353 | =back | ||
| 354 | |||
| 355 | =cut | ||
| 356 | |||
| 357 | async method subscribe ($channel_name) { | ||
| 358 | $channels->{$channel_name} = [] unless exists $channels->{$channel_name}; | ||
| 359 | my $sink = $ryu->sink; | ||
| 360 | push $channels->{$channel_name}->@*, $sink; | ||
| 361 | return $sink->source; | ||
| 362 | } | ||
| 363 | |||
| 364 | |||
| 365 | =head2 stream_groups_info | ||
| 366 | |||
| 367 | Get information about the stream's groups. | ||
| 368 | |||
| 369 | =cut | ||
| 370 | |||
| 371 | async method stream_groups_info ($stream_name) { | ||
| 372 | my $stream = $streams->{$stream_name} // Myriad::Exception::Transport::Memory::StreamNotFound->throw(); | ||
| 373 | my $info = []; | ||
| 374 | for my $group (keys $stream->{groups}->%*) { | ||
| 375 | my $group_info = { name => $group }; | ||
| 376 | # We don't keep track of consumers yet | ||
| 377 | $group_info->{consumers} = 0; | ||
| 378 | $group_info->{pending} = +(keys $stream->{groups}->{$group}->{pendings}->%*); | ||
| 379 | $group_info->{'last-delivered-id'} = $stream->{groups}->{$group}->{cursor}; | ||
| 380 | push $info->@*, $group_info; | ||
| 381 | } | ||
| 382 | |||
| 383 | return $info; | ||
| 384 | } | ||
| 385 | |||
| 386 | =head2 exists | ||
| 387 | |||
| 388 | Checks if a given key exists or not. | ||
| 389 | |||
| 390 | =cut | ||
| 391 | |||
| 392 | async method exists ($key) { | ||
| 393 | return exists $streams->{$key} || exists $channels->{$key}; | ||
| 394 | } | ||
| 395 | |||
| 396 | method get_stream_group ($stream_name, $group_name) { | ||
| 397 | my $stream = $streams->{$stream_name} // Myriad::Exception::Transport::Memory::StreamNotFound->throw(); | ||
| 398 | my $group = $stream->{groups}->{$group_name} // Myriad::Exception::Transport::Memory::GroupNotFound->throw(); | ||
| 399 | return ($stream, $group); | ||
| 400 | } | ||
| 401 | |||
| 402 | method _add_to_loop($loop) { | ||
| 403 | $self->add_child($ryu = Ryu::Async->new()); | ||
| 404 | $self->next::method($loop); | ||
| 405 | } | ||
| 406 | |||
| 407 | 1; | ||
| 408 | |||