Search code examples
perlsocketsasynchronousnet-http

How to use "Net::http" in Coro?


Got stuck on using Net::HTTP when i create a Net::HTTP object it initialize with "IO::Socket::IP". how can i change default socket without hard-coding? I would like to change it to IO::Socket::INET.

So if i hard-code socket, it works. But when i'm trying to use Coro::LWP and Coro::LWP changes IO::Socket::INET to Coro::Socket i got error:

Status read failed: Transport endpoint is not connected at perl5/lib/perl5/Net/HTTP/Methods.pm line 282.

I need to change socket because Clickhouse module on cpan doesn't support async requests.

here is code that doesn't work

use IO::Socket::INET qw( );
BEGIN { $Net::HTTP::SOCKET_CLASS = 'IO::Socket::INET'; };
use Coro::LWP;
my $s = Net::HTTP->new(Host => "www.perl.com") || die $@;
$s->write_request(GET => "/");
print $_ for ( $s->read_response_headers );

fixed! just change Coro::Socket with Coro::PatchSet::Socket


Solution

  • You can't use IO::Socket::INET or IO::Socket::IP with Coro. Coro is a co-operative multi-threading system, so it only works with co-operating modules, and neither of these modules are Coro-aware. (By "work", I mean allow threads and asynchronous operations to progress.)

    Among other things, Coro::LWP specifically makes Net::HTTP use Coro::LWP::Socket instead of IO::Socket::INET. Your attempts to make Net::HTTP use IO::Socket::IP are counter-productive.

    You said you're switching the module because Clickhouse (by which I presume you meant ClickHouse) doesn't support async requests, but replacing IO::Socket::INET with IO::Socket::IP doesn't help with that at all.

    Have you looked at AnyEvent::ClickHouse?