Skip to content

Commit

Permalink
lib-dns: Allow setting the ioloop that the dns_lookup/dns_client is s…
Browse files Browse the repository at this point in the history
…tarted on.
  • Loading branch information
stephanbosch authored and sirainen committed Jan 30, 2018
1 parent ddaf416 commit 8836c37
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/lib-dns/dns-lookup.c
Expand Up @@ -39,6 +39,8 @@ struct dns_client {

unsigned int timeout_msecs, idle_timeout_msecs;

struct ioloop *ioloop;

struct istream *input;
struct io *io;
struct timeout *to_idle;
Expand Down Expand Up @@ -239,8 +241,9 @@ static void dns_lookup_free(struct dns_lookup **_lookup)
if (client->deinit_client_at_free)
dns_client_deinit(&client);
else if (client->head == NULL && client->fd != -1) {
client->to_idle = timeout_add(client->idle_timeout_msecs,
dns_client_idle_timeout, client);
client->to_idle = timeout_add_to(client->ioloop,
client->idle_timeout_msecs,
dns_client_idle_timeout, client);
}
i_free(lookup);
}
Expand All @@ -255,7 +258,7 @@ void dns_lookup_switch_ioloop(struct dns_lookup *lookup)
if (lookup->to != NULL)
lookup->to = io_loop_move_timeout(&lookup->to);
if (lookup->client->deinit_client_at_free)
lookup->client->io = io_loop_move_io(&lookup->client->io);
dns_client_switch_ioloop(lookup->client);
}

struct dns_client *dns_client_init(const struct dns_lookup_settings *set)
Expand All @@ -266,6 +269,7 @@ struct dns_client *dns_client_init(const struct dns_lookup_settings *set)
client->path = i_strdup(set->dns_client_socket_path);
client->timeout_msecs = set->timeout_msecs;
client->idle_timeout_msecs = set->idle_timeout_msecs;
client->ioloop = (set->ioloop != NULL ? set->ioloop : current_ioloop);
client->fd = -1;
return client;
}
Expand Down Expand Up @@ -349,8 +353,9 @@ dns_client_lookup_common(struct dns_client *client,
lookup->client = client;
lookup->ptr_lookup = ptr_lookup;
if (client->timeout_msecs != 0) {
lookup->to = timeout_add(client->timeout_msecs,
dns_lookup_timeout, lookup);
lookup->to = timeout_add_to(client->ioloop,
client->timeout_msecs,
dns_lookup_timeout, lookup);
}
lookup->result.ret = EAI_FAIL;
lookup->callback = callback;
Expand Down Expand Up @@ -385,6 +390,10 @@ int dns_client_lookup_ptr(struct dns_client *client, const struct ip_addr *ip,
void dns_client_switch_ioloop(struct dns_client *client)
{
struct dns_lookup *lookup;

if (client->ioloop == current_ioloop)
return;
client->ioloop = current_ioloop;

if (client->io != NULL)
client->io = io_loop_move_io(&client->io);
Expand Down
3 changes: 3 additions & 0 deletions src/lib-dns/dns-lookup.h
Expand Up @@ -11,6 +11,9 @@ struct dns_lookup_settings {
/* the idle_timeout_msecs works only with the dns_client_* API.
0 = disconnect immediately */
unsigned int idle_timeout_msecs;

/* ioloop to run the lookup on (defaults to current_ioloop) */
struct ioloop *ioloop;
};

struct dns_lookup_result {
Expand Down

0 comments on commit 8836c37

Please sign in to comment.