From: Brian Aker Date: Tue, 16 Oct 2007 10:14:51 +0000 (-0700) Subject: Set recv/send sockket sizes. X-Git-Tag: 0_7~30 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=c11af7ccccd3260444da4f575022de655d2ffc26;p=m6w6%2Flibmemcached Set recv/send sockket sizes. Clarrified the memcached_behavior_set will reconnect sockets. All test cases pass. --- diff --git a/docs/memcached_behavior.pod b/docs/memcached_behavior.pod index d9e157f4..55e82d8d 100755 --- a/docs/memcached_behavior.pod +++ b/docs/memcached_behavior.pod @@ -32,7 +32,8 @@ that behavior is currently enabled in the client. memcached_behavior_set() changes the value of a particular option of the client. It takes both a flag (listed below) and a value. For simple on or -off options you just need to pass in a value of 1. +off options you just need to pass in a value of 1. Calls to +memcached_behavior_set() will flush and reset all connections. =over 4 diff --git a/include/memcached.h b/include/memcached.h index 652a77c5..a706fea6 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -28,7 +28,7 @@ typedef struct memcached_server_st memcached_server_st; #define HUGE_STRING_LEN 8196 #define MEMCACHED_MAX_KEY 251 /* We add one to have it null terminated */ //#define MEMCACHED_MAX_BUFFER 8196 -#define MEMCACHED_MAX_BUFFER HUGE_STRING_LEN*4 +#define MEMCACHED_MAX_BUFFER HUGE_STRING_LEN*2 typedef enum { MEMCACHED_SUCCESS, @@ -65,6 +65,8 @@ typedef enum { MEMCACHED_BEHAVIOR_TCP_NODELAY, MEMCACHED_BEHAVIOR_MD5_HASHING, MEMCACHED_BEHAVIOR_KETAMA, + MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, + MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, } memcached_behavior; typedef enum { @@ -129,6 +131,8 @@ struct memcached_st { char connected; int my_errno; unsigned long long flags; + int send_size; + int recv_size; memcached_return warning; /* Future Use */ }; diff --git a/lib/memcached_behavior.c b/lib/memcached_behavior.c index 1ce495af..17a0d83b 100644 --- a/lib/memcached_behavior.c +++ b/lib/memcached_behavior.c @@ -38,6 +38,20 @@ memcached_return memcached_behavior_set(memcached_st *ptr, case MEMCACHED_BEHAVIOR_KETAMA: set_behavior_flag(ptr, MEM_USE_KETAMA, data); break; + case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: + { + ptr->send_size= (*((int *)data)); + memcached_quit(ptr); + break; + } + case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE: + { + ptr->recv_size= (*((int *)data)); + memcached_quit(ptr); + break; + } + + } return MEMCACHED_SUCCESS; @@ -46,7 +60,7 @@ memcached_return memcached_behavior_set(memcached_st *ptr, unsigned long long memcached_behavior_get(memcached_st *ptr, memcached_behavior flag) { - memcached_flags temp_flag; + memcached_flags temp_flag= 0; switch (flag) { @@ -62,8 +76,39 @@ unsigned long long memcached_behavior_get(memcached_st *ptr, case MEMCACHED_BEHAVIOR_KETAMA: temp_flag= MEM_USE_KETAMA; break; + case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: + { + int sock_size; + socklen_t sock_length= sizeof(int); + + /* We just try the first host, and if it is down we return zero */ + if ((memcached_connect(ptr, 0)) != MEMCACHED_SUCCESS) + return 0; + + if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET, + SO_SNDBUF, &sock_size, &sock_length)) + return 0; /* Zero means error */ + + return sock_size; + } + case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE: + { + int sock_size; + socklen_t sock_length= sizeof(int); + + /* We just try the first host, and if it is down we return zero */ + if ((memcached_connect(ptr, 0)) != MEMCACHED_SUCCESS) + return 0; + + if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET, + SO_RCVBUF, &sock_size, &sock_length)) + return 0; /* Zero means error */ + + return sock_size; + } } + assert(temp_flag); /* Programming mistake if it gets this far */ if (ptr->flags & temp_flag) return 1; else diff --git a/lib/memcached_connect.c b/lib/memcached_connect.c index 57274be2..34613ddd 100644 --- a/lib/memcached_connect.c +++ b/lib/memcached_connect.c @@ -33,7 +33,6 @@ memcached_return memcached_real_connect(memcached_st *ptr, unsigned int server_k return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE; } - /* bind any port number */ localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = htonl(INADDR_ANY); @@ -57,6 +56,18 @@ memcached_return memcached_real_connect(memcached_st *ptr, unsigned int server_k &flag, (socklen_t)sizeof(int)); } + if (ptr->send_size) + { + setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF, + &ptr->send_size, (socklen_t)sizeof(int)); + } + + if (ptr->recv_size) + { + setsockopt(ptr->hosts[server_key].fd, SOL_SOCKET, SO_SNDBUF, + &ptr->recv_size, (socklen_t)sizeof(int)); + } + /* connect to server */ test_connect: if (connect(ptr->hosts[server_key].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) diff --git a/tests/output.res b/tests/output.res index a3c5c628..813c18b1 100644 --- a/tests/output.res +++ b/tests/output.res @@ -1,4 +1,6 @@ servers localhost + localhost : 11211 + Error 0 -> SUCCESS Error 1 -> FAILURE Error 2 -> HOSTNAME LOOKUP FAILURE @@ -24,6 +26,7 @@ Error 21 -> SERVER END Error 22 -> SERVER DELETE Error 23 -> SERVER VALUE Error 24 -> STAT VALUE +Error 25 -> UNKOWN ERROR SEE MY_ERRNO Found key pid Found key uptime Found key time @@ -127,6 +130,7 @@ Error 21 -> SERVER END Error 22 -> SERVER DELETE Error 23 -> SERVER VALUE Error 24 -> STAT VALUE +Error 25 -> UNKOWN ERROR SEE MY_ERRNO Found key pid Found key uptime Found key time @@ -230,6 +234,7 @@ Error 21 -> SERVER END Error 22 -> SERVER DELETE Error 23 -> SERVER VALUE Error 24 -> STAT VALUE +Error 25 -> UNKOWN ERROR SEE MY_ERRNO Found key pid Found key uptime Found key time @@ -333,6 +338,7 @@ Error 21 -> SERVER END Error 22 -> SERVER DELETE Error 23 -> SERVER VALUE Error 24 -> STAT VALUE +Error 25 -> UNKOWN ERROR SEE MY_ERRNO Found key pid Found key uptime Found key time diff --git a/tests/test.c b/tests/test.c index ba1aee37..16afb65e 100644 --- a/tests/test.c +++ b/tests/test.c @@ -102,7 +102,6 @@ void delete_test(memcached_st *memc) assert(rc == MEMCACHED_SUCCESS); rc= memcached_delete(memc, key, strlen(key), (time_t)0); - WATCHPOINT_ERROR(rc); assert(rc == MEMCACHED_SUCCESS); } @@ -222,8 +221,6 @@ void get_test3(memcached_st *memc) string= memcached_get(memc, key, strlen(key), &string_length, &flags, &rc); - WATCHPOINT_ERRNO(memc->my_errno); - WATCHPOINT_ERROR(rc); assert(rc == MEMCACHED_SUCCESS); assert(string); assert(string_length == value_length); @@ -500,6 +497,12 @@ void behavior_test(memcached_st *memc) memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_MD5_HASHING, &set); value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_MD5_HASHING); assert(value == 0); + + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE); + assert(value > 0); + + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE); + assert(value > 0); } /* Test case provided by Cal Haldenbrand */