func NewEtcdConn(zkAddr string, zkSessionTimeout int) (Conn, error) { singleInstanceLock.Lock() defer singleInstanceLock.Unlock() if etcdInstance != nil { return etcdInstance, nil } p := pools.NewResourcePool(func() (pools.Resource, error) { cluster := strings.Split(zkAddr, ",") for i, addr := range cluster { if !strings.HasPrefix(addr, "http://") { cluster[i] = "http://" + addr } } return &PooledEtcdClient{c: etcd.NewClient(cluster)}, nil }, 10, 10, 0) etcdInstance = &etcdImpl{ cluster: zkAddr, pool: p, indexMap: make(map[string]uint64), } log.Infof("new etcd %s", zkAddr) if etcdInstance == nil { return nil, errors.New("unknown error") } return etcdInstance, nil }
// Open must be call before starting to use the pool. func (cp *ConnectionPool) Open(connFactory CreateConnectionFunc) { cp.mu.Lock() defer cp.mu.Unlock() f := func() (pools.Resource, error) { return connFactory(cp) } cp.connections = pools.NewResourcePool(f, cp.capacity, cp.capacity, cp.idleTimeout) }
// NewPool creates a Pool. func NewPool(addr string, capability int, f createConnFunc) *Pool { poolFunc := func() (pools.Resource, error) { r, err := f(addr) if err == nil && r == nil { return nil, errors.Errorf("cannot create nil connection") } return r, errors.Trace(err) } p := new(Pool) p.p = pools.NewResourcePool(poolFunc, capability, capability, poolIdleTimeoutSeconds*time.Second) return p }
func (cp *CachePool) Open() { cp.mu.Lock() defer cp.mu.Unlock() if cp.pool != nil { panic("rowcache is already open") } if cp.rowCacheConfig.Binary == "" { panic("rowcache binary not specified") } cp.startMemcache() log.Infof("rowcache is enabled") f := func() (pools.Resource, error) { return memcache.Connect(cp.port, 10*time.Second) } cp.pool = pools.NewResourcePool(f, cp.capacity, cp.capacity, cp.idleTimeout) if cp.memcacheStats != nil { cp.memcacheStats.Open() } }