X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fmemcached_hosts.c;h=402018b4ae90cb58492776a5afed153c9c908b71;hb=3bd95102380ff8d9f2599f0965c09776344d0691;hp=0090625c4ff2bef08dd6a18a7acc3a296900d157;hpb=5c17e733976d4ce6287a68205a30d87f58f71ce1;p=m6w6%2Flibmemcached diff --git a/lib/memcached_hosts.c b/lib/memcached_hosts.c index 0090625c..402018b4 100644 --- a/lib/memcached_hosts.c +++ b/lib/memcached_hosts.c @@ -6,6 +6,34 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, unsigned int port, memcached_connection type); +#define MEMCACHED_WHEEL_SIZE 1024 +#define MEMCACHED_STRIDE 4 +static void rebalance_wheel(memcached_st *ptr) +{ + unsigned int x; + unsigned int y; + unsigned int latch; + unsigned int range; + + range= (MEMCACHED_WHEEL_SIZE / ptr->number_of_hosts); + + /* Seed the Wheel */ + memset(ptr->wheel, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE); + + for (latch= y= x= 0; x < MEMCACHED_WHEEL_SIZE; x++, latch++) + { + if (latch == MEMCACHED_STRIDE) + { + y++; + if (y == ptr->number_of_hosts) + y= 0; + latch= 0; + } + + ptr->wheel[x]= y; + } +} + static void host_reset(memcached_server_st *host, char *hostname, unsigned int port, memcached_connection type) { @@ -48,6 +76,8 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l } ptr->hosts[0].count= ptr->number_of_hosts; + rebalance_wheel(ptr); + return MEMCACHED_SUCCESS; } @@ -104,6 +134,8 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, ptr->number_of_hosts++; ptr->hosts[0].count++; + rebalance_wheel(ptr); + LIBMEMCACHED_MEMCACHED_SERVER_ADD_END(); return MEMCACHED_SUCCESS; @@ -154,8 +186,14 @@ unsigned int memcached_server_list_count(memcached_server_st *ptr) void memcached_server_list_free(memcached_server_st *ptr) { + unsigned int x; + if (ptr == NULL) return; + for (x= 0; x < ptr->count; x++) + if (ptr[x].address_info) + freeaddrinfo(ptr[x].address_info); + free(ptr); }