-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
694 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/common/proxy_wasm/ngx_proxy_wasm_foreign_callback.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#ifndef DDEBUG | ||
#define DDEBUG 0 | ||
#endif | ||
#include "ddebug.h" | ||
|
||
#include <ngx_proxy_wasm_foreign_callback.h> | ||
|
||
|
||
void | ||
ngx_proxy_wasm_foreign_callback_destroy(ngx_proxy_wasm_foreign_cb_t *cb) | ||
{ | ||
|
||
ngx_pfree(cb->pwexec->pool, cb); | ||
} | ||
|
||
|
||
ngx_proxy_wasm_foreign_cb_t * | ||
ngx_proxy_wasm_foreign_callback_alloc(ngx_proxy_wasm_exec_t *pwexec) | ||
{ | ||
ngx_proxy_wasm_foreign_cb_t *cb; | ||
|
||
cb = ngx_palloc(pwexec->pool, sizeof(ngx_proxy_wasm_foreign_cb_t)); | ||
cb->pwexec = pwexec; | ||
|
||
return cb; | ||
} | ||
|
||
|
||
void | ||
ngx_proxy_wasm_foreign_callback(ngx_proxy_wasm_foreign_cb_t *cb) | ||
{ | ||
ngx_proxy_wasm_exec_t *pwexec = cb->pwexec; | ||
ngx_proxy_wasm_err_e ecode = NGX_PROXY_WASM_ERR_NONE; | ||
ngx_proxy_wasm_step_e step = pwexec->parent->step; | ||
|
||
ngx_queue_remove(&cb->q); | ||
pwexec->fcallback = cb; | ||
|
||
#if (NGX_WASM_HTTP) | ||
pwexec->parent->phase = ngx_wasm_phase_lookup(&ngx_http_wasm_subsystem, | ||
NGX_WASM_BACKGROUND_PHASE); | ||
#endif | ||
|
||
ecode = ngx_proxy_wasm_run_step(pwexec, | ||
NGX_PROXY_WASM_STEP_FOREIGN_CALLBACK); | ||
if (ecode != NGX_PROXY_WASM_ERR_NONE) { | ||
/* TODO: error handling */ | ||
} | ||
|
||
pwexec->parent->step = step; | ||
pwexec->fcallback = NULL; | ||
|
||
if (ngx_proxy_wasm_foreign_callbacks_total(pwexec)) { | ||
ngx_log_debug0(NGX_LOG_DEBUG_WASM, pwexec->log, 0, | ||
"proxy_wasm more foreign function callbacks pending..."); | ||
|
||
#if (NGX_WASM_HTTP) | ||
ngx_wasm_yield(&cb->rctx->env); | ||
#endif | ||
ngx_proxy_wasm_ctx_set_next_action(pwexec->parent, | ||
NGX_PROXY_WASM_ACTION_PAUSE); | ||
|
||
} else { | ||
ngx_log_debug0(NGX_LOG_DEBUG_WASM, pwexec->log, 0, | ||
"proxy_wasm last foreign function callback handled"); | ||
|
||
#if (NGX_WASM_HTTP) | ||
ngx_wasm_continue(&cb->rctx->env); | ||
#endif | ||
ngx_proxy_wasm_ctx_set_next_action(pwexec->parent, | ||
NGX_PROXY_WASM_ACTION_CONTINUE); | ||
|
||
/* resume current step if unfinished */ | ||
ngx_proxy_wasm_resume(pwexec->parent, pwexec->parent->phase, step); | ||
} | ||
|
||
ngx_proxy_wasm_foreign_callback_destroy(cb); | ||
} | ||
|
||
|
||
ngx_int_t | ||
ngx_proxy_wasm_foreign_callback_buffer_create(ngx_proxy_wasm_foreign_cb_t *cb, | ||
size_t size) | ||
{ | ||
ngx_buf_t *b; | ||
ngx_chain_t *cl; | ||
ngx_proxy_wasm_exec_t *pwexec = cb->pwexec; | ||
|
||
ngx_wa_assert(pwexec); | ||
ngx_wa_assert(size); | ||
|
||
cl = ngx_alloc_chain_link(pwexec->pool); | ||
if (cl == NULL) { | ||
return NGX_ERROR; | ||
} | ||
|
||
/* TODO: if size exceeds a threshold, split allocation into N buffers */ | ||
|
||
b = ngx_create_temp_buf(pwexec->pool, size); | ||
if (b == NULL) { | ||
return NGX_ERROR; | ||
} | ||
|
||
cl->buf = b; | ||
cl->next = NULL; | ||
|
||
cb->args_out = cl; | ||
|
||
return NGX_OK; | ||
} | ||
|
||
|
||
ngx_int_t | ||
ngx_proxy_wasm_foreign_callback_buffer_write(ngx_proxy_wasm_foreign_cb_t *cb, | ||
ngx_str_t *data) | ||
{ | ||
size_t b_size; | ||
ngx_buf_t *b = cb->args_out->buf; | ||
|
||
b_size = b->end - b->start; | ||
|
||
ngx_wa_assert(data->len <= b_size); | ||
|
||
if (data->len <= b_size) { | ||
b->last = ngx_cpymem(b->last, data->data, data->len); | ||
} | ||
|
||
/* TODO: data->len > b_size */ | ||
|
||
return NGX_OK; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef _NGX_PROXY_WASM_FOREIGN_CALLBACK_H_INCLUDED_ | ||
#define _NGX_PROXY_WASM_FOREIGN_CALLBACK_H_INCLUDED_ | ||
|
||
|
||
#include <ngx_wasm.h> | ||
#include <ngx_proxy_wasm.h> | ||
#include <ngx_wasm_subsystem.h> | ||
|
||
|
||
struct ngx_proxy_wasm_foreign_cb_s { | ||
ngx_queue_t q; | ||
ngx_proxy_wasm_exec_t *pwexec; | ||
#if (NGX_WASM_HTTP) | ||
ngx_http_wasm_req_ctx_t *rctx; | ||
#endif | ||
ngx_proxy_wasm_foreign_function_e fcode; | ||
ngx_chain_t *args_out; | ||
}; | ||
|
||
|
||
ngx_proxy_wasm_foreign_cb_t * ngx_proxy_wasm_foreign_callback_alloc( | ||
ngx_proxy_wasm_exec_t *pwexec); | ||
void ngx_proxy_wasm_foreign_callback(ngx_proxy_wasm_foreign_cb_t *cb); | ||
ngx_int_t ngx_proxy_wasm_foreign_callback_buffer_create( | ||
ngx_proxy_wasm_foreign_cb_t *cb, size_t size); | ||
ngx_int_t ngx_proxy_wasm_foreign_callback_buffer_write( | ||
ngx_proxy_wasm_foreign_cb_t *cb, ngx_str_t *data); | ||
void ngx_proxy_wasm_foreign_callback_destroy(ngx_proxy_wasm_foreign_cb_t *cb); | ||
#endif |
Oops, something went wrong.