0

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

  • 1
    This looks like an [XY problem](https://en.wikipedia.org/wiki/XY_problem). It might be more useful to describe the original problem X you are trying to solve instead of only passing some snippets from your attempts of solving problem Y you run into in an attempt to solve X. See also [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Steffen Ullrich May 29 '19 at 11:46
  • 1
    I wrote [LWP::Protocol::AnyEvent::http](http://search.cpan.org/perldoc?LWP::Protocol::AnyEvent::http), which I'd use over Coro::LWP. That said, ClickHouse doesn't use LWP?!? – ikegami May 29 '19 at 17:00

1 Answers1

2

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?

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • > Coro::LWP specifically makes Net::HTTP use Coro::LWP::Socket instead of IO::Socket::INET i'm not trying to change it to IO::Socket::IP IO::Socket::IP is default socket when you try to create Net::HTTP client. so i need to change it to IO::Socket::INET, so Coro::LWP will replace it with Coro::LWP::Socket. but when Coro::LWP replaced socket to Coro::LWP::Socket, i've got error when trying send a request. – Rabota rabotavich May 30 '19 at 11:15
  • oic. Working around a bug in Coro::LWP. I'd normally suggest reaching out to the author, but this particular author is a real pain to deal with. What you said isn't exactly true, though. ::IP is only the default if ::IP is installed, and if a class isn't specified ahead of time. Put this early in your program: `use IO::Socket::INET qw( ); BEGIN { $Net::HTTP::SOCKET_CLASS = 'IO::Socket::INET'; }` – ikegami May 30 '19 at 19:08
  • thanks! i hardcoded inside Net::HTTP :) i edited a body of question, can you please check the code? – Rabota rabotavich May 31 '19 at 07:27
  • The code in you added uses Coro::LWP::Socket (instead of IO::Socket::IP or IO::Socket::INET) as the base class for Net::HTTP (which you can verify by examining `@Net::HTTP::ISA`). Again, if you're having problem, maybe you should stop hacking a solution and use AnyEvent::ClickHouse. AnyEvent and Coro are compatible. – ikegami May 31 '19 at 07:38
  • I don't seem to be able to install Coro on my machine anymore, so I can't run the code to see if it works for me. – ikegami May 31 '19 at 07:46