修改GET/PUT对外接口;cache miss不算fail;
This commit is contained in:
141
cache/src/tango_cache_transfer.cpp
vendored
141
cache/src/tango_cache_transfer.cpp
vendored
@@ -647,48 +647,10 @@ int tango_cache_multi_delete_start(struct tango_cache_ctx *ctx)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tango_cache_curl_get_done(struct tango_cache_ctx *ctx, CURLcode res, long res_code)
|
||||
{
|
||||
switch(ctx->get.state)
|
||||
{
|
||||
case GET_STATE_START:
|
||||
if(!ctx->fail_state)
|
||||
{
|
||||
if(res!=CURLE_OK || res_code!=200L)
|
||||
{
|
||||
tango_cache_set_fail_state(ctx, (res!=CURLE_OK)?CACHE_ERR_CURL:CACHE_CACHE_MISS);
|
||||
promise_failed(future_to_promise(ctx->future), FUTURE_ERROR_CANCEL, tango_cache_get_errstring(ctx));
|
||||
}
|
||||
else
|
||||
{
|
||||
promise_success(future_to_promise(ctx->future), NULL);
|
||||
}
|
||||
}
|
||||
tango_cache_ctx_destroy(ctx);
|
||||
break;
|
||||
|
||||
case GET_STATE_DELETE:
|
||||
if(cache_delete_minio_object(ctx))
|
||||
{
|
||||
ctx->get.state = GET_STATE_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
tango_cache_ctx_destroy(ctx);
|
||||
}
|
||||
break;
|
||||
|
||||
case GET_STATE_END:
|
||||
tango_cache_ctx_destroy(ctx);
|
||||
break;
|
||||
default: assert(0);break;
|
||||
}
|
||||
}
|
||||
|
||||
static size_t curl_get_response_body_cb(void *ptr, size_t size, size_t count, void *userp)
|
||||
{
|
||||
struct tango_cache_ctx *ctx = (struct tango_cache_ctx *)userp;
|
||||
struct tango_cache_result result;
|
||||
|
||||
if(ctx->fail_state || ctx->get.state==GET_STATE_DELETE)
|
||||
{
|
||||
@@ -705,24 +667,24 @@ static size_t curl_get_response_body_cb(void *ptr, size_t size, size_t count, vo
|
||||
|
||||
if(ctx->get.response_tag.len > 0)
|
||||
{
|
||||
result.data_frag = ctx->get.response_tag.buff;
|
||||
result.size = ctx->get.response_tag.len;
|
||||
result.type = RESULT_TYPE_USERTAG;
|
||||
promise_success(future_to_promise(ctx->future), &result);
|
||||
ctx->get.result.data_frag = ctx->get.response_tag.buff;
|
||||
ctx->get.result.size = ctx->get.response_tag.len;
|
||||
ctx->get.result.type = RESULT_TYPE_USERTAG;
|
||||
promise_success(future_to_promise(ctx->future), &ctx->get.result);
|
||||
easy_string_destroy(&ctx->get.response_tag);
|
||||
}
|
||||
if(ctx->response.len > 0)
|
||||
{
|
||||
result.data_frag = ctx->response.buff;
|
||||
result.size = ctx->response.len;
|
||||
result.type = RESULT_TYPE_HEADER;
|
||||
promise_success(future_to_promise(ctx->future), &result);
|
||||
ctx->get.result.data_frag = ctx->response.buff;
|
||||
ctx->get.result.size = ctx->response.len;
|
||||
ctx->get.result.type = RESULT_TYPE_HEADER;
|
||||
promise_success(future_to_promise(ctx->future), &ctx->get.result);
|
||||
easy_string_destroy(&ctx->response);
|
||||
}
|
||||
result.data_frag = ptr;
|
||||
result.size = size * count;
|
||||
result.type = RESULT_TYPE_BODY;
|
||||
promise_success(future_to_promise(ctx->future), &result);
|
||||
ctx->get.result.data_frag = (const char *)ptr;
|
||||
ctx->get.result.size = size * count;
|
||||
ctx->get.result.type = RESULT_TYPE_BODY;
|
||||
promise_success(future_to_promise(ctx->future), &ctx->get.result);
|
||||
return size*count;
|
||||
}
|
||||
|
||||
@@ -733,12 +695,13 @@ static bool check_expires_header(struct tango_cache_ctx *ctx, const char *expire
|
||||
ctx->get.expires = expires_hdr2timestamp(expires_val, len);
|
||||
time_gmt = get_gmtime_timestamp(time(NULL));
|
||||
|
||||
if(time_gmt > ctx->get.expires) //<2F><><EFBFBD><EFBFBD>ʧЧ<CAA7><D0A7>TODO relative_age<67>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD>ɶ
|
||||
if(time_gmt > ctx->get.expires)
|
||||
{
|
||||
tango_cache_set_fail_state(ctx, CACHE_TIMEOUT);
|
||||
ctx->get.state = GET_STATE_DELETE; //<2F><><EFBFBD><EFBFBD>ʧЧʱ<D0A7><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
ctx->get.result.type = RESULT_TYPE_MISS;
|
||||
promise_success(future_to_promise(ctx->future), &ctx->get.result);
|
||||
easy_string_destroy(&ctx->response);
|
||||
promise_failed(future_to_promise(ctx->future), FUTURE_ERROR_CANCEL, tango_cache_get_errstring(ctx));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -755,24 +718,38 @@ static bool check_fresh_header(struct tango_cache_ctx *ctx)
|
||||
if(ctx->get.last_modify+ctx->get.max_age > now_gmt || now_gmt+ctx->get.min_fresh>ctx->get.expires)
|
||||
{
|
||||
tango_cache_set_fail_state(ctx, CACHE_TIMEOUT);
|
||||
ctx->get.result.type = RESULT_TYPE_MISS;
|
||||
promise_success(future_to_promise(ctx->future), &ctx->get.result);
|
||||
easy_string_destroy(&ctx->response);
|
||||
promise_failed(future_to_promise(ctx->future), FUTURE_ERROR_CANCEL, tango_cache_get_errstring(ctx));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool check_get_result_code(struct tango_cache_ctx *ctx)
|
||||
static bool check_get_result_code(struct tango_cache_ctx *ctx, CURLcode code, long res_code)
|
||||
{
|
||||
CURLcode code;
|
||||
|
||||
code = curl_easy_getinfo(ctx->curl, CURLINFO_RESPONSE_CODE, &ctx->res_code);
|
||||
if(code != CURLE_OK || ctx->res_code!=200L)
|
||||
if(code != CURLE_OK)
|
||||
{
|
||||
tango_cache_set_fail_state(ctx, (code!=CURLE_OK)?CACHE_ERR_CURL:CACHE_CACHE_MISS);
|
||||
tango_cache_set_fail_state(ctx, CACHE_ERR_CURL);
|
||||
promise_failed(future_to_promise(ctx->future), FUTURE_ERROR_CANCEL, tango_cache_get_errstring(ctx));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(res_code != 200L)
|
||||
{
|
||||
if(res_code == 404L)
|
||||
{
|
||||
tango_cache_set_fail_state(ctx, CACHE_CACHE_MISS);
|
||||
ctx->get.result.type = RESULT_TYPE_MISS;
|
||||
promise_success(future_to_promise(ctx->future), &ctx->get.result);
|
||||
}
|
||||
else
|
||||
{
|
||||
tango_cache_set_fail_state(ctx, CACHE_ERR_INTERNAL);
|
||||
promise_failed(future_to_promise(ctx->future), FUTURE_ERROR_CANCEL, tango_cache_get_errstring(ctx));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -788,9 +765,13 @@ static size_t curl_get_response_header_cb(void *ptr, size_t size, size_t count,
|
||||
{
|
||||
return raw_len;
|
||||
}
|
||||
if(ctx->res_code==0 && !check_get_result_code(ctx)) //<2F>״<EFBFBD>Ӧ<EFBFBD><D3A6>ʱ<EFBFBD>ȿ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>200
|
||||
if(ctx->res_code == 0) //<2F>״<EFBFBD>Ӧ<EFBFBD><D3A6>ʱ<EFBFBD>ȿ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>200
|
||||
{
|
||||
return raw_len;
|
||||
CURLcode code = curl_easy_getinfo(ctx->curl, CURLINFO_RESPONSE_CODE, &ctx->res_code);
|
||||
if(!check_get_result_code(ctx, code, ctx->res_code))
|
||||
{
|
||||
return raw_len;
|
||||
}
|
||||
}
|
||||
pos_colon = (char*)memchr(start, ':', raw_len);
|
||||
if(pos_colon == NULL)
|
||||
@@ -831,9 +812,14 @@ static size_t curl_get_response_header_cb(void *ptr, size_t size, size_t count,
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 14:
|
||||
if(strcmp_one_word_mesa_equal_len("content-length", "CONTENT-LENGTH", start, 14))
|
||||
{
|
||||
sscanf(pos_colon+1, "%lu", &ctx->get.result.tlength);
|
||||
}
|
||||
break;
|
||||
case 11: if(strcmp_one_word_mesa_equal_len("content-md5", "CONTENT-MD5", start, 11)) easy_string_savedata(&ctx->response, (const char*)ptr, raw_len); break;
|
||||
case 12: if(strcmp_one_word_mesa_equal_len("content-type", "CONTENT-TYPE", start, 12)) easy_string_savedata(&ctx->response, (const char*)ptr, raw_len); break;
|
||||
case 14: if(strcmp_one_word_mesa_equal_len("content-length", "CONTENT-LENGTH", start, 14)) easy_string_savedata(&ctx->response, (const char*)ptr, raw_len); break;
|
||||
case 16: if(strcmp_one_word_mesa_equal_len("content-encoding", "CONTENT-ENCODING", start, 16)) easy_string_savedata(&ctx->response, (const char*)ptr, raw_len); break;
|
||||
case 19: if(strcmp_one_word_mesa_equal_len("content-disposition", "CONTENT-DISPOSITION", start, 19)) easy_string_savedata(&ctx->response, (const char*)ptr, raw_len); break;
|
||||
default: break;
|
||||
@@ -841,6 +827,37 @@ static size_t curl_get_response_header_cb(void *ptr, size_t size, size_t count,
|
||||
return raw_len;
|
||||
}
|
||||
|
||||
void tango_cache_curl_get_done(struct tango_cache_ctx *ctx, CURLcode res, long res_code)
|
||||
{
|
||||
switch(ctx->get.state)
|
||||
{
|
||||
case GET_STATE_START:
|
||||
if(!ctx->fail_state && check_get_result_code(ctx, res, res_code))
|
||||
{
|
||||
ctx->get.result.type = RESULT_TYPE_END;
|
||||
promise_success(future_to_promise(ctx->future), &ctx->get.result);
|
||||
}
|
||||
tango_cache_ctx_destroy(ctx);
|
||||
break;
|
||||
|
||||
case GET_STATE_DELETE:
|
||||
if(cache_delete_minio_object(ctx))
|
||||
{
|
||||
ctx->get.state = GET_STATE_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
tango_cache_ctx_destroy(ctx);
|
||||
}
|
||||
break;
|
||||
|
||||
case GET_STATE_END:
|
||||
tango_cache_ctx_destroy(ctx);
|
||||
break;
|
||||
default: assert(0);break;
|
||||
}
|
||||
}
|
||||
|
||||
int tango_cache_fetch_start(struct tango_cache_ctx *ctx)
|
||||
{
|
||||
CURLMcode rc;
|
||||
|
||||
Reference in New Issue
Block a user