[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis from mruby #1152

Merged
merged 53 commits into from
Mar 7, 2018
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
8d65fa9
add mruby redis feature
i110 Dec 27, 2016
033a9a1
Merge branch 'i110/mruby-consistent-fiber-handling' into i110/mruby-r…
i110 Mar 16, 2017
c41ad6d
tweak for mruby-redis
i110 Mar 16, 2017
4275123
fix mruby-redis gc problem
i110 Mar 16, 2017
da6c09f
Merge branch 'master' into i110/mruby-redis
i110 Mar 27, 2017
9c6034c
call ensure_connected for all commands
i110 Mar 27, 2017
b100dd5
fix redis reply allocation problem
i110 Mar 27, 2017
a9bfb96
Merge branch 'i110/mruby-consistent-fiber-handling' into i110/mruby-r…
i110 Mar 29, 2017
01b0526
Merge branch 'i110/mruby-consistent-fiber-handling' into i110/mruby-r…
i110 Apr 21, 2017
4ebc107
Merge branch 'i110/mruby-consistent-fiber-handling' into i110/mruby-r…
i110 Jun 9, 2017
f1d3f2b
fix redis reader memory issue
i110 Jun 10, 2017
5351d1a
tweak redis
i110 Jun 10, 2017
c0eeda0
raise CommandError when redis command failed
i110 Jun 10, 2017
2e33a36
define redis methods
i110 Jun 12, 2017
b803182
remove redis reader
i110 Jun 12, 2017
92226db
add error code and message in redis command callback
i110 Jun 12, 2017
64bc23d
add redis streaming command
i110 Jun 12, 2017
c904a00
fix mruby redis bugs
i110 Jun 13, 2017
f486184
add redis mruby tests
i110 Jun 13, 2017
fc7e026
install redis-server on travis
i110 Jun 13, 2017
b506c88
Merge branch 'master' into i110/mruby-redis
i110 Sep 6, 2017
f594f7c
- refactor mruby x redis
i110 Sep 7, 2017
4714f02
add connected? method to H2O::Redis
i110 Sep 7, 2017
87b1b31
Merge branch 'master' into i110/mruby-redis
i110 Sep 7, 2017
0199663
fix mruby redis gc issue
i110 Sep 7, 2017
6094301
add :db and :password option to H2O::Redis
i110 Sep 7, 2017
fdc79ee
remove unnecessary nil check
i110 Sep 19, 2017
ad1dee8
Merge remote-tracking branch 'origin/master' into i110/mruby-redis
i110 Sep 19, 2017
adfa1c3
Merge branch 'master' into i110/mruby-redis
i110 Oct 5, 2017
117a679
fix redis example client
i110 Oct 5, 2017
347699e
mruby-redis subscribe
i110 Oct 5, 2017
a5a295b
mruby-redis connect and command timeout
i110 Oct 6, 2017
65fea24
reformat
i110 Oct 6, 2017
e10def2
remove redis's fancy ascii art from test output
i110 Oct 6, 2017
3bdcdba
fix mruby redis test issue
i110 Oct 6, 2017
67f4459
set mruby redis default timeout to 5 secs
i110 Oct 6, 2017
57d3f86
fix mruby redis timeout destroying issue
i110 Oct 6, 2017
80b8950
fix mruby-redis command timeout issue
i110 Oct 6, 2017
66a627d
Merge branch 'master' into i110/mruby-redis
i110 Oct 20, 2017
70132d8
fix memory leak of mruby-redis
i110 Oct 20, 2017
6c8facb
Merge branch 'master' into i110/mruby-redis
i110 Jan 21, 2018
15c4ef7
tweak
i110 Jan 21, 2018
64b6077
remove redis error argument
i110 Jan 21, 2018
ee3cbc0
Merge branch 'master' into i110/mruby-redis
i110 Jan 26, 2018
3ead0da
refactor redis
i110 Jan 26, 2018
a872f9d
Merge branch 'i110/asan-symbols' into i110/mruby-redis
i110 Jan 27, 2018
c3d0f85
remove unnecessary NULL checks
i110 Jan 29, 2018
05fe2a6
rename close_connection to close_and_detach_connection, and move call…
i110 Jan 29, 2018
0e7f9be
rename h2o_redis_conn_t to h2o_redis_client_t
i110 Jan 29, 2018
dcad6e5
remove unnecessary enum definition
i110 Jan 29, 2018
9460efb
re-format changes
i110 Jan 29, 2018
3c997a9
fix heap-use-after-free problem
i110 Jan 29, 2018
ee0494c
Merge branch 'master' into i110/mruby-redis
kazuho Mar 7, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix mruby-redis gc problem
  • Loading branch information
i110 committed Mar 16, 2017
commit 4275123054a5abde5a86d2a63bdad3198c9254f5
78 changes: 33 additions & 45 deletions lib/handler/mruby/redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,65 +59,45 @@ redisReplyObjectFunctions reader_object_functions = {
reader_free_object
};

static void on_gc_dispose_redis(mrb_state *mrb, void *_conn)
{
struct st_h2o_mruby_redis_conn_t *conn = _conn;
if (conn == NULL) return;

h2o_redis_free(&conn->super);
conn->refs.redis = mrb_nil_value();
}

static void on_gc_dispose_command(mrb_state *mrb, void *_ctx)
{
struct st_h2o_mruby_redis_command_context_t *ctx = _ctx;
if (ctx == NULL) return;

ctx->refs.command = mrb_nil_value();
}

const static struct mrb_data_type redis_type = {"redis", on_gc_dispose_redis};
const static struct mrb_data_type command_type = {"redis_command", on_gc_dispose_command};

static mrb_value create_downstream_closed_exception(mrb_state *mrb)
{
return mrb_exc_new_str_lit(mrb, E_RUNTIME_ERROR, "downstream HTTP closed");
}

static void attach_receiver(struct st_h2o_mruby_redis_command_context_t *ctx, mrb_value receiver)
{
assert(mrb_nil_p(ctx->receiver));
ctx->receiver = receiver;
mrb_gc_register(ctx->conn->ctx->shared->mrb, receiver);
}

static mrb_value detach_receiver(struct st_h2o_mruby_redis_command_context_t *ctx)
static mrb_value detach_receiver(struct st_h2o_mruby_redis_command_context_t *ctx, int protect)
{
mrb_value ret = ctx->receiver;
assert(!mrb_nil_p(ret));
ctx->receiver = mrb_nil_value();
mrb_gc_unregister(ctx->conn->ctx->shared->mrb, ret);
mrb_gc_protect(ctx->conn->ctx->shared->mrb, ret);
if (protect) mrb_gc_protect(ctx->conn->ctx->shared->mrb, ret);
return ret;
}

static void on_command_dispose(void *_ctx)
static void on_gc_dispose_redis(mrb_state *mrb, void *_conn)
{
struct st_h2o_mruby_redis_command_context_t *ctx = _ctx;
struct st_h2o_mruby_redis_conn_t *conn = _conn;
if (conn == NULL) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance that conn becomes NULL? Unless there's such case, this early return is redundant with the potential of hiding a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed at c3d0f85


/* TODO: is this necessary? */
if (!mrb_nil_p(ctx->refs.command))
DATA_PTR(ctx->refs.command) = NULL;
h2o_redis_free(&conn->super);
conn->refs.redis = mrb_nil_value();
}

/* notify the app, if it is waiting to hear from us */
if (!mrb_nil_p(ctx->receiver)) {
mrb_state *mrb = ctx->conn->ctx->shared->mrb;
int gc_arena = mrb_gc_arena_save(mrb);
h2o_mruby_run_fiber(ctx->conn->ctx, detach_receiver(ctx), create_downstream_closed_exception(mrb), NULL);
mrb_gc_arena_restore(mrb, gc_arena);
static void on_gc_dispose_command(mrb_state *mrb, void *_ctx)
{
struct st_h2o_mruby_redis_command_context_t *ctx = _ctx;
if (ctx == NULL) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed at c3d0f85

if (! mrb_nil_p(ctx->receiver)) {
detach_receiver(ctx, 0);
}
free(ctx);
}

const static struct mrb_data_type redis_type = {"redis", on_gc_dispose_redis};
const static struct mrb_data_type command_type = {"redis_command", on_gc_dispose_command};

static redisReader *create_reader(h2o_redis_conn_t *_conn)
{
struct st_h2o_mruby_redis_conn_t *conn = (void *)_conn;
Expand Down Expand Up @@ -187,7 +167,7 @@ static void on_redis_command(void *_reply, void *_ctx)
}
} else {
int gc_arena = mrb_gc_arena_save(mrb);
h2o_mruby_run_fiber(ctx->conn->ctx, detach_receiver(ctx), reply, NULL);
h2o_mruby_run_fiber(ctx->conn->ctx, detach_receiver(ctx, 1), reply, NULL);
mrb_gc_arena_restore(mrb, gc_arena);
}
}
Expand All @@ -199,7 +179,6 @@ static mrb_value call_method(mrb_state *mrb, mrb_value self)

/* allocate context and initialize */
struct st_h2o_mruby_redis_command_context_t *command_ctx = h2o_mem_alloc(sizeof(*command_ctx));
// FIXME: who and when free this?
memset(command_ctx, 0, sizeof(*command_ctx));
command_ctx->conn = conn;
command_ctx->receiver = mrb_nil_value();
Expand All @@ -212,17 +191,26 @@ static mrb_value call_method(mrb_state *mrb, mrb_value self)

const char **argv = h2o_mem_alloc(command_len * sizeof(char *));
size_t *argvlen = h2o_mem_alloc(command_len * sizeof(size_t));

int gc_arena = mrb_gc_arena_save(mrb);

for (i = 0; i != command_len; ++i) {
int gc_arena = mrb_gc_arena_save(mrb);
mrb_value s = mrb_obj_as_string(mrb, command_args[i]);
argv[i] = mrb_str_to_cstr(mrb, s);
argvlen[i] = mrb_string_value_len(mrb, s);
mrb_gc_arena_restore(mrb, gc_arena);
if (mrb_symbol_p(command_args[i])) {
mrb_int len;
argv[i] = mrb_sym2name_len(mrb, mrb_symbol(command_args[i]), &len);
argvlen[i] = len;
} else {
mrb_value s = mrb_obj_as_string(mrb, command_args[i]);
argv[i] = mrb_string_value_cstr(mrb, &s);
argvlen[i] = mrb_string_value_len(mrb, s);
}
}

/* send command to redis */
h2o_redis_command_argv(&conn->super, on_redis_command, command_ctx, (int)command_len, argv, argvlen);

mrb_gc_arena_restore(mrb, gc_arena);

free(argv);
free(argvlen);

Expand Down