@@ -147,6 +147,7 @@ typedef struct {
147147
148148static ngx_int_t ngx_http_link_func_pre_configuration (ngx_conf_t * cf );
149149static ngx_int_t ngx_http_link_func_post_configuration (ngx_conf_t * cf );
150+ static void * ngx_http_link_func_get_duplicate_handler (ngx_http_link_func_srv_conf_t * scf , ngx_str_t * method_name );
150151static ngx_int_t ngx_http_link_func_application_compatibility_check (ngx_conf_t * cf , ngx_http_core_main_conf_t * cmcf );
151152static char * ngx_http_link_func_validation_check_and_set_str_slot (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
152153static char * ngx_http_link_func_set_link_func_shm (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
@@ -749,6 +750,25 @@ ngx_http_link_func_pre_configuration(ngx_conf_t *cf) {
749750 return NGX_OK ;
750751}
751752
753+ static void *
754+ ngx_http_link_func_get_duplicate_handler (ngx_http_link_func_srv_conf_t * scf , ngx_str_t * method_name ) {
755+ /* this is only do when init or exit application, don't be count as performance issue, this is for same method merged */
756+ ngx_queue_t * q ;
757+ for (q = ngx_queue_head (scf -> _link_func_locs_queue );
758+ q != ngx_queue_sentinel (scf -> _link_func_locs_queue );
759+ q = ngx_queue_next (q )) {
760+ ngx_http_link_func_loc_q_t * cflq = (ngx_http_link_func_loc_q_t * ) q ;
761+ ngx_http_link_func_loc_conf_t * lcf = cflq -> _loc_conf ;
762+ if ( lcf && lcf -> _method_name .len > 0 ) {
763+ if ( lcf -> _handler && lcf -> _method_name .len == method_name -> len &&
764+ ngx_strncmp (lcf -> _method_name .data , method_name -> data , method_name -> len ) == 0 ) {
765+ return lcf -> _handler ;
766+ }
767+ }
768+ }
769+ return NULL ;
770+ }
771+
752772static ngx_int_t
753773ngx_http_link_func_application_compatibility_check (ngx_conf_t * cf , ngx_http_core_main_conf_t * cmcf ) {
754774 ngx_uint_t s ;
@@ -780,8 +800,6 @@ ngx_http_link_func_application_compatibility_check(ngx_conf_t *cf, ngx_http_core
780800 if ( !scf -> _app ) {
781801 ngx_conf_log_error (NGX_LOG_ERR , cf , 0 , "%s" , "unable to initialized the Application " );
782802 return NGX_ERROR ;
783- } else {
784- ngx_conf_log_error (NGX_LOG_INFO , cf , 0 , "Application %V loaded successfully " , & scf -> _libname );
785803 }
786804
787805 char * error ;
@@ -817,6 +835,7 @@ ngx_http_link_func_application_compatibility_check(ngx_conf_t *cf, ngx_http_core
817835 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "Error function load: %s" , error );
818836 return NGX_ERROR ;
819837 }
838+ lcf -> _handler = NULL ; // reset back
820839 } else {
821840 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "%s" , "Ambiguous function name" );
822841 return NGX_ERROR ;
@@ -828,6 +847,7 @@ ngx_http_link_func_application_compatibility_check(ngx_conf_t *cf, ngx_http_core
828847 return NGX_ERROR ;
829848 } else {
830849 ngx_conf_log_error (NGX_LOG_DEBUG , cf , 0 , "app \"%V\" successfully verified" , & scf -> _libname );
850+ scf -> _app = NULL ; // reset back
831851 }
832852 } else {
833853 continue ;
@@ -867,42 +887,45 @@ ngx_http_link_func_module_init(ngx_cycle_t *cycle) {
867887 }
868888
869889 char * error ;
870- /*** Loop and remove queue ***/
871- while (! (ngx_queue_empty (scf -> _link_func_locs_queue )) ) {
872- ngx_queue_t * q = ngx_queue_head (scf -> _link_func_locs_queue );
873- ngx_http_link_func_loc_q_t * cflq = ngx_queue_data (q , ngx_http_link_func_loc_q_t , _queue );
890+ /*** loop and without remove queue***/
891+ ngx_queue_t * q ;
892+ for (q = ngx_queue_head (scf -> _link_func_locs_queue );
893+ q != ngx_queue_sentinel (scf -> _link_func_locs_queue );
894+ q = ngx_queue_next (q )) {
895+ ngx_http_link_func_loc_q_t * cflq = (ngx_http_link_func_loc_q_t * ) q ;
896+
874897 ngx_http_link_func_loc_conf_t * lcf = cflq -> _loc_conf ;
875898 if ( lcf && lcf -> _method_name .len > 0 ) {
876- * (void * * )(& lcf -> _handler ) = dlsym (scf -> _app , (const char * )lcf -> _method_name .data );
877- if ((error = dlerror ()) != NULL ) {
878- ngx_log_error (NGX_LOG_EMERG , cycle -> log , 0 , "Error function load: %s" , error );
879- return NGX_ERROR ;
899+ if ( ( lcf -> _handler = ngx_http_link_func_get_duplicate_handler (scf , & lcf -> _method_name ) ) == NULL ) {
900+ * (void * * )(& lcf -> _handler ) = dlsym (scf -> _app , (const char * )lcf -> _method_name .data );
901+ if ((error = dlerror ()) != NULL ) {
902+ ngx_log_error (NGX_LOG_EMERG , cycle -> log , 0 , "Error function load: %s" , error );
903+ return NGX_ERROR ;
904+ }
880905 }
881906 } else {
882907 ngx_log_error (NGX_LOG_EMERG , cycle -> log , 0 , "%s" , "Ambiguous function name" );
883908 return NGX_ERROR ;
884909 }
885- ngx_queue_remove (q );
886910 }
887- /*** loop and without remove queue***/
888- // ngx_queue_t* q;
889- // for (q = ngx_queue_head(scf->_link_func_locs_queue);
890- // q != ngx_queue_sentinel(scf->_link_func_locs_queue);
891- // q = ngx_queue_next(q)) {
892- // ngx_http_link_func_loc_q_t* cflq = (ngx_http_link_func_loc_q_t *) q;
893-
894- // ngx_http_link_func_loc_conf_t *lcf = cflq->_loc_conf;
895- // if ( lcf && lcf->_method_name.len > 0 ) {
896- // *(void**)(&lcf->_handler) = dlsym(scf->_app, (const char*)lcf->_method_name.data);
897- // if ((error = dlerror()) != NULL) {
898- // ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "Error function load: %s", error);
899- // return NGX_ERROR;
900- // }
901- // } else {
902- // ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "%s", "Ambiguous function name");
903- // return NGX_ERROR;
904- // }
905- // }
911+
912+ /*** Loop and remove queue, don't retain the queue ***/
913+ while (! (ngx_queue_empty (scf -> _link_func_locs_queue )) ) {
914+ ngx_queue_t * q = ngx_queue_head (scf -> _link_func_locs_queue );
915+ // ngx_http_link_func_loc_q_t* cflq = ngx_queue_data(q, ngx_http_link_func_loc_q_t, _queue);
916+ // ngx_http_link_func_loc_conf_t *lcf = cflq->_loc_conf;
917+ // if ( lcf && lcf->_method_name.len > 0 ) {
918+ // *(void**)(&lcf->_handler) = dlsym(scf->_app, (const char*)lcf->_method_name.data);
919+ // if ((error = dlerror()) != NULL) {
920+ // ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "Error function load: %s", error);
921+ // return NGX_ERROR;
922+ // }
923+ // } else {
924+ // ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "%s", "Ambiguous function name");
925+ // return NGX_ERROR;
926+ // }
927+ ngx_queue_remove (q );
928+ }
906929 } else {
907930 continue ;
908931 }
0 commit comments