Skip to content

Commit 3a2d459

Browse files
committed
update init and exit function , move to cycle phase at least create an init_cycle and exit_cycle function block with cycle argument, header update
1 parent 2226793 commit 3a2d459

File tree

4 files changed

+135
-22
lines changed

4 files changed

+135
-22
lines changed

build_test_resources/linkfuntest.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
int is_service_on = 0;
1717

18-
void ngx_link_func_init(ngx_link_func_ctx_t* ctx) {
19-
ngx_link_func_log(info, ctx, "%s", "Starting The Application");
18+
void ngx_link_func_init_cycle(ngx_link_func_cycle_t* cycle) {
19+
ngx_link_func_cyc_log(info, cycle, "%s", "Starting The Application");
2020

2121
is_service_on = 1;
2222
}
@@ -277,11 +277,11 @@ void my_app_simple_get_no_resp(ngx_link_func_ctx_t *ctx) {
277277
}
278278

279279

280-
void ngx_link_func_exit(ngx_link_func_ctx_t* ctx) {
280+
void ngx_link_func_exit_cycle(ngx_link_func_cycle_t* cycle) {
281281

282-
ngx_link_func_cache_remove(ctx->shared_mem, "key");
282+
ngx_link_func_cache_remove(cycle->shared_mem, "key");
283283

284-
ngx_link_func_log(info, ctx, "%s\n", "Shutting down The Application");
284+
ngx_link_func_cyc_log(info, cycle, "%s\n", "Shutting down The Application");
285285

286286
is_service_on = 0;
287287
}

config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ngx_feature_incs="#include <ngx_link_func_module.h>"
2424
ngx_feature_path=
2525
ngx_feature_libs=
2626
# ngx_feature_exit_if_not_found=yes
27-
ngx_feature_test="int ngx_link_func_module_current_version_=ngx_link_func_module_version_32;"
27+
ngx_feature_test="int ngx_link_func_module_current_version_=ngx_link_func_module_version_33;"
2828
. auto/feature
2929

3030
if [ $ngx_found != yes ]; then

src/ngx_link_func_module.c

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ typedef struct {
8484
} ngx_http_link_func_main_conf_t;
8585

8686
typedef void (*ngx_http_link_func_app_handler)(ngx_link_func_ctx_t*);
87+
typedef void (*ngx_http_link_func_app_cycle_handler)(ngx_link_func_cycle_t*);
8788

8889
typedef struct {
8990
void *_app;
@@ -203,6 +204,12 @@ static ngx_http_link_func_http_header_body* ngx_http_link_func_https_request( ng
203204
/*** End Download Feature Support ***/
204205

205206
/**Extern interface**/
207+
void ngx_link_func_cyc_log_debug(ngx_link_func_cycle_t *cyc, const char* msg);
208+
void ngx_link_func_cyc_log_info(ngx_link_func_cycle_t *cyc, const char* msg);
209+
void ngx_link_func_cyc_log_warn(ngx_link_func_cycle_t *cyc, const char* msg);
210+
void ngx_link_func_cyc_log_err(ngx_link_func_cycle_t *cyc, const char* msg);
211+
u_char* ngx_link_func_cyc_get_prop(ngx_link_func_cycle_t *cyc, const char *key, size_t keylen);
212+
206213
void ngx_link_func_log_debug(ngx_link_func_ctx_t *ctx, const char* msg);
207214
void ngx_link_func_log_info(ngx_link_func_ctx_t *ctx, const char* msg);
208215
void ngx_link_func_log_warn(ngx_link_func_ctx_t *ctx, const char* msg);
@@ -634,18 +641,25 @@ static ngx_int_t
634641
ngx_http_link_func_proceed_init_calls(ngx_cycle_t* cycle, ngx_http_link_func_srv_conf_t *scf, ngx_http_link_func_main_conf_t* mcf) {
635642
/**** Init the client apps ngx_http_link_func_init ***/
636643
char *error;
637-
ngx_http_link_func_app_handler func;
638-
*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_init");
644+
ngx_http_link_func_app_cycle_handler func;
645+
*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_init_cycle");
639646
if ((error = dlerror()) != NULL) {
640647
ngx_log_error(NGX_LOG_WARN, cycle->log, 0, "Unable to init call %s , skipped init called", error);
641648
} else {
642649
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "application initializing");
643650
/*** Init the apps ***/
644-
ngx_link_func_ctx_t new_ctx; //config request
645-
new_ctx.__pl__ = cycle->pool;
646-
new_ctx.__log__ = cycle->log;
647-
new_ctx.shared_mem = (void*)mcf->shm_ctx->shared_mem;
648-
func(&new_ctx);
651+
ngx_link_func_cycle_t appcyc;
652+
appcyc.has_error = 0;
653+
appcyc.__cycle__ = cycle;
654+
appcyc.__srv_cf__ = scf;
655+
appcyc.__pl__ = cycle->pool;
656+
appcyc.__log__ = cycle->log;
657+
appcyc.shared_mem = (void*)mcf->shm_ctx->shared_mem;
658+
func(&appcyc);
659+
if(appcyc.has_error) {
660+
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "%s", "link function worker Initialize unsuccessfully");
661+
return NGX_ERROR;
662+
}
649663
}
650664

651665
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "%s", "Done proceed init calls");
@@ -729,7 +743,7 @@ ngx_http_link_func_pre_configuration(ngx_conf_t *cf) {
729743
return NGX_ERROR;
730744
#endif
731745

732-
#ifndef ngx_link_func_module_version_32
746+
#ifndef ngx_link_func_module_version_33
733747
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", "the ngx_http_link_func_module.h might not be latest or not found in the c header path, \
734748
please copy latest ngx_http_link_func_module.h to your /usr/include or /usr/local/include or relavent header search path \
735749
with read and write permission.");
@@ -775,6 +789,24 @@ ngx_http_link_func_application_compatibility_check(ngx_conf_t *cf, ngx_http_core
775789
}
776790

777791
char *error;
792+
793+
/* * check init function block, this version has to be at least init with empty function * */
794+
ngx_http_link_func_app_cycle_handler func;
795+
*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_init_cycle");
796+
if ((error = dlerror()) != NULL) {
797+
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
798+
"function ngx_link_func_init_cycle(ngx_link_func_cycle_t *cycle) not found in \"%V\", at least create an empty init function block \n %s",
799+
&scf->_libname, error);
800+
return NGX_ERROR;
801+
}
802+
*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_exit_cycle");
803+
if ((error = dlerror()) != NULL) {
804+
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
805+
"function ngx_link_func_exit_cycle(ngx_link_func_cycle_t *cycle) not found in \"%V\", at least create an empty exit function block \n %s",
806+
&scf->_libname, error);
807+
}
808+
809+
778810
/*** loop and without remove queue***/
779811
ngx_queue_t* q;
780812
for (q = ngx_queue_head(scf->_link_func_locs_queue);
@@ -942,16 +974,22 @@ ngx_http_link_func_process_exit(ngx_cycle_t *cycle) {
942974
scf = cscf->ctx->srv_conf[ngx_http_link_func_module.ctx_index];
943975
if (scf && scf->_app ) {
944976
/*** Exiting the client apps ***/
945-
ngx_http_link_func_app_handler func;
946-
*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_exit");
977+
ngx_http_link_func_app_cycle_handler func;
978+
*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_exit_cycle");
947979
if ((error = dlerror()) != NULL) {
948980
ngx_log_error(NGX_LOG_WARN, cycle->log, 0, "Unable to exit call %s , skipped exit called", error);
949981
} else {
950-
ngx_link_func_ctx_t new_ctx; //config request
951-
new_ctx.__pl__ = cycle->pool;
952-
new_ctx.__log__ = cycle->log;
953-
new_ctx.shared_mem = (void*)mcf->shm_ctx->shared_mem;
954-
func(&new_ctx);
982+
ngx_link_func_cycle_t appcyc;
983+
appcyc.has_error = 0;
984+
appcyc.__cycle__ = cycle;
985+
appcyc.__srv_cf__ = scf;
986+
appcyc.__pl__ = cycle->pool;
987+
appcyc.__log__ = cycle->log;
988+
appcyc.shared_mem = (void*)mcf->shm_ctx->shared_mem;
989+
func(&appcyc);
990+
if(appcyc.has_error) {
991+
ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "%s", "link function worker exit error");
992+
}
955993
}
956994
// Unload app, unload old app if nginx reload
957995
if (dlclose(scf->_app) != 0) {
@@ -1700,6 +1738,23 @@ ngx_http_link_func_client_body_handler(ngx_http_request_t *r) {
17001738
}
17011739

17021740
/****** extern interface ********/
1741+
void
1742+
ngx_link_func_cyc_log_debug(ngx_link_func_cycle_t *cyc, const char* msg) {
1743+
ngx_log_error(NGX_LOG_DEBUG, (ngx_log_t *)cyc->__log__, 0, "%s", msg);
1744+
}
1745+
void
1746+
ngx_link_func_cyc_log_info(ngx_link_func_cycle_t *cyc, const char* msg) {
1747+
ngx_log_error(NGX_LOG_INFO, (ngx_log_t *)cyc->__log__, 0, "%s", msg);
1748+
}
1749+
void
1750+
ngx_link_func_cyc_log_warn(ngx_link_func_cycle_t *cyc, const char* msg) {
1751+
ngx_log_error(NGX_LOG_WARN, (ngx_log_t *)cyc->__log__, 0, "%s", msg);
1752+
}
1753+
void
1754+
ngx_link_func_cyc_log_err(ngx_link_func_cycle_t *cyc, const char* msg) {
1755+
ngx_log_error(NGX_LOG_ERR, (ngx_log_t *)cyc->__log__, 0, "%s", msg);
1756+
}
1757+
17031758
void
17041759
ngx_link_func_log_debug(ngx_link_func_ctx_t *ctx, const char* msg) {
17051760
ngx_log_error(NGX_LOG_DEBUG, (ngx_log_t *)ctx->__log__, 0, "%s", msg);
@@ -1802,6 +1857,42 @@ ngx_link_func_get_prop(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen)
18021857
return NULL;
18031858
}
18041859

1860+
u_char*
1861+
ngx_link_func_cyc_get_prop(ngx_link_func_cycle_t *cyc, const char *key, size_t keylen) {
1862+
ngx_http_link_func_srv_conf_t *scf;
1863+
ngx_uint_t nelts, i;
1864+
ngx_keyval_t *keyval;
1865+
ngx_log_t *log;
1866+
1867+
if (cyc == NULL) {
1868+
return NULL;
1869+
}
1870+
1871+
log = (ngx_log_t*) cyc->__log__;
1872+
scf =(ngx_http_link_func_srv_conf_t*) cyc->__srv_cf__;
1873+
1874+
if( scf == NULL || log == NULL) {
1875+
ngx_log_error(NGX_LOG_EMERG, log, 0, "Invalid link function server config");
1876+
return NULL;
1877+
}
1878+
1879+
if(scf->_props == NULL) {
1880+
return NULL;
1881+
}
1882+
1883+
nelts = scf->_props->nelts;
1884+
keyval = scf->_props->elts;
1885+
1886+
for (i = 0; i < nelts; i++) {
1887+
if ( keyval->key.len == keylen && ngx_strncasecmp(keyval->key.data, (u_char*) key, keylen) == 0) {
1888+
/** it is config memory pool, should not reallocate or overwrite **/
1889+
return keyval->value.data;
1890+
}
1891+
keyval++;
1892+
}
1893+
return NULL;
1894+
}
1895+
18051896

18061897
static int
18071898
strpos(const char *haystack, const char *needle) {

src/ngx_link_func_module.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include <stdlib.h>
4141
#include <stdint.h>
4242

43-
#define ngx_link_func_module_version_32 32
43+
#define ngx_link_func_module_version_33 33
4444

4545

4646
#define ngx_link_func_content_type_plaintext "text/plain"
@@ -65,6 +65,28 @@ typedef struct {
6565
void* __log__;
6666
} ngx_link_func_ctx_t;
6767

68+
typedef struct {
69+
void *shared_mem;
70+
int has_error;
71+
/* internal */
72+
void* __cycle__;
73+
void* __srv_cf__;
74+
void* __pl__;
75+
void* __log__;
76+
} ngx_link_func_cycle_t;
77+
78+
extern u_char* ngx_link_func_cyc_get_prop(ngx_link_func_cycle_t *ctx, const char *key, size_t keylen);
79+
extern void ngx_link_func_cyc_log_debug(ngx_link_func_cycle_t *ctx, const char* msg);
80+
extern void ngx_link_func_cyc_log_info(ngx_link_func_cycle_t *ctx, const char* msg);
81+
extern void ngx_link_func_cyc_log_warn(ngx_link_func_cycle_t *ctx, const char* msg);
82+
extern void ngx_link_func_cyc_log_err(ngx_link_func_cycle_t *ctx, const char* msg);
83+
84+
#define ngx_link_func_cyc_log(loglevel, cyc_context, ...) ({\
85+
char __buff__[200];\
86+
snprintf(__buff__, 200, ##__VA_ARGS__);\
87+
ngx_link_func_cyc_log_##loglevel(cyc_context, __buff__);\
88+
})
89+
6890
extern void ngx_link_func_log_debug(ngx_link_func_ctx_t *ctx, const char* msg);
6991
extern void ngx_link_func_log_info(ngx_link_func_ctx_t *ctx, const char* msg);
7092
extern void ngx_link_func_log_warn(ngx_link_func_ctx_t *ctx, const char* msg);

0 commit comments

Comments
 (0)