Skip to content

Commit

Permalink
Use Net::ZMQ::Message objects with a Str
Browse files Browse the repository at this point in the history
This is currently better at memory management,
as discussed in arnsholt/Net-ZMQ#6

Also:
    * put the heartbeat in a loop
    * Keep the separator in a lexical variable
    * a few minor cleanups, doc changes, debug msgs
  • Loading branch information
bduggan committed Aug 22, 2017
1 parent b8b4aaf commit a8d2b2d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ You can try it in the console like this:
jupyter-console --kernel=perl6
```

Which is more convenient as a shell alias:

```
alias iperl6='jupyter-console --kernel=perl6'
```

SEE ALSO
--------

Expand Down
6 changes: 3 additions & 3 deletions lib/Jupyter/Kernel.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ method run {
my $iopub = svc('iopub', ZMQ_PUB);
my $hb = svc('hb', ZMQ_REP);

start loop {
start {
$hb.start-heartbeat;
}

Expand Down Expand Up @@ -99,10 +99,10 @@ method run {
my $code = ~ $msg<content><code>;
my $result = $sandbox.eval($code);
my $status = 'complete';
debug $result.exception.gist;
debug "exception from sandbox: { .gist }" with $result.exception;
$status = 'invalid' if $result.exception;
$status = 'incomplete' if $result.incomplete;
debug "sending completion status $status";
debug "sending is_complete_reply: $status";
$shell.send: 'is_complete_reply', { :$status };
}
when 'shutdown_request' {
Expand Down
33 changes: 24 additions & 9 deletions lib/Jupyter/Kernel/Service.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unit class Jupyter::Kernel::Service;

use Net::ZMQ;
use Net::ZMQ::Constants;
use Net::ZMQ::Message;
use UUID;
use Log::Async;
use Digest::HMAC;
Expand Down Expand Up @@ -37,8 +38,6 @@ method setup {
self
}

constant $separator = buf8.new: "<IDS|MSG>".encode;

method !hmac(@m) {
hmac-hex $!key, @m[0] ~ @m[1] ~ @m[2] ~ @m[3], &sha256;
}
Expand All @@ -47,6 +46,7 @@ method read-message {
my @identities;
my @message;
my $separated = False;
my $separator = buf8.new: "<IDS|MSG>".encode;
while $!socket.receive.data() -> $data {
if !$separated {
if $data eqv $separator {
Expand Down Expand Up @@ -83,21 +83,36 @@ method send($type, $message, :$metadata = {} ) {
version => '5.0',
};

my @parts = ( $header, $!parent<header> // {}, $metadata, $message
).map({ to-json($_).encode });
my $hmac = self!hmac(@parts).encode;
my @parts = (
$header,
$!parent<header> // {},
$metadata,
$message
).map({
to-json($_)
});
my $hmac = self!hmac(@parts);

for |( @$identities.grep(*.defined) ), $separator, $hmac, |@parts[0..2] {
$!socket.send: $_, ZMQ_SNDMORE
for |( @$identities.grep(*.defined) ) {
my $data = $_;
my $msg = Net::ZMQ::Message.new(:$data);
$!socket.send: $msg, ZMQ_SNDMORE;
}
my $sep = "<IDS|MSG>";
for $sep, $hmac, |@parts[0..2] {
my $data = $_;
my $msg = Net::ZMQ::Message.new(message => $data);
$!socket.send: $msg, ZMQ_SNDMORE;
}
$!socket.send: @parts[3];
my $msg = Net::ZMQ::Message.new(message => @parts[3]);
$!socket.send: $msg;
}

method start-heartbeat {
loop {
try zmq_device(ZMQ_FORWARDER, $!socket, $!socket);
error $! if $!;
info 'heartbeat';
debug 'heartbeat';
sleep 1;
}
}

0 comments on commit a8d2b2d

Please sign in to comment.