diff --git a/src/stream/ngx_stream_mruby_async.c b/src/stream/ngx_stream_mruby_async.c index 3f318c1b..9ac85588 100644 --- a/src/stream/ngx_stream_mruby_async.c +++ b/src/stream/ngx_stream_mruby_async.c @@ -17,16 +17,17 @@ typedef struct { mrb_state *mrb; mrb_value *fiber; ngx_stream_session_t *s; + ngx_int_t stream_status; } ngx_stream_mrb_reentrant_t; -static mrb_value ngx_stream_mrb_run_fiber(mrb_state *mrb, mrb_value *fiber_proc, mrb_value *result) +static mrb_value ngx_stream_mrb_run_fiber(ngx_stream_session_t *s, mrb_state *mrb, mrb_value *fiber_proc, + mrb_value *result) { mrb_value resume_result = mrb_nil_value(); mrb_value aliving = mrb_false_value(); mrb_value handler_result = mrb_nil_value(); ngx_stream_mruby_ctx_t *ctx; - ngx_stream_session_t *s = ngx_mrb_get_session(); ctx = ngx_stream_mrb_get_module_ctx(mrb, s); ctx->fiber_proc = fiber_proc; @@ -72,19 +73,20 @@ mrb_value ngx_stream_mrb_start_fiber(ngx_stream_session_t *s, mrb_state *mrb, st return mrb_false_value(); } - return ngx_stream_mrb_run_fiber(mrb, fiber_proc, result); + return ngx_stream_mrb_run_fiber(s, mrb, fiber_proc, result); } static ngx_int_t ngx_stream_mrb_post_fiber(ngx_stream_mrb_reentrant_t *re, ngx_stream_mruby_ctx_t *ctx) { int ai; ai = mrb_gc_arena_save(re->mrb); + ngx_stream_mruby_internal_ctx_t *ictx = re->mrb->ud; + ictx->s = re->s; + ictx->stream_status = re->stream_status; if (re->fiber != NULL) { - ngx_mrb_push_session(re->s); - - if (mrb_test(ngx_stream_mrb_run_fiber(re->mrb, re->fiber, ctx->async_handler_result))) { + if (mrb_test(ngx_stream_mrb_run_fiber(re->s, re->mrb, re->fiber, ctx->async_handler_result))) { mrb_gc_arena_restore(re->mrb, ai); return NGX_DONE; } else { @@ -145,8 +147,8 @@ static mrb_value ngx_stream_mrb_async_sleep(mrb_state *mrb, mrb_value self) mrb_int timer; u_char *p; ngx_stream_mrb_reentrant_t *re; - ngx_stream_session_t *s; ngx_stream_mruby_ctx_t *ctx; + ngx_stream_mruby_internal_ctx_t *ictx; mrb_get_args(mrb, "i", &timer); @@ -154,13 +156,15 @@ static mrb_value ngx_stream_mrb_async_sleep(mrb_state *mrb, mrb_value self) mrb_raise(mrb, E_ARGUMENT_ERROR, "value of the timer must be a positive number"); } - s = ngx_mrb_get_session(); - p = ngx_palloc(s->connection->pool, sizeof(ngx_event_t) + sizeof(ngx_stream_mrb_reentrant_t)); + ictx = mrb->ud; + p = ngx_palloc(ictx->s->connection->pool, sizeof(ngx_event_t) + sizeof(ngx_stream_mrb_reentrant_t)); re = (ngx_stream_mrb_reentrant_t *)(p + sizeof(ngx_event_t)); re->mrb = mrb; - re->s = s; - ctx = ngx_stream_mrb_get_module_ctx(mrb, s); + re->s = ictx->s; + re->stream_status = ictx->stream_status; + + ctx = ngx_stream_mrb_get_module_ctx(mrb, ictx->s); re->fiber = ctx->fiber_proc; // keeps the object from GC when can resume the fiber diff --git a/src/stream/ngx_stream_mruby_module.c b/src/stream/ngx_stream_mruby_module.c index 6d455404..893f0b9d 100644 --- a/src/stream/ngx_stream_mruby_module.c +++ b/src/stream/ngx_stream_mruby_module.c @@ -678,18 +678,6 @@ static ngx_int_t ngx_stream_mrb_run_conf(ngx_conf_t *cf, mrb_state *mrb, ngx_mrb } */ -ngx_stream_session_t *ngx_mruby_session = NULL; -ngx_int_t ngx_mrb_push_session(ngx_stream_session_t *s) -{ - ngx_mruby_session = s; - return NGX_OK; -} - -ngx_stream_session_t *ngx_mrb_get_session(void) -{ - return ngx_mruby_session; -} - static ngx_int_t ngx_stream_mruby_handler(ngx_stream_session_t *s) { ngx_stream_mruby_srv_conf_t *mscf; @@ -712,7 +700,6 @@ static ngx_int_t ngx_stream_mruby_handler(ngx_stream_session_t *s) mrb_value *mrb_result = (mrb_value *)ngx_palloc(s->connection->pool, sizeof(mrb_value)); *mrb_result = mrb_nil_value(); - ngx_mrb_push_session(s); if (mrb_test(ngx_stream_mrb_start_fiber(s, mrb, mscf->code->proc, mrb_result))) { ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, "%s INFO %s:%d: already can resume this fiber", MODULE_NAME, __func__, __LINE__); diff --git a/src/stream/ngx_stream_mruby_module.h b/src/stream/ngx_stream_mruby_module.h index ea543714..eeb26e1e 100644 --- a/src/stream/ngx_stream_mruby_module.h +++ b/src/stream/ngx_stream_mruby_module.h @@ -55,7 +55,5 @@ typedef struct { } ngx_stream_mruby_srv_conf_t; -ngx_int_t ngx_mrb_push_session(ngx_stream_session_t *s); -ngx_stream_session_t *ngx_mrb_get_session(void); void ngx_stream_mruby_raise_error(mrb_state *mrb, mrb_value obj, ngx_stream_session_t *s); #endif // NGX_STREAM_MRUBY_MODULE_H