File: README.mkdn

package info (click to toggle)
libpoe-component-resolver-perl 0.921-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 200 kB
  • sloc: perl: 829; makefile: 4
file content (298 lines) | stat: -rw-r--r-- 9,258 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# NAME

POE::Component::Resolver - A non-blocking getaddrinfo() resolver

# VERSION

version 0.921

# SYNOPSIS

	#!/usr/bin/perl

	use warnings;
	use strict;

	use POE;
	use POE::Component::Resolver qw(AF_INET AF_INET6);

	my $r = POE::Component::Resolver->new(
		max_resolvers => 8,
		idle_timeout  => 5,
		af_order      => [ AF_INET6, AF_INET ],
		# sidecar_program => $path_to_program,
	);

	my @hosts = qw( ipv6-test.com );
	my $tcp   = getprotobyname("tcp");

	POE::Session->create(
		inline_states => {
			_start => sub {
				foreach my $host (@hosts) {
					$r->resolve(
						host    => $host,
						service => "http",
						event   => "got_response",
						hints   => { protocol => $tcp },
					) or die $!;
				}
			},

			_stop => sub { print "client session stopped\n" },

			got_response => sub {
				my ($error, $addresses, $request) = @_[ARG0..ARG2];
				use YAML; print YAML::Dump(
					{
						error => $error,
						addr => $addresses,
						req => $request,
					}
				);
			},
		}
	);

	POE::Kernel->run();

# DESCRIPTION

POE::Component::Resolver performs Socket::getaddrinfo() calls in
subprocesses where they're permitted to block as long as necessary.

By default it will run eight subprocesses and prefer address families
in whatever order getaddrinfo() returns them.  These defaults can be
overridden with constructor parameters.  getaddrinfo() delegates to
the operating system's resolver, which may be reconfigured according
to the usual conventions.

## PUBLIC METHODS

### new

Create a new resolver.  Returns an object that must be held and used
to make requests.  See the synopsis.

Accepts up to four optional named parameters.

"af_order" may contain an arrayref with the address families to
permit, in the order in which they're preferred.  Without "af_order",
the component will prefer IPv4 addresses over IPv6 for legacy
compatibility.  This may change in the future as IPv6 gains more
widespread acceptance.  See [ENVIRONMENT VARIABLES](#pod_ENVIRONMENT VARIABLES) for a way to
override the default without hacking modules.

	# Prefer IPv6 addresses, but also return IPv4 ones.
	my $r1 = POE::Component::Resolver->new(
		af_order => [ AF_INET6, AF_INET ]
	);

	# Only return IPv6 addresses,
	# or nothing in cases where only IPv4 addresses exist.
	my $r2 = POE::Component::Resolver->new(
		af_order => [ AF_INET6 ]
	);

"idle_timeout" determines how long to keep idle resolver subprocesses
before cleaning them up, in seconds.  It defaults to 15.0 seconds.

"max_resolvers" controls the component's parallelism by defining the
maximum number of sidecar processes to manage.  It defaults to 8, but
fewer or more processes can be configured depending on the resources
you have available and the amount of parallelism you require.

	# One at a time, but without the pesky blocking.
	my $r3 = POE::Component::Resolver->new( max_resolvers => 1 );

"sidecar_program" contains the disk location of a program that will
perform blocking lookups on standard input and print the results on
standard output.  The sidecar program is needed only in special
environments where the bundling and execution of extra utilities is
tricky.  PAR is one such environment.

The sidecar program needs to contain at least two statements:

	use POE::Component::Resolver::Sidecar;
	POE::Component::Resover::Sidecar->main();

### resolve

resolve() begins a new request to resolve a domain.  The request will
be enqueued in the component until a sidecar process can service it.
resolve() returns a request ID that may be used to cancel() a request
before it has completed (or undef if the request couldn't begin, such
as during shutdown).  Resolve requires two parameters and accepts some
additional optional ones.

"host" and "service" are required and contain the host (name or
Internet address) and service (name or numeric port) that will be
passed verbatim to getaddrinfo().  See [Socket](http://search.cpan.org/perldoc?Socket) for details.

"event" is optional; it contains the name of the event that will
contain the resolver response.  If omitted, it will default to
"resolver_response"; you may want to specify a shorter event name.

"hints" is optional.  If specified, it must contain a hashref of hints
exactly as getaddrinfo() expects them.  See [Socket](http://search.cpan.org/perldoc?Socket) for details.

"misc" is optional continuation data that will be passed back in the
response.  It may contain any type of data the application requires.

### cancel

Cancel a request, given the request's ID.

	my $request_id = $resolver->resolve("poe.dyndns.org", "http");
	$resolver->cancel($request_id);

### shutdown

Shut down the resolver.  POE::Component::Resolver retains resources
including child processes for up to "idle_timeout" seconds.  This may
keep programs running up to "idle_timeout" seconds longer than they
should.

POE::Component::Resolver will release its resources (including child
processes) when its shutdown() method is called.

### unpack_addr

In scalar context, unpack_addr($response_addr_hashref) returns the
addr element of $response_addr_hashref in a numeric form appropriate
for the address family of the address.

	sub handle_resolver_response {
		my ($error, $addresses, $request) = @_[ARG0..ARG2];

		foreach my $a (@$addresses) {
			my $numeric_addr = $resolver->unpack_addr($a);
			print "$request->{host} = $numeric_addr\n";
		}
	}

In list context, it returns the numeric port and address.

	sub handle_resolver_response {
		my ($error, $addresses, $request) = @_[ARG0..ARG2];

		foreach my $a (@$addresses) {
			my ($$numeric_addr, $port) = $resolver->unpack_addr($a);
			print "$request->{host} = $numeric_addr\n";
		}
	}

unpack_addr() is a convenience wrapper around getnameinfo() from
[Socket](http://search.cpan.org/perldoc?Socket).  You're certainly welcome to use the discrete function
instead.

unpack_addr() returns bleak emptiness on failure, regardless of
context.  You can check for undef return.

## PUBLIC EVENTS

### resolver_response

The resolver response event includes three parameters.

$_[ARG0] and $_[ARG1] contain the retrn values from Socket's
getaddrinfo() call.  These are an error message (if the call failed),
and an arrayref of address structures if the call succeeded.

The component provides its own error message, 'component shut down'.
This response is given for every pending request at the time the user
shuts down the component.

$_[ARG2] contains a hashref of information provided to the resolve()
method.  Specifically, the values of resolve()'s "host", "service" and
"misc" parameters.

# ENVIRONMENT VARIABLES

## POCO_RESOLVER_IPV

The POCO_RESOLVER_IPV environment variable sets this component's
default Internet Protocol Version search order.  If the variable
exists, it should contain a string with the numbers 4 and/or 6.
POE::Component::Resolver will treate these as Internet Protocol
versions to consider, in the order they are preferred.

POE::Component::Resolver's new() method accepts an "af_order"
parameter that overrides this environment variable.

Default to IPv4 addresses only:

	export POCO_RESOLVER_IPV=4

Default to IPv6 addresses only:

	export POCO_RESOLVER_IPV=6

Prefer IPv6, but accept IPv4 if needed:

	export POCO_RESOLVER_IPV=64

Prefer IPv4, but accept IPv6 if needed:

	export POCO_RESOLVER_IPV=46

# COMPATIBILITY ISSUES

## Microsoft Windows

This module requires "Microsoft TCP/IP version 6" to be installed.
Steps for Windows XP Pro (the steps for your particular version of
Windows may be subtly or drastically different):

- * Open your Control Panel

- * Open your Network Connections

- * Select your network connection from the available one(s)

- * In the Local Area Connection Status dialog, click the Properties button

- * If "Microsoft TCP/IP version 6" is listed as an item being used, you are done.

- * Otherwise click Install...

- * Choose to add a Protocol

- * And install "Microsoft TCP/IP version 6" from the list of network protocols.

# BUGS

There is no timeout on requests.

There is no way to cancel a pending request.

# TROUBLESHOOTING

## programs linger for several seconds before exiting

Programs should shutdown() their POE::Component::Resolver objects when
they are through needing asynchronous DNS resolution.  Programs should
additionally destroy their resolvers if they intend to run awhile and
want to reuse the memory they consume.

In some cases, it may be necessary to shutdown components that perform
asynchronous DNS using POE::Component::Resolver... such as
POE::Component::IRC, POE::Component::Client::Keepalive and
POE::Component::Client::HTTP.

By default, the resolver subprocesses hang around for idle_timeout,
which defaults to 15.0 seconds.  Destroying the Resolver object will
clean up the process pool.  Assuming only that is keeping the event
loop active, the program will then exit cleanly.

Alternatively, reduce idle_timeout to a more manageable number, such
as 5.0 seconds.

Otherwise something else may also be keeping the event loop active.

# LICENSE

Except where otherwise noted, this distribution is Copyright 2011 by
Rocco Caputo.  All rights reserved.  This distribution is free
software; you may redistribute it and/or modify it under the same
terms as Perl itself.