Skip to content

Commit ebe1006

Browse files
committed
add balancer_ssl_session_fetchby and balancer_ssl_session_storeby
1 parent 612931c commit ebe1006

11 files changed

+735
-41
lines changed

config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ HTTP_LUA_SRCS=" \
361361
$ngx_addon_dir/src/ngx_http_lua_ssl_session_fetchby.c \
362362
$ngx_addon_dir/src/ngx_http_lua_ssl.c \
363363
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.c \
364+
$ngx_addon_dir/src/ngx_http_lua_balancer_ssl_session_fetchby.c \
365+
$ngx_addon_dir/src/ngx_http_lua_balancer_ssl_session_storeby.c \
364366
"
365367

366368
HTTP_LUA_DEPS=" \
@@ -422,6 +424,8 @@ HTTP_LUA_DEPS=" \
422424
$ngx_addon_dir/src/ngx_http_lua_ssl_session_fetchby.h \
423425
$ngx_addon_dir/src/ngx_http_lua_ssl.h \
424426
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.h \
427+
$ngx_addon_dir/src/ngx_http_lua_balancer_ssl_session_fetchby.h \
428+
$ngx_addon_dir/src/ngx_http_lua_balancer_ssl_session_storeby.h \
425429
"
426430

427431
CFLAGS="$CFLAGS -DNDK_SET_VAR"

src/ngx_http_lua_balancer.c

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,8 @@
1414
#include "ngx_http_lua_balancer.h"
1515
#include "ngx_http_lua_util.h"
1616
#include "ngx_http_lua_directive.h"
17-
18-
19-
struct ngx_http_lua_balancer_peer_data_s {
20-
/* the round robin data must be first */
21-
ngx_http_upstream_rr_peer_data_t rrp;
22-
23-
ngx_http_lua_srv_conf_t *conf;
24-
ngx_http_request_t *request;
25-
26-
ngx_uint_t more_tries;
27-
ngx_uint_t total_tries;
28-
29-
struct sockaddr *sockaddr;
30-
socklen_t socklen;
31-
32-
ngx_str_t *host;
33-
in_port_t port;
34-
35-
int last_peer_state;
36-
37-
#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
38-
unsigned cloned_upstream_conf; /* :1 */
39-
#endif
40-
};
17+
#include "ngx_http_lua_balancer_ssl_session_storeby.h"
18+
#include "ngx_http_lua_balancer_ssl_session_fetchby.h"
4119

4220

4321
#if (NGX_HTTP_SSL)
@@ -438,8 +416,11 @@ ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc, void *data)
438416
ngx_http_lua_balancer_peer_data_t *bp = data;
439417

440418
if (bp->sockaddr && bp->socklen) {
441-
/* TODO */
442-
return NGX_OK;
419+
if (bp->conf->balancer.ssl_sess_fetch_handler) {
420+
return ngx_http_lua_balancer_ssl_sess_fetch(pc, data);
421+
} else {
422+
return NGX_OK;
423+
}
443424
}
444425

445426
return ngx_http_upstream_set_round_robin_peer_session(pc, &bp->rrp);
@@ -452,7 +433,10 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
452433
ngx_http_lua_balancer_peer_data_t *bp = data;
453434

454435
if (bp->sockaddr && bp->socklen) {
455-
/* TODO */
436+
if (bp->conf->balancer.ssl_sess_store_handler) {
437+
ngx_http_lua_balancer_ssl_sess_store(pc, data);
438+
}
439+
456440
return;
457441
}
458442

src/ngx_http_lua_balancer.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010

1111
#include "ngx_http_lua_common.h"
1212

13+
struct ngx_http_lua_balancer_peer_data_s {
14+
/* the round robin data must be first */
15+
ngx_http_upstream_rr_peer_data_t rrp;
16+
17+
ngx_http_lua_srv_conf_t *conf;
18+
ngx_http_request_t *request;
19+
20+
ngx_uint_t more_tries;
21+
ngx_uint_t total_tries;
22+
23+
struct sockaddr *sockaddr;
24+
socklen_t socklen;
25+
26+
ngx_str_t *host;
27+
in_port_t port;
28+
29+
int last_peer_state;
30+
31+
#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
32+
unsigned cloned_upstream_conf; /* :1 */
33+
#endif
34+
};
1335

1436
ngx_int_t ngx_http_lua_balancer_handler_inline(ngx_http_request_t *r,
1537
ngx_http_lua_srv_conf_t *lscf, lua_State *L);
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
2+
/*
3+
* Copyright (C) Chenglong Zhang (kone)
4+
*/
5+
6+
7+
#ifndef DDEBUG
8+
#define DDEBUG 0
9+
#endif
10+
#include "ddebug.h"
11+
12+
#include "ngx_http_lua_balancer_ssl_session_fetchby.h"
13+
#include "ngx_http_lua_cache.h"
14+
#include "ngx_http_lua_util.h"
15+
#include "ngx_http_lua_directive.h"
16+
#include "ngx_http_lua_balancer.h"
17+
18+
19+
#if (NGX_HTTP_SSL)
20+
static ngx_int_t ngx_http_lua_balancer_ssl_sess_fetch_by_chunk(lua_State *L,
21+
ngx_http_request_t *r);
22+
23+
24+
ngx_int_t
25+
ngx_http_lua_balancer_ssl_sess_fetch_handler_file(ngx_http_request_t *r,
26+
ngx_http_lua_srv_conf_t *lscf, lua_State *L)
27+
{
28+
ngx_int_t rc;
29+
30+
rc = ngx_http_lua_cache_loadfile(r->connection->log, L,
31+
lscf->balancer.ssl_sess_fetch_src.data,
32+
lscf->balancer.ssl_sess_fetch_src_key);
33+
if (rc != NGX_OK) {
34+
return rc;
35+
}
36+
37+
/* make sure we have a valid code chunk */
38+
ngx_http_lua_assert(lua_isfunction(L, -1));
39+
40+
return ngx_http_lua_balancer_ssl_sess_fetch_by_chunk(L, r);
41+
}
42+
43+
44+
ngx_int_t
45+
ngx_http_lua_balancer_ssl_sess_fetch_handler_inline(ngx_http_request_t *r,
46+
ngx_http_lua_srv_conf_t *lscf, lua_State *L)
47+
{
48+
ngx_int_t rc;
49+
50+
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
51+
lscf->balancer.ssl_sess_fetch_src.data,
52+
lscf->balancer.ssl_sess_fetch_src.len,
53+
lscf->balancer.ssl_sess_fetch_src_key,
54+
"=balancer_ssl_session_fetch_by_lua");
55+
if (rc != NGX_OK) {
56+
return rc;
57+
}
58+
59+
/* make sure we have a valid code chunk */
60+
ngx_http_lua_assert(lua_isfunction(L, -1));
61+
62+
return ngx_http_lua_balancer_ssl_sess_fetch_by_chunk(L, r);
63+
}
64+
65+
66+
ngx_int_t
67+
ngx_http_lua_balancer_ssl_sess_fetch(ngx_peer_connection_t *pc, void *data)
68+
{
69+
lua_State *L;
70+
ngx_int_t rc;
71+
ngx_http_request_t *r;
72+
ngx_http_lua_ctx_t *ctx;
73+
ngx_http_lua_srv_conf_t *lscf;
74+
ngx_http_lua_balancer_peer_data_t *bp = data;
75+
76+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, "balancer ssl session fetch");
77+
78+
lscf = bp->conf;
79+
80+
r = bp->request;
81+
82+
ngx_http_lua_assert(lscf->balancer.ssl_sess_fetch_handler && r);
83+
84+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
85+
86+
if (ctx == NULL) {
87+
ctx = ngx_http_lua_create_ctx(r);
88+
if (ctx == NULL) {
89+
return NGX_ERROR;
90+
}
91+
92+
L = ngx_http_lua_get_lua_vm(r, ctx);
93+
94+
} else {
95+
L = ngx_http_lua_get_lua_vm(r, ctx);
96+
97+
dd("reset ctx");
98+
ngx_http_lua_reset_ctx(r, L, ctx);
99+
}
100+
101+
ctx->context = NGX_HTTP_LUA_CONTEXT_BALANCER_SSL_SESS_FETCH;
102+
103+
rc = lscf->balancer.ssl_sess_fetch_handler(r, lscf, L);
104+
105+
if (rc == NGX_ERROR) {
106+
return NGX_ERROR;
107+
}
108+
109+
if (ctx->exited && ctx->exit_code != NGX_OK) {
110+
rc = ctx->exit_code;
111+
if (rc == NGX_ERROR
112+
|| rc == NGX_BUSY
113+
|| rc == NGX_DECLINED
114+
#ifdef HAVE_BALANCER_STATUS_CODE_PATCH
115+
|| rc >= NGX_HTTP_SPECIAL_RESPONSE
116+
#endif
117+
) {
118+
return rc;
119+
}
120+
121+
if (rc > NGX_OK) {
122+
return NGX_ERROR;
123+
}
124+
}
125+
126+
return NGX_OK;
127+
}
128+
129+
130+
char *
131+
ngx_http_lua_balancer_ssl_sess_fetch_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
132+
void *conf)
133+
{
134+
char *rv;
135+
ngx_conf_t save;
136+
137+
save = *cf;
138+
cf->handler = ngx_http_lua_balancer_ssl_sess_fetch_by_lua;
139+
cf->handler_conf = conf;
140+
141+
rv = ngx_http_lua_conf_lua_block_parse(cf, cmd);
142+
143+
*cf = save;
144+
145+
return rv;
146+
}
147+
148+
149+
char *
150+
ngx_http_lua_balancer_ssl_sess_fetch_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
151+
void *conf)
152+
{
153+
u_char *p;
154+
u_char *name;
155+
ngx_str_t *value;
156+
ngx_http_lua_srv_conf_t *lscf = conf;
157+
158+
dd("enter");
159+
160+
/* must specify a content handler */
161+
if (cmd->post == NULL) {
162+
return NGX_CONF_ERROR;
163+
}
164+
165+
if(lscf->balancer.ssl_sess_fetch_handler) {
166+
return "is duplicate";
167+
}
168+
169+
value = cf->args->elts;
170+
171+
lscf->balancer.ssl_sess_fetch_handler = (ngx_http_lua_srv_conf_handler_pt) cmd->post;
172+
173+
if (cmd->post == ngx_http_lua_balancer_ssl_sess_fetch_handler_file) {
174+
/* Lua code in an external file */
175+
176+
name = ngx_http_lua_rebase_path(cf->pool, value[1].data,
177+
value[1].len);
178+
if (name == NULL) {
179+
return NGX_CONF_ERROR;
180+
}
181+
182+
lscf->balancer.ssl_sess_fetch_src.data = name;
183+
lscf->balancer.ssl_sess_fetch_src.len = ngx_strlen(name);
184+
185+
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
186+
if (p == NULL) {
187+
return NGX_CONF_ERROR;
188+
}
189+
190+
lscf->balancer.ssl_sess_fetch_src_key = p;
191+
192+
p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
193+
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
194+
*p = '\0';
195+
196+
} else {
197+
/* inlined Lua code */
198+
199+
lscf->balancer.ssl_sess_fetch_src = value[1];
200+
201+
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
202+
if (p == NULL) {
203+
return NGX_CONF_ERROR;
204+
}
205+
206+
lscf->balancer.ssl_sess_fetch_src_key = p;
207+
208+
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
209+
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
210+
*p = '\0';
211+
}
212+
213+
return NGX_CONF_OK;
214+
}
215+
216+
217+
static ngx_int_t
218+
ngx_http_lua_balancer_ssl_sess_fetch_by_chunk(lua_State *L, ngx_http_request_t *r)
219+
{
220+
u_char *err_msg;
221+
size_t len;
222+
ngx_int_t rc;
223+
224+
/* init nginx context in Lua VM */
225+
ngx_http_lua_set_req(L, r);
226+
ngx_http_lua_create_new_globals_table(L, 0 /* narr */, 1 /* nrec */);
227+
228+
/* {{{ make new env inheriting main thread's globals table */
229+
lua_createtable(L, 0, 1 /* nrec */); /* the metatable for the new env */
230+
ngx_http_lua_get_globals_table(L);
231+
lua_setfield(L, -2, "__index");
232+
lua_setmetatable(L, -2); /* setmetatable({}, {__index = _G}) */
233+
/* }}} */
234+
235+
lua_setfenv(L, -2); /* set new running env for the code closure */
236+
237+
lua_pushcfunction(L, ngx_http_lua_traceback);
238+
lua_insert(L, 1); /* put it under chunk and args */
239+
240+
/* protected call user code */
241+
rc = lua_pcall(L, 0, 1, 1);
242+
243+
lua_remove(L, 1); /* remove traceback function */
244+
245+
dd("rc == %d", (int) rc);
246+
247+
if (rc != 0) {
248+
/* error occurred when running loaded code */
249+
err_msg = (u_char *) lua_tolstring(L, -1, &len);
250+
251+
if (err_msg == NULL) {
252+
err_msg = (u_char *) "unknown reason";
253+
len = sizeof("unknown reason") - 1;
254+
}
255+
256+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
257+
"failed to run balancer_ssl_session_fetch_by_lua*: %*s", len, err_msg);
258+
259+
lua_settop(L, 0); /* clear remaining elems on stack */
260+
261+
return NGX_ERROR;
262+
}
263+
264+
lua_settop(L, 0); /* clear remaining elems on stack */
265+
return rc;
266+
}
267+
268+
#endif /* NGX_HTTP_SSL */

0 commit comments

Comments
 (0)