2

I have a epoll server which sometimes opens outgoing connections, using their hostnames representation. Because of high rate of incoming connections flow, I don't want to block somewhere like getaddrinfo() or gethostbyname(). Sure, I could implement a cache plus a new thread, where the hostname resolution would be performed. Is there a single-threaded non-blocking way to resolve a hostname to an IP?

pavelkolodin
  • 2,859
  • 3
  • 31
  • 74
  • single-threaded and non-blocking is (almost) excluding each other. If you do not have a secondary thread to process the work for you, how do you expect to not block when waiting for data? – Tymoteusz Paul Jul 02 '14 at 18:56
  • @Puciek: i think you are wrong about excluding, see `epoll` – pavelkolodin Jul 02 '14 at 20:41
  • I did say that almost, but this isn't a feasible solution for everyday programming. Easiest way to go around IO waits is to thread your application, if you do not want to do that - make it asynchronous, which is going to be quite a pain to redesign but hey, you didn't want the easy way out! – Tymoteusz Paul Jul 02 '14 at 20:48

1 Answers1

1

There are various libraries for the purpose, e.g. libevent contains a resolver.

I sort of agree with @Puciek though, doing this in a single thread adds quite a bit of complexity for questionable benefits. Using a dedicated resolving thread and communicating with it through pipes might be the best solution.

Since you mention epoll I guess you're using Linux. It has a getaddrinfo_a function that if I understand correctly does part of this for you. It clones a thread and runs getaddrinfo there. I never used it though so can't help beyond that.

Per Johansson
  • 6,697
  • 27
  • 34
  • How does `libevent` implements async name resolution? – pavelkolodin Jul 10 '14 at 07:33
  • It's a separate module called `evdns`. Note: I haven't actually used that one either. – Per Johansson Jul 10 '14 at 17:28
  • I don't think getaddrinfo_a clones a thread? It uses code from BIND 8, a real resolver... – Carlo Wood Aug 13 '18 at 15:37
  • 1
    @CarloWood The code is here: https://sourceware.org/git/?p=glibc.git;a=blob;f=resolv/gai_misc.c;h=e7c3b63cc5725b4fc98cef4902f00d9a8228516a;hb=HEAD#l253 I mostly skimmed it but looks like it creates a thread and then calls getaddrinfo on those threads. – Per Johansson Aug 14 '18 at 10:34