diff --git a/plugin/business/pangu-http/src/pangu_http.cpp b/plugin/business/pangu-http/src/pangu_http.cpp index d4ffe81..1fb17b0 100644 --- a/plugin/business/pangu-http/src/pangu_http.cpp +++ b/plugin/business/pangu-http/src/pangu_http.cpp @@ -63,6 +63,7 @@ enum pangu_http_stat STAT_ACTION_PRE_REPLACE, STAT_ACTION_REPLACE, STAT_ACTION_WHITELSIT, + STAT_SUSPENDING, __PG_STAT_MAX }; struct pangu_rt @@ -77,6 +78,7 @@ struct pangu_rt char * reject_page; int page_size; + long long suspend_max; int cache_enabled; struct cache_handle* cache; @@ -197,6 +199,7 @@ static void pangu_http_stat_init(struct pangu_rt * pangu_runtime) spec[STAT_ACTION_PRE_REPLACE]="pre_replace"; spec[STAT_ACTION_REPLACE]="replace"; spec[STAT_ACTION_WHITELSIT]="whitelist"; + spec[STAT_SUSPENDING]="suspending"; for(i=0;i<__PG_STAT_MAX;i++) { @@ -313,7 +316,7 @@ int pangu_http_init(struct tfe_proxy * proxy) { const char * profile = "./conf/pangu/pangu_pxy.conf"; const char * logfile = "./log/pangu_pxy.log"; - int table_id=0; + int table_id=0, temp=0; g_pangu_rt = ALLOC(struct pangu_rt, 1); g_pangu_rt->thread_num = tfe_proxy_get_work_thread_count(); @@ -365,6 +368,9 @@ int pangu_http_init(struct tfe_proxy * proxy) MESA_load_profile_string_def(profile, "TEMPLATE", "PAGE_451", page_path, sizeof(page_path), "./resource/pangu/HTTP451.html"); g_pangu_rt->tpl_451 = ctemplate::Template::GetTemplate(page_path, ctemplate::DO_NOT_STRIP); + + MESA_load_profile_int_def(profile, "TANGO_CACHE", "suspend_max", &(temp), 1024*1024); + g_pangu_rt->suspend_max=temp; MESA_load_profile_int_def(profile, "TANGO_CACHE", "enable_cache", &(g_pangu_rt->cache_enabled), 1); if(g_pangu_rt->cache_enabled) @@ -1083,6 +1089,7 @@ static void cache_read_on_succ(future_result_t * result, void * user) ctx->cache_result_declared_sz=meta->content_length; ctx->resumed_cb=dummy_resume; tfe_http_session_resume(ctx->ref_session); + ATOMIC_DEC(&(g_pangu_rt->stat_val[STAT_SUSPENDING])); ctx->cached_response=tfe_http_session_response_create(ctx->ref_session, 200); tfe_http_std_field_write(ctx->cached_response, TFE_HTTP_CONT_TYPE, meta->content_type); @@ -1116,6 +1123,7 @@ static void cache_read_on_succ(future_result_t * result, void * user) ctx->pending_result=PENDING_RESULT_MISS; ctx->resumed_cb=dummy_resume; tfe_http_session_resume(ctx->ref_session); + ATOMIC_DEC(&(g_pangu_rt->stat_val[STAT_SUSPENDING])); future_destroy(ctx->f_cache_query); ctx->f_cache_query=NULL; break; @@ -1133,6 +1141,7 @@ static void cache_read_on_fail(enum e_future_error err, const char * what, void { tfe_http_session_resume(ctx->ref_session); ctx->resumed_cb=dummy_resume; + ATOMIC_DEC(&(g_pangu_rt->stat_val[STAT_SUSPENDING])); } else { @@ -1149,6 +1158,7 @@ static void cache_pend_on_succ(future_result_t * result, void * user) meta=cache_pending_result_read_meta(result); ctx->resumed_cb=dummy_resume; tfe_http_session_resume(ctx->ref_session); + ATOMIC_DEC(&(g_pangu_rt->stat_val[STAT_SUSPENDING])); future_destroy(ctx->f_cache_pending); ctx->f_cache_pending=NULL; if(meta==NULL) @@ -1193,7 +1203,8 @@ static void cache_pend_on_fail(enum e_future_error err, const char * what, void struct pangu_http_ctx * ctx = (struct pangu_http_ctx *)user; ctx->pending_result=PENDING_RESULT_FOBIDDEN; - tfe_http_session_resume(ctx->ref_session); + tfe_http_session_resume(ctx->ref_session); + ATOMIC_DEC(&(g_pangu_rt->stat_val[STAT_SUSPENDING])); ctx->resumed_cb=dummy_resume; future_destroy(ctx->f_cache_pending); ctx->f_cache_pending=NULL; @@ -1203,6 +1214,11 @@ static void cache_pend_on_fail(enum e_future_error err, const char * what, void void cache_pend(const struct tfe_http_session * session, unsigned int thread_id, struct pangu_http_ctx * ctx) { + if(g_pangu_rt->stat_val[STAT_SUSPENDING]>g_pangu_rt->suspend_max) + { + ctx->pending_result=PENDING_RESULT_FOBIDDEN; + return; + } ctx->f_cache_pending=future_create("cache_pend", cache_pend_on_succ, cache_pend_on_fail, ctx); ctx->ref_session=tfe_http_session_allow_write(session); ctx->pending_result=web_cache_async_pending(g_pangu_rt->cache, thread_id, session->req, &(ctx->cmid), ctx->f_cache_pending); @@ -1210,6 +1226,7 @@ void cache_pend(const struct tfe_http_session * session, unsigned int thread_id, { case PENDING_RESULT_REVALIDATE: tfe_http_session_suspend(ctx->ref_session); + ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_SUSPENDING])); break; case PENDING_RESULT_ALLOWED: case PENDING_RESULT_FOBIDDEN: @@ -1224,12 +1241,17 @@ void cache_pend(const struct tfe_http_session * session, unsigned int thread_id, } void cache_read(const struct tfe_http_session * session, unsigned int thread_id, struct pangu_http_ctx * ctx) { + if(g_pangu_rt->stat_val[STAT_SUSPENDING]>g_pangu_rt->suspend_max) + { + return; + } ctx->f_cache_query=future_create("cache_read", cache_read_on_succ, cache_read_on_fail, ctx); int ret=web_cache_async_read(g_pangu_rt->cache, thread_id, session->req, &(ctx->cmid), ctx->f_cache_query); if(ret==0) { ctx->ref_session=tfe_http_session_allow_write(session); tfe_http_session_suspend(ctx->ref_session); + ATOMIC_INC(&(g_pangu_rt->stat_val[STAT_SUSPENDING])); } else { diff --git a/plugin/business/pangu-http/src/pangu_web_cache.cpp b/plugin/business/pangu-http/src/pangu_web_cache.cpp index 551214a..9595487 100644 --- a/plugin/business/pangu-http/src/pangu_web_cache.cpp +++ b/plugin/business/pangu-http/src/pangu_web_cache.cpp @@ -82,6 +82,7 @@ struct cache_param time_t inactive_time_sec; size_t max_cache_size; size_t max_cache_obj_size; + size_t min_cache_obj_size; pthread_mutex_t lock; }; struct cache_bloom @@ -573,6 +574,9 @@ void cache_param_new(int idx, const struct Maat_rule_t* rule, const char* srv_de item=cJSON_GetObjectItem(json,"max_cache_obj_size"); if(item && item->type==cJSON_String) param->max_cache_obj_size=storage_unit_byte(item->valuestring); + + item=cJSON_GetObjectItem(json,"min_cache_obj_size"); + if(item && item->type==cJSON_String) param->min_cache_obj_size=storage_unit_byte(item->valuestring); cJSON_Delete(json); *ad=param; @@ -723,11 +727,16 @@ struct cache_handle* create_web_cache_handle(const char* profile_path, const cha cache->default_cache_policy.ignore_req_nocache=0; cache->default_cache_policy.ignore_res_nocache=0; cache->default_cache_policy.force_caching=0; - cache->default_cache_policy.min_use=0; cache->default_cache_policy.pinning_time_sec=0; cache->default_cache_policy.inactive_time_sec=0; cache->default_cache_policy.max_cache_size=0; - cache->default_cache_policy.max_cache_obj_size=1024*1024*1024;//<1GB by default + + MESA_load_profile_int_def(profile_path, section, "min_use", &(cache->default_cache_policy.min_use), 0); + MESA_load_profile_int_def(profile_path, section, "max_cache_obj_size", &(temp), 1024*1024*1024); + cache->default_cache_policy.max_cache_obj_size=temp; //<1GB by default + + MESA_load_profile_int_def(profile_path, section, "min_cache_obj_size", &(temp), 16*1024); + cache->default_cache_policy.min_cache_obj_size=temp;// > 16kb by default if(cache->cache_policy_enabled) { @@ -1246,6 +1255,7 @@ struct cache_update_context* web_cache_write_start(struct cache_handle* handle, case UNDEFINED: if(_mid->shall_bypass || (param->max_cache_obj_size!=0 && content_len > param->max_cache_obj_size) + || (param->min_cache_obj_size > content_len) || (!param->cache_cookied_cont && _mid->has_cookie) || (!param->cache_html && _mid->is_html)) {