X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fpurge.cc;h=841af82ebe5c989170cfeb811c16dfb9d8a60a43;hb=fbdf9f2089a2653c8ea8150af22852ebc61ce42d;hp=b0f864a8714c9cb943c4408da8628806ef749b0b;hpb=f3883e19f984baa8981326d9e652d49433d49732;p=m6w6%2Flibmemcached diff --git a/libmemcached/purge.cc b/libmemcached/purge.cc index b0f864a8..841af82e 100644 --- a/libmemcached/purge.cc +++ b/libmemcached/purge.cc @@ -39,10 +39,48 @@ #include +#define memcached_set_purging(__object, __value) ((__object)->state.is_purging= (__value)) -memcached_return_t memcached_purge(memcached_server_write_instance_st ptr) +class Purge +{ +public: + Purge(memcached_st* arg) : + _memc(arg) + { + memcached_set_purging(_memc, true); + } + + ~Purge() + { + memcached_set_purging(_memc, false); + } + +private: + memcached_st* _memc; +}; + +class PollTimeout +{ +public: + PollTimeout(memcached_st* arg) : + _timeout(arg->poll_timeout), + _origin(arg->poll_timeout) + { + _origin = 2000; + } + + ~PollTimeout() + { + _origin= _timeout; + } + +private: + int32_t _timeout; + int32_t& _origin; +}; + +bool memcached_purge(memcached_server_write_instance_st ptr) { - memcached_return_t ret= MEMCACHED_SUCCESS; memcached_st *root= (memcached_st *)ptr->root; if (memcached_is_purging(ptr->root) || /* already purging */ @@ -51,48 +89,47 @@ memcached_return_t memcached_purge(memcached_server_write_instance_st ptr) (ptr->io_bytes_sent >= ptr->root->io_bytes_watermark && memcached_server_response_count(ptr) < 2)) { - return MEMCACHED_SUCCESS; + return true; } - /* memcached_io_write and memcached_response may call memcached_purge - so we need to be able stop any recursion.. */ - memcached_set_purging(root, true); + /* + memcached_io_write and memcached_response may call memcached_purge + so we need to be able stop any recursion.. + */ + Purge set_purge(root); WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); - /* Force a flush of the buffer to ensure that we don't have the n-1 pending - requests buffered up.. */ - if (memcached_io_write(ptr, NULL, 0, true) == -1) + /* + Force a flush of the buffer to ensure that we don't have the n-1 pending + requests buffered up.. + */ + if (memcached_io_write(ptr) == false) { - memcached_set_purging(root, true); - - return memcached_set_error(*ptr, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT); + memcached_set_error(*ptr, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT); + return false; } WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); + bool is_successful= true; uint32_t no_msg= memcached_server_response_count(ptr) - 1; if (no_msg > 0) { memcached_result_st result; - memcached_result_st *result_ptr; - char buffer[SMALL_STRING_LEN]; /* * We need to increase the timeout, because we might be waiting for * data to be sent from the server (the commands was in the output buffer * and just flushed */ - const int32_t timeo= ptr->root->poll_timeout; - root->poll_timeout= 2000; + PollTimeout poll_timeout(ptr->root); - result_ptr= memcached_result_create(root, &result); - WATCHPOINT_ASSERT(result_ptr); + memcached_result_st* result_ptr= memcached_result_create(root, &result); + assert(result_ptr); for (uint32_t x= 0; x < no_msg; x++) { memcached_result_reset(result_ptr); - memcached_return_t rc= memcached_read_one_response(ptr, buffer, - sizeof (buffer), - result_ptr); + memcached_return_t rc= memcached_read_one_response(ptr, result_ptr); /* * Purge doesn't care for what kind of command results that is received. * The only kind of errors I care about if is I'm out of sync with the @@ -101,20 +138,18 @@ memcached_return_t memcached_purge(memcached_server_write_instance_st ptr) if (rc== MEMCACHED_PROTOCOL_ERROR or rc == MEMCACHED_UNKNOWN_READ_FAILURE or rc == MEMCACHED_READ_FAILURE) { WATCHPOINT_ERROR(rc); - ret= rc; memcached_io_reset(ptr); - memcached_set_error(*ptr, rc, MEMCACHED_AT); + is_successful= false; } if (ptr->root->callbacks != NULL) { memcached_callback_st cb = *ptr->root->callbacks; - if (rc == MEMCACHED_SUCCESS) + if (memcached_success(rc)) { for (uint32_t y= 0; y < cb.number_of_callback; y++) { - rc = (*cb.callback[y])(ptr->root, result_ptr, cb.context); - if (rc != MEMCACHED_SUCCESS) + if (memcached_fatal((*cb.callback[y])(ptr->root, result_ptr, cb.context))) { break; } @@ -124,9 +159,7 @@ memcached_return_t memcached_purge(memcached_server_write_instance_st ptr) } memcached_result_free(result_ptr); - root->poll_timeout= timeo; } - memcached_set_purging(root, false); - return ret; + return is_successful; }