支持promise finish。

This commit is contained in:
zhangchengwei
2018-11-23 20:55:28 +08:00
committed by zhengchao
parent 313f36c58a
commit 9fc2a8a0d2
8 changed files with 85 additions and 29 deletions

View File

@@ -95,6 +95,7 @@ const char *tango_cache_get_errstring(const struct tango_cache_ctx *ctx)
case CACHE_ERR_REDIS_JSON:return "parse redis json error";
case CACHE_ERR_REDIS_CONNECT:return "redis is not connected";
case CACHE_OUTOF_SESSION:return "two many curl sessions";
case CACHE_UPDATE_CANCELED:return "update was canceled";
default: return ctx->error;
}
}
@@ -234,15 +235,15 @@ void tango_cache_ctx_destroy(struct tango_cache_ctx *ctx, bool callback)
curl_slist_free_all(ctx->headers);
}//no break here
case CACHE_REQUEST_DELETE:
if(callback && ctx->future != NULL)
if(callback && ctx->promise != NULL)
{
if(ctx->fail_state)
{
promise_failed(future_to_promise(ctx->future), FUTURE_ERROR_CANCEL, tango_cache_get_errstring(ctx));
promise_failed(ctx->promise, FUTURE_ERROR_CANCEL, tango_cache_get_errstring(ctx));
}
else
{
promise_success(future_to_promise(ctx->future), NULL);
promise_success(ctx->promise, NULL);
}
}
break;
@@ -257,6 +258,26 @@ void tango_cache_update_end(struct tango_cache_ctx *ctx)
cache_kick_upload_minio_end(ctx);
}
void tango_cache_update_cancel(struct tango_cache_ctx *ctx)
{
ctx->put.close_state = true;
if(ctx->curl != NULL)
{
curl_multi_remove_handle(ctx->instance->multi_hd, ctx->curl);
curl_easy_cleanup(ctx->curl);
ctx->curl = NULL;
}
tango_cache_set_fail_state(ctx, CACHE_UPDATE_CANCELED);
if(ctx->put.uploadID!=NULL && cache_cancel_upload_minio(ctx))
{
ctx->put.state = PUT_STATE_CANCEL;
}
else
{
tango_cache_ctx_destroy(ctx, false);
}
}
int tango_cache_update_frag_data(struct tango_cache_ctx *ctx, const char *data, size_t size)
{
if(ctx->fail_state)
@@ -325,7 +346,7 @@ struct tango_cache_ctx *tango_cache_update_prepare(struct tango_cache_instance *
ctx = (struct tango_cache_ctx *)calloc(1, sizeof(struct tango_cache_ctx));
ctx->instance = instance;
ctx->future = f;
ctx->promise = future_to_promise(f);
ctx->method = CACHE_REQUEST_PUT;
if(instance->param->hash_object_key)
@@ -455,7 +476,8 @@ struct tango_cache_ctx *tango_cache_fetch_prepare(struct tango_cache_instance *i
ctx = (struct tango_cache_ctx *)calloc(1, sizeof(struct tango_cache_ctx));
ctx->instance = instance;
ctx->future = f;
ctx->promise = future_to_promise(f);
promise_allow_many_successes(ctx->promise);
ctx->method = method;
ctx->get.state = GET_STATE_START;
ctx->get.max_age = meta->get.max_age;
@@ -526,7 +548,7 @@ struct tango_cache_ctx *tango_cache_delete_prepare(struct tango_cache_instance *
ctx = (struct tango_cache_ctx *)calloc(1, sizeof(struct tango_cache_ctx));
ctx->instance = instance;
ctx->future = f;
ctx->promise = future_to_promise(f);
ctx->method = CACHE_REQUEST_DELETE;
if(instance->param->hash_object_key)
@@ -574,7 +596,7 @@ struct tango_cache_ctx *tango_cache_multi_delete_prepare(struct tango_cache_inst
ctx = (struct tango_cache_ctx *)calloc(1, sizeof(struct tango_cache_ctx));
ctx->instance = instance;
ctx->future = f;
ctx->promise = future_to_promise(f);
ctx->method = CACHE_REQUEST_DELETE_MUL;
ctx->del.succ_num = num;