connections as we need them).
Found a bug in stats where we were hardcoding the serer key (which
ended up a a hang when requesting stats).
} memcached_flags;
void md5_signature(unsigned char *key, unsigned int length, unsigned char *result);
-memcached_return memcached_connect(memcached_st *ptr);
+memcached_return memcached_connect(memcached_st *ptr, unsigned int server_key);
memcached_return memcached_response(memcached_st *ptr,
char *buffer, size_t buffer_length,
unsigned int server_key);
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
unsigned int server_key;
- rc= memcached_connect(ptr);
+ server_key= memcached_generate_hash(ptr, key, key_length);
- if (rc != MEMCACHED_SUCCESS)
+ if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS)
return rc;
- server_key= memcached_generate_hash(ptr, key, key_length);
-
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"%s %.*s %u\r\n", verb,
(int)key_length, key,
#include <sys/socket.h>
#include <netinet/tcp.h>
-memcached_return memcached_connect(memcached_st *ptr)
+memcached_return memcached_real_connect(memcached_st *ptr, unsigned int server_key)
{
- unsigned int x;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
- LIBMEMCACHED_MEMCACHED_CONNECT_START();
-
- if (ptr->connected == ptr->number_of_hosts)
- return MEMCACHED_SUCCESS;
+ if (ptr->hosts[server_key].fd == -1)
+ {
+ if ((h= gethostbyname(ptr->hosts[server_key].hostname)) == NULL)
+ return MEMCACHED_HOST_LOCKUP_FAILURE;
- if (!ptr->hosts)
- return MEMCACHED_NO_SERVERS;
+ servAddr.sin_family= h->h_addrtype;
+ memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
+ servAddr.sin_port = htons(ptr->hosts[server_key].port);
- for (x= 0; x < ptr->number_of_hosts; x++)
- {
- if (ptr->hosts[x].fd == -1)
+ /* Create the socket */
+ if ((ptr->hosts[server_key].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
- if ((h= gethostbyname(ptr->hosts[x].hostname)) == NULL)
- return MEMCACHED_HOST_LOCKUP_FAILURE;
-
- servAddr.sin_family= h->h_addrtype;
- memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
- servAddr.sin_port = htons(ptr->hosts[x].port);
+ ptr->my_errno= errno;
+ return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
+ }
- /* Create the socket */
- if ((ptr->hosts[x].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- ptr->my_errno= errno;
- return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
- }
+ /* bind any port number */
+ localAddr.sin_family = AF_INET;
+ localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ localAddr.sin_port = htons(0);
- /* bind any port number */
- localAddr.sin_family = AF_INET;
- localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- localAddr.sin_port = htons(0);
+ /* For the moment, not getting a nonblocking mode will note be fatal */
+ if (ptr->flags & MEM_NO_BLOCK)
+ {
+ int flags;
- /* For the moment, not getting a nonblocking mode will note be fatal */
- if (ptr->flags & MEM_NO_BLOCK)
- {
- int flags;
-
- flags= fcntl(ptr->hosts[x].fd, F_GETFL, 0);
- if (flags != -1)
- (void)fcntl(ptr->hosts[x].fd, F_SETFL, flags | O_NONBLOCK);
- }
+ flags= fcntl(ptr->hosts[server_key].fd, F_GETFL, 0);
+ if (flags != -1)
+ (void)fcntl(ptr->hosts[server_key].fd, F_SETFL, flags | O_NONBLOCK);
+ }
- if (ptr->flags & MEM_TCP_NODELAY)
- {
- int flag= 1;
+ if (ptr->flags & MEM_TCP_NODELAY)
+ {
+ int flag= 1;
- setsockopt(ptr->hosts[x].fd, IPPROTO_TCP, TCP_NODELAY,
- &flag, (socklen_t)sizeof(int));
- }
+ setsockopt(ptr->hosts[server_key].fd, IPPROTO_TCP, TCP_NODELAY,
+ &flag, (socklen_t)sizeof(int));
+ }
- /* connect to server */
+ /* connect to server */
test_connect:
- if (connect(ptr->hosts[x].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
- {
- switch (errno) {
- /* We are spinning waiting on connect */
- case EINPROGRESS:
- case EINTR:
- goto test_connect;
- case EISCONN: /* We were spinning waiting on connect */
- break;
- default:
+ if (connect(ptr->hosts[server_key].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
+ {
+ switch (errno) {
+ /* We are spinning waiting on connect */
+ case EINPROGRESS:
+ case EINTR:
+ goto test_connect;
+ case EISCONN: /* We were spinning waiting on connect */
+ break;
+ default:
ptr->my_errno= errno;
return MEMCACHED_HOST_LOCKUP_FAILURE;
}
-
ptr->connected++;
- }
}
}
- LIBMEMCACHED_MEMCACHED_CONNECT_END();
return MEMCACHED_SUCCESS;
}
+
+
+memcached_return memcached_connect(memcached_st *ptr, unsigned int server_key)
+{
+ memcached_return rc;
+ LIBMEMCACHED_MEMCACHED_CONNECT_START();
+
+ if (ptr->connected == ptr->number_of_hosts)
+ return MEMCACHED_SUCCESS;
+
+ if (!ptr->hosts)
+ return MEMCACHED_NO_SERVERS;
+
+ /* We need to clean up the multi startup piece */
+ if (server_key)
+ rc= memcached_real_connect(ptr, server_key);
+ else
+ {
+ unsigned int x;
+
+ for (x= 0; x < ptr->number_of_hosts; x++)
+ rc= memcached_real_connect(ptr, x);
+ }
+ LIBMEMCACHED_MEMCACHED_CONNECT_END();
+
+ return rc;
+}
LIBMEMCACHED_MEMCACHED_DELETE_START();
- rc= memcached_connect(ptr);
+ server_key= memcached_generate_hash(ptr, key, key_length);
- if (rc != MEMCACHED_SUCCESS)
+ if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS)
return rc;
- server_key= memcached_generate_hash(ptr, key, key_length);
if (expiration)
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
LIBMEMCACHED_MEMCACHED_FLUSH_START();
- rc= memcached_connect(ptr);
+ rc= memcached_connect(ptr, 0);
if (rc != MEMCACHED_SUCCESS)
rc= MEMCACHED_SOME_ERRORS;
char *value;
LIBMEMCACHED_MEMCACHED_GET_START();
+ server_key= memcached_generate_hash(ptr, key, key_length);
+
*value_length= 0;
- *error= memcached_connect(ptr);
+ *error= memcached_connect(ptr, server_key);
if (*error != MEMCACHED_SUCCESS)
goto error;
- server_key= memcached_generate_hash(ptr, key, key_length);
-
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n",
(int)key_length, key);
ptr->cursor_server= 0;
memset(buffer, 0, HUGE_STRING_LEN);
- rc= memcached_connect(ptr);
-
- if (rc != MEMCACHED_SUCCESS)
- return rc;
-
cursor_key_exec= (memcached_string_st **)malloc(sizeof(memcached_string_st *) * ptr->number_of_hosts);
memset(cursor_key_exec, 0, sizeof(memcached_string_st *) * ptr->number_of_hosts);
{
if (cursor_key_exec[x])
{
+ /* We need to doo something about non-connnected hosts in the future */
+ rc= memcached_connect(ptr, x);
+
memcached_string_st *string= cursor_key_exec[x];
memcached_string_append(ptr, string, "\r\n", 2);
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
size_t send_length, sent_length;
- rc= memcached_connect(ptr);
+ rc= memcached_connect(ptr, server_key);
if (rc != MEMCACHED_SUCCESS)
return rc;
while (1)
{
- rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 0);
+ rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
if (rc == MEMCACHED_STAT)
{
memcached_return rc;
memcached_stat_st *stats;
- rc= memcached_connect(ptr);
- if (rc != MEMCACHED_SUCCESS)
- {
- *error= rc;
- return NULL;
- }
-
stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1));
if (!stats)
{
}
memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1));
+ rc= MEMCACHED_SUCCESS;
for (x= 0; x < ptr->number_of_hosts; x++)
{
- rc= memcached_stats_fetch(ptr, stats+x, args, x);
- if (rc != MEMCACHED_SUCCESS)
+ memcached_return temp_return;
+ temp_return= memcached_stats_fetch(ptr, stats+x, args, x);
+ if (temp_return != MEMCACHED_SUCCESS)
rc= MEMCACHED_SOME_ERRORS;
}
memcached_server_add(&memc, hostname, port);
- rc= memcached_connect(&memc);
-
- if (rc != MEMCACHED_SUCCESS)
- return rc;
-
rc= memcached_stats_fetch(&memc, stat, args, 0);
memcached_free(&memc);
memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
- rc= memcached_connect(ptr);
- if (rc != MEMCACHED_SUCCESS)
- return rc;
-
/* Leaveing this assert in since only a library fubar could blow this */
assert(ptr->write_buffer_offset == 0);
server_key= memcached_generate_hash(ptr, key, key_length);
+ rc= memcached_connect(ptr, server_key);
+ if (rc != MEMCACHED_SUCCESS)
+ return rc;
+
write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"%s %.*s %x %llu %zu\r\n", storage_op_string(verb),
(int)key_length, key, flags,
memcached_return rc;
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
- rc= memcached_connect(ptr);
+ rc= memcached_connect(ptr, 0);
if (rc != MEMCACHED_SUCCESS)
rc= MEMCACHED_SOME_ERRORS;