Open laurent-aml opened 6 years ago
Looks reasonable. This ( + documentation + a test ) would make a good PR.
I am not sure but I think I'd like the prepare callback to be a attribute on the client object.
Although I am not sure, I did try implementing it as an optional last argument on the connect function here:
arguably this is more like the AnyEvent::Socket
interface, especially now that we are going to have optional host and port overrides.
I was about to revisit this. Thanks for your work on this. Your patch looks good, except that it does not fallback to the default timeout if the prepare callback returns 0/undef (which is the behavior of AnyEvent::Socket).
Suggestion:
@@ -83,7 +83,10 @@ has env_proxy => (
sub connect
{
- my($self, $uri, $host, $port) = @_;
+ my $self = shift;
+ my $uri = shift;
+ my $prepare_cb = pop if ref $_[-1] eq 'CODE';
+ my($host, $port) = @_;
unless(ref $uri)
{
require URI;
@@ -184,7 +187,10 @@ sub connect
undef $done;
}
});
- }, sub { $self->timeout });
+ }, sub {
+ my $timeout = $prepare_cb->(@_) if defined $prepare_cb;
+ return $timeout || $self->timeout;
+ });
$done;
}
connect() is missing the prepare callback, similarly to what AnyEvent::Socket::tcp_connect provides. I suggest: