-
Notifications
You must be signed in to change notification settings - Fork 576
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
Protocol specific IOLoop streams #1227
Conversation
lib/Mojo/IOLoop.pm
Outdated
Mojo::IOLoop->remove($id) | ||
|
||
Perform transition of a stream container class to a new one. It will steal handle | ||
from an original connection, but won't remove connection itself. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Description is bad english.
lib/Mojo/IOLoop.pm
Outdated
|
||
my $new = Mojo::IOLoop->transition($id, 'Mojo::IOLoop::Stream::HTTPClient'); | ||
my $stream = Mojo::IOLoop->stream($new); | ||
Mojo::IOLoop->remove($id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example is inconsistent with surrounding examples.
lib/Mojo/IOLoop/Stream/Client.pm
Outdated
|
||
=head1 DESCRIPTION | ||
|
||
Base class for building transaction based protocol specific clients. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description sounds like a generic placeholder.
lib/Mojo/IOLoop/Stream/Client.pm
Outdated
} | ||
); | ||
$stream->start; | ||
$stream->process($tx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example is inconsistent with other method examples in Mojolicious.
lib/Mojo/IOLoop/Stream/Client.pm
Outdated
|
||
=cut | ||
|
||
# vim: tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab tw=81 fo+=t fo-=l |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't keep vim lines in Mojolicious.
t/mojo/streams.t
Outdated
ok !$stash->{kept_alive}, 'connection was not kept alive'; | ||
ok !$closed, 'connection is not closed'; | ||
|
||
$app->plugins->once(before_dispatch => sub { $stash = shift->stash }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code looks lost.
t/mojo/streams.t
Outdated
$tx = _tx('http://127.0.0.1/keep-alive'); | ||
$tx->on(finish => sub { Mojo::IOLoop->stop }); | ||
|
||
$client_stream->process($tx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block looks lost too.
t/mojo/streams.t
Outdated
Mojo::IOLoop->stop; | ||
} | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra newline.
t/mojo/streams.t
Outdated
return $t; | ||
} | ||
|
||
# vim: tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab tw=81 fo+=t fo-=l |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not keep vim lines in Mojolicious.
t/mojo/streams.t
Outdated
is $err->{message}, 'Request timeout', 'right message'; | ||
|
||
|
||
done_testing(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually make done_testing();
the last line in a test.
For those not following IRC, i was the one who recommended the change from |
lib/Mojo/IOLoop.pm
Outdated
@@ -48,6 +48,9 @@ sub acceptor { | |||
sub client { | |||
my ($self, $cb) = (_instance(shift), pop); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra newline. Don't split up arg handling if you don't have to.
lib/Mojo/IOLoop.pm
Outdated
@@ -106,11 +109,14 @@ sub reset { | |||
sub server { | |||
my ($self, $cb) = (_instance(shift), pop); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra newline. Don't split up arg handling if you don't have to.
lib/Mojo/IOLoop.pm
Outdated
sub transition { | ||
my ($self, $id, $class) = (_instance(shift), @_); | ||
my $new = $class->new($self->stream($id)->steal_handle); | ||
return $self->_stream($new, $self->_id, !!$self->{in}{$id}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is the old stream garbage collected? Can't we just reuse the same id? Once the handle is stolen the old stream is dead anyway.
25ea38e
to
04067a5
Compare
lib/Mojo/IOLoop.pm
Outdated
|
||
my $stream = Mojo::IOLoop->transition($id, 'Mojo::IOLoop::Stream::HTTPClient'); | ||
|
||
Replace connection with new one from different class. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something simple like Transition stream to a different class.
would be better?
lib/Mojo/IOLoop/Stream/HTTPClient.pm
Outdated
|
||
=head1 NAME | ||
|
||
Mojo::IOLoop::Stream::HTTPClient - Non-blocking I/O HTTP client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Non-blocking I/O HTTP client stream
would work better with the theme.
lib/Mojo/IOLoop/Stream/HTTPClient.pm
Outdated
} | ||
); | ||
|
||
$client->connect(address => 'mojolicious.org', port => 80); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block needs a descriptive comment too, or it looks strange.
lib/Mojo/IOLoop/Stream/HTTPClient.pm
Outdated
$tx->on(finish => sub { | ||
my $tx = shift; | ||
say $tx->res->code; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Borked indentation.
lib/Mojo/IOLoop/Stream/HTTPClient.pm
Outdated
|
||
=head1 DESCRIPTION | ||
|
||
HTTP client connection based on L<Mojo::IOLoop::Stream>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a better description.
lib/Mojo/IOLoop/Stream/HTTPClient.pm
Outdated
... | ||
}); | ||
|
||
Emitted when connection should be upgraded to WebSocket. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Emitted when the connection should be upgraded to the WebSocket protocol.
?
lib/Mojo/IOLoop/Stream/HTTPClient.pm
Outdated
|
||
$stream->process(Mojo::Transaction::HTTP->new); | ||
|
||
Process a transaction with current connection. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Process a L<Mojo::Transaction::HTTP> object with the current connection
?
lib/Mojo/IOLoop/Stream/HTTPServer.pm
Outdated
$tx->on(resume => sub { $self->_write_content }); | ||
$self->_write_content; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra newline.
lib/Mojo/IOLoop/Stream/HTTPServer.pm
Outdated
|
||
=head1 NAME | ||
|
||
Mojo::IOLoop::Stream::HTTPServer - Non-blocking I/O HTTP server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking I/O HTTP server stream
?
lib/Mojo/IOLoop/Stream/HTTPServer.pm
Outdated
|
||
=head1 DESCRIPTION | ||
|
||
HTTP server connection based on L<Mojo::IOLoop::Stream>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a better description.
lib/Mojo/IOLoop/Stream/HTTPServer.pm
Outdated
... | ||
}); | ||
|
||
Emitted when a transaction for new request was build. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't help but think that this event needs a better name, maybe something like start
? (Oh, and of course a better description 😉)
lib/Mojo/IOLoop/Stream/HTTPServer.pm
Outdated
... | ||
}); | ||
|
||
Emitted when connection should be upgraded to WebSocket. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, needs a better description.
|
||
=head1 NAME | ||
|
||
Mojo::IOLoop::Stream::WebSocketClient - Non-blocking I/O WebSocket client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking I/O WebSocket client stream
?
|
||
=head1 DESCRIPTION | ||
|
||
WebSocket client connection based on L<Mojo::IOLoop::Stream>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better description, as usual. 😄
|
||
my $stream = Mojo::IOLoop::Stream::WebSocketServer->new($handle); | ||
$stream->tx($ws); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra newline.
use Mojo::IOLoop::Stream::WebSocketServer; | ||
use Mojo::Transaction::WebSocket; | ||
|
||
my $ws = Mojo::Transaction::WebSocket->new; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code blocks are missing descriptive comments.
|
||
$stream->process(Mojo::Transaction::WebSocket->new); | ||
|
||
Process a transaction with current connection. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Process a L<Mojo::Transaction::WebSocket> object.
?
|
||
$stream->process(Mojo::Transaction::WebSocket->new); | ||
|
||
Process a transaction with current connection. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Process a L<Mojo::Transaction::WebSocket> object
.
t/mojo/streams.t
Outdated
@@ -0,0 +1,175 @@ | |||
use Mojo::Base -strict; | |||
|
|||
BEGIN { $ENV{MOJO_REQUEST_TIMEOUT} = 0.25 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this used for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should also be a BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
here, to avoid accidental uses of EV
.
I've removed the |
@jberger @marcusramberg @jhthorsen I'm calling for a formal vote on this pull request. |
I've reviewed the code and it looks good. The new stream APIs are very minimalistic and seem future proof. A performance difference is barely measurable. The new P.S.: I think there might even be more code in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks solid
Thanks, applied. |
Unfortunately this change had to be reverted. 61f6cbf |
Summary
Implementing Mojo::IOLoop::Stream subclasses for HTTP and WebSocket connections.
Motivation
An attempt to implement feature required for an issue.
References
#423 (comment)