-
Notifications
You must be signed in to change notification settings - Fork 56
feat: support command cas, stats $memc_value #34
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ static ngx_str_t ngx_http_memc_cmd = ngx_string("memc_cmd"); | |
static ngx_str_t ngx_http_memc_value = ngx_string("memc_value"); | ||
static ngx_str_t ngx_http_memc_flags = ngx_string("memc_flags"); | ||
static ngx_str_t ngx_http_memc_exptime = ngx_string("memc_exptime"); | ||
static ngx_str_t ngx_http_memc_unique_token = ngx_string("memc_unique_token"); | ||
|
||
|
||
static ngx_int_t ngx_http_memc_add_more_variables(ngx_conf_t *cf); | ||
|
@@ -68,6 +69,7 @@ ngx_http_memc_handler(ngx_http_request_t *r) | |
ngx_http_variable_value_t *value_vv; | ||
ngx_http_variable_value_t *flags_vv; | ||
ngx_http_variable_value_t *exptime_vv; | ||
ngx_http_variable_value_t *unique_token_vv; | ||
|
||
ngx_http_memc_cmd_t memc_cmd; | ||
ngx_flag_t is_storage_cmd = 0; | ||
|
@@ -257,9 +259,13 @@ ngx_http_memc_handler(ngx_http_request_t *r) | |
u->input_filter_init = ngx_http_memc_empty_filter_init; | ||
u->input_filter = ngx_http_memc_empty_filter; | ||
|
||
} else if (memc_cmd == ngx_http_memc_cmd_version | ||
|| memc_cmd == ngx_http_memc_cmd_stats) | ||
{ | ||
} else if (memc_cmd == ngx_http_memc_cmd_stats) { | ||
u->create_request = ngx_http_memc_create_stats_cmd_request; | ||
u->process_header = ngx_http_memc_process_simple_header; | ||
|
||
u->input_filter_init = ngx_http_memc_empty_filter_init; | ||
u->input_filter = ngx_http_memc_empty_filter; | ||
} else if (memc_cmd == ngx_http_memc_cmd_version) { | ||
u->create_request = ngx_http_memc_create_noarg_cmd_request; | ||
u->process_header = ngx_http_memc_process_simple_header; | ||
|
||
|
@@ -344,6 +350,7 @@ ngx_http_memc_handler(ngx_http_request_t *r) | |
|
||
if (is_storage_cmd | ||
|| memc_cmd == ngx_http_memc_cmd_incr | ||
|| memc_cmd == ngx_http_memc_cmd_stats | ||
|| memc_cmd == ngx_http_memc_cmd_decr) | ||
{ | ||
value_vv = ngx_http_get_indexed_variable(r, mmcf->value_index); | ||
|
@@ -376,6 +383,31 @@ ngx_http_memc_handler(ngx_http_request_t *r) | |
ctx->memc_value_vv = value_vv; | ||
} | ||
|
||
if (memc_cmd == ngx_http_memc_cmd_cas) { | ||
unique_token_vv = ngx_http_get_indexed_variable(r, mmcf->unique_token_index); | ||
if (unique_token_vv == NULL) { | ||
return NGX_HTTP_INTERNAL_SERVER_ERROR; | ||
} | ||
|
||
if (unique_token_vv->not_found || unique_token_vv->len == 0) { | ||
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
"the \"$memc_unique_token\" variable is required for " | ||
"command \"%V\"", &ctx->cmd_str); | ||
|
||
return NGX_HTTP_BAD_REQUEST; | ||
} | ||
|
||
if (!ngx_http_memc_valid_uint32_str(unique_token_vv->data, | ||
unique_token_vv->len)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: See the nginx coding style doc for more details: |
||
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
"variable \"$memc_unique_token\" takes invalid value: %v", | ||
unique_token_vv); | ||
|
||
return NGX_HTTP_BAD_REQUEST; | ||
} | ||
ctx->memc_unique_token_vv = unique_token_vv; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs a blank line before this line. |
||
} | ||
|
||
rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); | ||
|
||
if (rc == NGX_ERROR || rc > NGX_OK) { | ||
|
@@ -512,6 +544,11 @@ ngx_http_memc_init(ngx_conf_t *cf) | |
return NGX_ERROR; | ||
} | ||
|
||
mmcf->unique_token_index = ngx_http_memc_add_variable(cf, &ngx_http_memc_unique_token); | ||
if (mmcf->unique_token_index == NGX_ERROR) { | ||
return NGX_ERROR; | ||
} | ||
|
||
return ngx_http_memc_add_more_variables(cf); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ ngx_http_memc_create_storage_cmd_request(ngx_http_request_t *r) | |
ngx_http_variable_value_t *flags_vv; | ||
ngx_http_variable_value_t *exptime_vv; | ||
ngx_http_variable_value_t *memc_value_vv; | ||
ngx_http_variable_value_t *unique_token_vv; | ||
|
||
u_char bytes_buf[NGX_UINT32_LEN]; | ||
|
||
|
@@ -124,6 +125,12 @@ ngx_http_memc_create_storage_cmd_request(ngx_http_request_t *r) | |
+ bytes_len | ||
+ sizeof(CRLF) - 1; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only 1 blank line should be used here. |
||
unique_token_vv = ctx->memc_unique_token_vv; | ||
if (ctx->cmd == ngx_http_memc_cmd_cas) { | ||
len += unique_token_vv->len + sizeof(" ") - 1; | ||
} | ||
|
||
b = ngx_create_temp_buf(r->pool, len); | ||
if (b == NULL) { | ||
return NGX_ERROR; | ||
|
@@ -182,6 +189,12 @@ ngx_http_memc_create_storage_cmd_request(ngx_http_request_t *r) | |
|
||
b->last = ngx_copy(b->last, bytes_buf, bytes_len); | ||
|
||
/* copy cas unique token */ | ||
if (ctx->cmd == ngx_http_memc_cmd_cas) { | ||
*b->last++ = ' '; | ||
b->last = ngx_copy(b->last, unique_token_vv->data, unique_token_vv->len); | ||
} | ||
|
||
*b->last++ = CR; *b->last++ = LF; | ||
|
||
if (memc_value_vv) { | ||
|
@@ -325,6 +338,53 @@ ngx_http_memc_create_get_cmd_request(ngx_http_request_t *r) | |
return NGX_OK; | ||
} | ||
|
||
|
||
ngx_int_t | ||
ngx_http_memc_create_stats_cmd_request(ngx_http_request_t *r) | ||
{ | ||
size_t len; | ||
ngx_buf_t *b; | ||
ngx_http_memc_ctx_t *ctx; | ||
ngx_chain_t *cl; | ||
ngx_http_variable_value_t *value_vv; | ||
|
||
ctx = ngx_http_get_module_ctx(r, ngx_http_memc_module); | ||
|
||
len = ctx->cmd_str.len + sizeof(CRLF) - 1; | ||
|
||
value_vv = ctx->memc_value_vv; | ||
if (value_vv && value_vv->not_found == 0 && value_vv->len) { | ||
len += value_vv->len + sizeof(" ") - 1; | ||
} | ||
|
||
b = ngx_create_temp_buf(r->pool, len); | ||
if (b == NULL) { | ||
return NGX_ERROR; | ||
} | ||
|
||
cl = ngx_alloc_chain_link(r->pool); | ||
if (cl == NULL) { | ||
return NGX_ERROR; | ||
} | ||
|
||
cl->buf = b; | ||
cl->next = NULL; | ||
|
||
r->upstream->request_bufs = cl; | ||
|
||
b->last = ngx_copy(b->last, ctx->cmd_str.data, ctx->cmd_str.len); | ||
|
||
if (value_vv && value_vv->not_found == 0 && value_vv->len) { | ||
*b->last++ = ' '; | ||
b->last = ngx_copy(b->last, value_vv->data, value_vv->len); | ||
} | ||
|
||
*b->last++ = CR; *b->last++ = LF; | ||
|
||
return NGX_OK; | ||
} | ||
|
||
|
||
ngx_int_t | ||
ngx_http_memc_create_noarg_cmd_request(ngx_http_request_t *r) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: needs a blank line before
} else if ...
.