+ memcached_server_st *list;
+ uint32_t pointer_counter= 0;
+ uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
+ uint32_t pointer_per_hash= 1;
+ uint64_t total_weight= 0;
+ uint32_t is_ketama_weighted= 0;
+ uint32_t is_auto_ejecting= 0;
+ uint32_t points_per_server= 0;
+ uint32_t live_servers= 0;
+ struct timeval now;
+
+ if (gettimeofday(&now, NULL) != 0)
+ {
+ ptr->cached_errno = errno;
+ return MEMCACHED_ERRNO;
+ }
+
+ list = ptr->hosts;
+
+ /* count live servers (those without a retry delay set) */
+ is_auto_ejecting= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS);
+ if (is_auto_ejecting)
+ {
+ live_servers= 0;
+ ptr->next_distribution_rebuild= 0;
+ for (host_index= 0; host_index < ptr->number_of_hosts; ++host_index)
+ {
+ if (list[host_index].next_retry <= now.tv_sec)
+ live_servers++;
+ else
+ {
+ if (ptr->next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->next_distribution_rebuild)
+ ptr->next_distribution_rebuild= list[host_index].next_retry;
+ }
+ }
+ }
+ else
+ live_servers= ptr->number_of_hosts;
+
+ is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
+ points_per_server= is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER;
+
+ if (live_servers == 0)
+ return MEMCACHED_SUCCESS;
+
+ if (live_servers > ptr->continuum_count)
+ {
+ memcached_continuum_item_st *new_ptr;
+
+ if (ptr->call_realloc)
+ new_ptr= (memcached_continuum_item_st *)ptr->call_realloc(ptr, ptr->continuum,
+ sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
+ else
+ new_ptr= (memcached_continuum_item_st *)realloc(ptr->continuum,
+ sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
+
+ if (new_ptr == 0)
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+
+ ptr->continuum= new_ptr;
+ ptr->continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
+ }
+
+ if (is_ketama_weighted)
+ {
+ for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
+ {
+ if (list[host_index].weight == 0)
+ {
+ list[host_index].weight = 1;
+ }
+ if (!is_auto_ejecting || list[host_index].next_retry <= now.tv_sec)
+ total_weight += list[host_index].weight;
+ }
+ }