Example #1
0
// return a ConnectionPool instance, and for each server initializes a connection pool
func New(config *config.Config) (Pool, error) {
	if len(config.Servers) == 0 {
		return nil, errors.New("Memcached : Memcached Servers must not empty")
	}

	pool := &ConnectionPool{
		pools:      make([]chan *common.Conn, 0, len(config.Servers)),
		config:     config,
		factory:    factory.NewConnectionFactory(config),
		consistent: selector.NewConsistent(config),
	}

	for i := 0; i < len(pool.config.Servers); i++ {
		pool.pools = append(pool.pools, make(chan *common.Conn, pool.config.InitConns))

		for j := 0; j < int(pool.config.InitConns / 2 + 1); j++ {
			conn, err := pool.factory.NewTcpConnect(pool.config.Servers[i], i)

			if err != nil {
				return nil, err
			} else {
				pool.pools[i] <- conn
			}
		}

		pool.consistent.Add(pool.config.Servers[i])
	}

	pool.consistent.RefreshTicker()

	return pool, nil
}
func BenchmarkConsistentAdd(b *testing.B) {
	conf := config.New()
	conf.Servers = servers

	consistent := selector.NewConsistent(conf)

	for i := 0; i < 300; i++ {
		key := fmt.Sprintf("127.0.0.1_%d", i)
		consistent.Add(key)
	}
}
func TestAdd(t *testing.T) {
	conf := config.New()
	conf.Servers = servers

	consistent := selector.NewConsistent(conf)

	for _, server := range servers {
		consistent.Add(server)
	}

	consistent.Print()
}
func BenchmarkConsistentGet(b *testing.B) {
	conf := config.New()
	conf.Servers = servers

	consistent := selector.NewConsistent(conf)

	for _, v := range servers {
		consistent.Add(v)
	}

	for i := 0; i < b.N; i++ {
		key := fmt.Sprintf("127.0.0.1_%d", i)
		b.Log(consistent.Get(key))
	}
}
func TestGet(t *testing.T) {
	conf := config.New()
	conf.Servers = servers

	consistent := selector.NewConsistent(conf)

	for _, server := range servers {
		consistent.Add(server)
	}

	for i := 0; i < 30; i++ {
		key := servers[i%len(servers)]
		t.Log(consistent.Get(fmt.Sprintf("%s-oef%d", key, i)))
	}
}