Skip to content

Commit

Permalink
Update Async example
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmod4n committed May 30, 2021
1 parent 9c74b9a commit b5619f7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Async Client

```ruby
async = Hiredis::Async.new #(callbacks = Hiredis::Async::Callbacks.new, evloop = RedisAe.new, host_or_path = "localhost", port = 6379)
async.evloop.run_once
async.queue(:set, "foo", "bar")
async.evloop.run_once
async.queue(:get, "foo") {|reply| puts reply}
Expand Down
23 changes: 13 additions & 10 deletions src/mrb_hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,19 @@ mrb_redisCallbackFn(struct redisAsyncContext *async_context, void *r, void *priv
if (async_context->c.flags & REDIS_FREEING)
return;

mrb_hiredis_async_context *mrb_async_context = (mrb_hiredis_async_context *) async_context->ev.data;
mrb_state *mrb = mrb_async_context->mrb;
mrb_assert(mrb);

int ai = mrb_gc_arena_save(mrb);
mrb_value reply = mrb_hiredis_get_reply((redisReply *) r, mrb);
mrb_value block = mrb_obj_value(privdata);
mrb_funcall(mrb, mrb_async_context->replies, "delete", 1, block);
mrb_yield(mrb, block, reply);
mrb_gc_arena_restore(mrb, ai);
if (r) {
mrb_hiredis_async_context *mrb_async_context = (mrb_hiredis_async_context *)async_context->ev.data;
mrb_state *mrb = mrb_async_context->mrb;
mrb_assert(mrb);
int ai = mrb_gc_arena_save(mrb);
mrb_value reply = mrb_hiredis_get_reply((redisReply *)r, mrb);
mrb_value block = mrb_obj_value(privdata);
if (likely(mrb_type(block) == MRB_TT_PROC)) {
mrb_funcall(mrb, mrb_async_context->replies, "delete", 1, block);
mrb_yield(mrb, block, reply);
}
mrb_gc_arena_restore(mrb, ai);
}
}

static mrb_value
Expand Down
1 change: 1 addition & 0 deletions test/hiredis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

assert("Hiredis::Async") do
async = Hiredis::Async.new
async.evloop.run_once
async.queue(:del, "mruby-hiredis-test:foo")
async.evloop.run_once
async.queue(:incr, "mruby-hiredis-test:foo") {|reply| assert_equal(1, reply)}
Expand Down

0 comments on commit b5619f7

Please sign in to comment.