lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Paul K once stated:
> Hi Adam,
> 
> > That's because connect() includes a DNS lookup, and DNS resolution on
> > Windows is synchronous. Most programs work around this problem using
> > threads. It's kinda lame.
> 
> Yes, I thought it blocks somewhere in the resolver. There is not much
> I can do at the application level. Fortunately in my case I only have
> to deal with known addresses.

  You could always handle the DNS request yourself, and as long as your DNS
resolver will do all the work for you (a recusrive DNS resolver) the most
difficult part is encoding and decoding the DNS packets [1].

> > What's the expected behavior of a zero timeout? I would expect it to
> > never succeed at all, because no packet ever has zero latency.
> 
> It would be the same as for a small timeout; I'd expect "nil, timeout"
> to be returned with copas taking care of that. Even using 0.000001 as
> the timeout value works, but not 0.
> Paul.

  If you want to handle connect in a non-blocking manner, you need to make
the connecting socket non-blocking, then add it to select() [2] for writing,
then make the connect() system call.  It will return EINPROGRESS, and the
man page on my system says this:

	EINPROGRESS
		The  socket  is  non-blocking and the connection cannot be
		completed immediately.  It is possible to select(2) or
		poll(2) for completion by selecting the socket for writing.
		After select indicates writability, use getsockopt(2) to
		read the SO_ERROR option at level SOL_SOCKET to determine
		whether connect completed successfully (SO_ERROR is zero) or
		unsuccessfully (SO_ERROR is one of the usual error codes
		listed here, explaining the reason for the failure).

> > That's because connect() includes a DNS lookup, and DNS resolution on
> > Windows is synchronous. Most programs work around this problem using
> > threads. It's kinda lame.

  The actual connect() system call does not do a DNS lookup, that's usually
done by upper level code.

  -spc

[1]	This DNS library:

	https://github.com/spc476/SPCDNS

	contains Lua bindings to encode DNS queries and decode DNS
	responses.  The network side is lacking, but it's the network side
	that isn't the hard part of DNS.

[2]	Or poll, or epoll or kqueue or whatever your system uses for this.