From acd51e4987d27c762de5639cbff8ec87ff0990c3 Mon Sep 17 00:00:00 2001 From: Andre Cruz Date: Mon, 8 Mar 2010 14:30:21 +0000 Subject: [PATCH] Added option to enable TCP_KEEPALIVE on created sockets. --- docs/memcached_behavior.pod | 11 +++++++---- libmemcached/behavior.c | 10 +++++++--- libmemcached/connect.c | 14 +++++++++++++- libmemcached/constants.h | 1 + libmemcached/memcached.c | 5 +++-- libmemcached/memcached.h | 1 + 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/docs/memcached_behavior.pod b/docs/memcached_behavior.pod index 2e0240af..4095e8a5 100644 --- a/docs/memcached_behavior.pod +++ b/docs/memcached_behavior.pod @@ -211,11 +211,11 @@ other objects (on the 'n' next servers specified in your server list). =item MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ -Allows randomizing the replica reads starting point. Normally the read is +Allows randomizing the replica reads starting point. Normally the read is done from primary server and in case of miss the read is done from primary -+ 1, then primary + 2 all the way to 'n' replicas. If this option is set ++ 1, then primary + 2 all the way to 'n' replicas. If this option is set on the starting point of the replica reads is randomized between the servers. -This allows distributing read load to multiple servers with the expense of +This allows distributing read load to multiple servers with the expense of more write traffic. =item MEMCACHED_BEHAVIOR_CORK @@ -224,9 +224,12 @@ Enable TCP_CORK behavior. This is only available as an option Linux. MEMCACHED_NO_SERVERS is returned if no servers are available to test with. MEMCACHED_NOT_SUPPORTED is returned if we were not able to determine if support was available. All other responses then MEMCACHED_SUCCESS -report an error of some sort. This behavior also enables +report an error of some sort. This behavior also enables MEMCACHED_BEHAVIOR_TCP_NODELAY when set. +=item MEMCACHED_BEHAVIOR_KEEPALIVE + +Enable TCP_KEEPALIVE behavior. =item MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE diff --git a/libmemcached/behavior.c b/libmemcached/behavior.c index 886457a5..aa697cfe 100644 --- a/libmemcached/behavior.c +++ b/libmemcached/behavior.c @@ -87,6 +87,10 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, ptr->flags.tcp_nodelay= set_flag(data); memcached_quit(ptr); break; + case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE: + ptr->flags.tcp_keepalive= set_flag(data); + memcached_quit(ptr); + break; case MEMCACHED_BEHAVIOR_DISTRIBUTION: return memcached_behavior_set_distribution(ptr, (memcached_server_distribution_t)data); case MEMCACHED_BEHAVIOR_KETAMA: @@ -114,7 +118,7 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, /** @note We try to keep the same distribution going. This should be deprecated and rewritten. */ - return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA); + return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA); } case MEMCACHED_BEHAVIOR_HASH: return memcached_behavior_set_key_hash(ptr, (memcached_hash_t)(data)); @@ -324,8 +328,8 @@ uint64_t memcached_behavior_get(memcached_st *ptr, instance= memcached_server_instance_fetch(ptr, 0); - /** - @note REFACTOR + /** + @note REFACTOR */ if (instance) { diff --git a/libmemcached/connect.c b/libmemcached/connect.c index 4b8939c1..41bd7fb0 100644 --- a/libmemcached/connect.c +++ b/libmemcached/connect.c @@ -111,6 +111,18 @@ static memcached_return_t set_socket_options(memcached_server_st *ptr) return MEMCACHED_FAILURE; } + if (ptr->root->flags.tcp_keepalive) + { + int flag= 1; + int error; + + error= setsockopt(ptr->fd, SOL_SOCKET, SO_KEEPALIVE, + &flag, (socklen_t)sizeof(int)); + WATCHPOINT_ASSERT(error == 0); + if (error) + return MEMCACHED_FAILURE; + } + if (ptr->root->send_size > 0) { int error; @@ -368,7 +380,7 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) WATCHPOINT_ASSERT(0); } - unlikely ( rc != MEMCACHED_SUCCESS) + unlikely ( rc != MEMCACHED_SUCCESS) { //@todo create interface around last_discontected_server memcached_st *root= (memcached_st *)ptr->root; diff --git a/libmemcached/constants.h b/libmemcached/constants.h index f05b03eb..6405df29 100644 --- a/libmemcached/constants.h +++ b/libmemcached/constants.h @@ -115,6 +115,7 @@ typedef enum { MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, MEMCACHED_BEHAVIOR_CORK, + MEMCACHED_BEHAVIOR_TCP_KEEPALIVE, MEMCACHED_BEHAVIOR_MAX } memcached_behavior_t; diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index 4e0308a0..647d0f79 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -5,7 +5,7 @@ * Use and distribution licensed under the BSD license. See * the COPYING file in the parent directory for full text. * - * Summary: + * Summary: * */ @@ -32,7 +32,8 @@ static const memcached_st global_copy= { .use_cache_lookups= false, .use_sort_hosts= false, .use_udp= false, - .verify_key= false + .verify_key= false, + .tcp_keepalive= false } }; diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 30823a87..db164649 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -80,6 +80,7 @@ struct memcached_st { bool use_sort_hosts MEMCACHED_BITFIELD; bool use_udp MEMCACHED_BITFIELD; bool verify_key MEMCACHED_BITFIELD; + bool tcp_keepalive MEMCACHED_BITFIELD; } flags; memcached_server_distribution_t distribution; hashkit_st hashkit; -- 2.30.2