Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weaken before non-blocking #6

Closed
genio opened this issue Sep 28, 2015 · 1 comment
Closed

Weaken before non-blocking #6

genio opened this issue Sep 28, 2015 · 1 comment

Comments

@genio
Copy link
Member

genio commented Sep 28, 2015

Each non-blocking call should weaken the reference to $self first.

# non-blocking request
$self->login(sub {
    my ( $sf, $err, $token ) = @_;
    return $sf->$cb($err,undef) if $err;
    my $url = Mojo::URL->new($sf->_instance_url)->path($sf->_path)->path("limits");
    $sf->ua->get($url, $sf->_headers(), sub {
        my ($ua, $tx) = @_;
        return $sf->$cb($sf->_error($tx->error, $tx->res->json),undef) unless $tx->success;
        return $sf->$cb(undef,$tx->res->json);
    });
});
return $self;

vs

# non-blocking request
Scalar::Util::weaken($self);
Mojo::IOLoop->delay(
    sub { $self->login(shift->begin); },
    sub {
      my ($delay, $err, $token) = @_;
      return $self->$cb($err, undef) if $err;
      return $self->ua->get(@req, $delay->begin);
    },
    sub {
      my ($delay, $tx) = @_;
      return $self->$cb($self->_error($tx), undef) unless $tx->success;
      return $self->$cb('', $tx->res->json);
    },
);
return $self;
@genio
Copy link
Member Author

genio commented Oct 1, 2015

http://irclog.perlgeek.de/mojo/2015-09-28#i_11287191

if you don't store the global ioloop singleton in $self, there is no need to weaken for
Mojo::IOLoop->... methods
you weaken if there's a cycle somewhere, not preemptively

@genio genio closed this as completed Oct 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant