Skip to content

Commit

Permalink
lib-dict: Added dict_switch_ioloop()
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen authored and GitLab committed Jun 19, 2016
1 parent a9e079f commit a5ec975
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/lib-dict/dict-client.c
Expand Up @@ -552,24 +552,26 @@ static void client_dict_wait(struct dict *_dict)

dict->prev_ioloop = current_ioloop;
io_loop_set_current(dict->ioloop);

if (dict->to_idle != NULL)
dict->to_idle = io_loop_move_timeout(&dict->to_idle);
if (dict->to_requests != NULL)
dict->to_requests = io_loop_move_timeout(&dict->to_requests);
connection_switch_ioloop(&dict->conn.conn);

dict_switch_ioloop(_dict);
while (array_count(&dict->cmds) > 0)
io_loop_run(dict->ioloop);

io_loop_set_current(dict->prev_ioloop);
dict->prev_ioloop = NULL;

dict_switch_ioloop(_dict);
}

static bool client_dict_switch_ioloop(struct dict *_dict)
{
struct client_dict *dict = (struct client_dict *)_dict;

if (dict->to_idle != NULL)
dict->to_idle = io_loop_move_timeout(&dict->to_idle);
if (dict->to_requests != NULL)
dict->to_requests = io_loop_move_timeout(&dict->to_requests);
connection_switch_ioloop(&dict->conn.conn);
return array_count(&dict->cmds) > 0;
}

static void
Expand Down Expand Up @@ -1008,6 +1010,7 @@ struct dict dict_driver_client = {
client_dict_set,
client_dict_unset,
client_dict_atomic_inc,
client_dict_lookup_async
client_dict_lookup_async,
client_dict_switch_ioloop
}
};
1 change: 1 addition & 0 deletions src/lib-dict/dict-file.c
Expand Up @@ -660,6 +660,7 @@ struct dict dict_driver_file = {
dict_transaction_memory_set,
dict_transaction_memory_unset,
dict_transaction_memory_atomic_inc,
NULL,
NULL
}
};
1 change: 1 addition & 0 deletions src/lib-dict/dict-memcached-ascii.c
Expand Up @@ -673,6 +673,7 @@ struct dict dict_driver_memcached_ascii = {
dict_transaction_memory_set,
dict_transaction_memory_unset,
dict_transaction_memory_atomic_inc,
NULL,
NULL
}
};
1 change: 1 addition & 0 deletions src/lib-dict/dict-memcached.c
Expand Up @@ -378,6 +378,7 @@ struct dict dict_driver_memcached = {
NULL,
NULL,
NULL,
NULL,
NULL
}
};
1 change: 1 addition & 0 deletions src/lib-dict/dict-private.h
Expand Up @@ -39,6 +39,7 @@ struct dict_vfuncs {

void (*lookup_async)(struct dict *dict, const char *key,
dict_lookup_callback_t *callback, void *context);
bool (*switch_ioloop)(struct dict *dict);
};

struct dict {
Expand Down
1 change: 1 addition & 0 deletions src/lib-dict/dict-redis.c
Expand Up @@ -790,6 +790,7 @@ struct dict dict_driver_redis = {
redis_set,
redis_unset,
redis_atomic_inc,
NULL,
NULL
}
};
3 changes: 2 additions & 1 deletion src/lib-dict/dict-sql.c
Expand Up @@ -1237,7 +1237,8 @@ static struct dict sql_dict = {
sql_dict_set,
sql_dict_unset,
sql_dict_atomic_inc,
sql_dict_lookup_async
sql_dict_lookup_async,
NULL
}
};

Expand Down
8 changes: 8 additions & 0 deletions src/lib-dict/dict.c
Expand Up @@ -101,6 +101,14 @@ void dict_wait(struct dict *dict)
dict->v.wait(dict);
}

bool dict_switch_ioloop(struct dict *dict)
{
if (dict->v.switch_ioloop != NULL)
return dict->v.switch_ioloop(dict);
else
return FALSE;
}

static bool dict_key_prefix_is_valid(const char *key)
{
return strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0 ||
Expand Down
4 changes: 4 additions & 0 deletions src/lib-dict/dict.h
Expand Up @@ -73,6 +73,10 @@ int dict_init(const char *uri, const struct dict_settings *set,
void dict_deinit(struct dict **dict);
/* Wait for all pending asynchronous operations to finish. */
void dict_wait(struct dict *dict);
/* Switch the dict to the current ioloop. This can be used to do dict_wait()
among other IO work. Returns TRUE if there is actually some work that can
be waited on. */
bool dict_switch_ioloop(struct dict *dict) ATTR_NOWARN_UNUSED_RESULT;

/* Lookup value for key. Set it to NULL if it's not found.
Returns 1 if found, 0 if not found and -1 if lookup failed. */
Expand Down
15 changes: 12 additions & 3 deletions src/plugins/dict-ldap/dict-ldap.c
Expand Up @@ -240,19 +240,27 @@ static void ldap_dict_wait(struct dict *dict)

ctx->prev_ioloop = current_ioloop;
ctx->ioloop = io_loop_create();
ldap_client_switch_ioloop(ctx->client);
dict_switch_ioloop(dict);

do {
io_loop_run(current_ioloop);
} while (ctx->pending > 0);

io_loop_set_current(ctx->prev_ioloop);
ldap_client_switch_ioloop(ctx->client);
dict_switch_ioloop(dict);
io_loop_set_current(ctx->ioloop);
io_loop_destroy(&ctx->ioloop);
ctx->prev_ioloop = NULL;
}

static bool ldap_dict_switch_ioloop(struct dict *dict)
{
struct ldap_dict *ctx = (struct ldap_dict *)dict;

ldap_client_switch_ioloop(ctx->client);
return ctx->pending > 0;
}

static
void ldap_dict_lookup_done(const struct dict_lookup_result *result, void *ctx)
{
Expand Down Expand Up @@ -419,7 +427,8 @@ struct dict dict_driver_ldap = {
NULL, /*ldap_set,*/
NULL, /*ldap_unset,*/
NULL, /*ldap_atomic_inc,*/
ldap_dict_lookup_async
ldap_dict_lookup_async,
ldap_dict_switch_ioloop
}
};

Expand Down

0 comments on commit a5ec975

Please sign in to comment.