// NewMutexWithPool returns a new Mutex on a named resource connected to the Redis instances at given redis Pools.
973531
func NewMutexWithPool(name string, nodes []*redis.Pool) (*Mutex, error) {
	if len(nodes) == 0 {
		panic("redsync: nodes is empty")
	}

	genericNodes := make([]Pool, len(nodes))
	for i, node := range nodes {
		genericNodes[i] = Pool(node)
	}

	return &Mutex{
		Name:   name,
		Quorum: len(genericNodes)/2 + 1,
		nodes:  genericNodes,
	}, nil
}
1246919
func New(addrs []net.Addr) *RedSync {
	pools := make([]Pool, len(addrs))
	for i, addr := range addrs {
		dialTo := addr
		node := &redis.Pool{
			MaxActive: 1,
			Wait:      true,
			Dial: func() (redis.Conn, error) {
				return redis.Dial("tcp", dialTo.String())
			},
		}
		pools[i] = Pool(node)
	}

	return &RedSync{pools}
}
455775
func NewConnectionPoolHost(hostColonPort string) *ConnectionPool {
	// Create a redis Pool
	redisPool := &redis.Pool{
		// Maximum number of idle connections to the redis database
		MaxIdle:     maxIdleConnections,
		IdleTimeout: idleTimeout,
		// Anonymous function for calling new RedisConnectionTo with the host:port
		Dial: func() (redis.Conn, error) {
			conn, err := newRedisConnectionTo(hostColonPort)
			if err != nil {
				return nil, err
			}
			// If a password is given, use it to authenticate
			if password, _, ok := twoFields(hostColonPort, "@"); ok {
				if password != "" {
					if _, err := conn.Do("AUTH", password); err != nil {
						conn.Close()
						return nil, err
					}
				}
			}
			return conn, err
		},
	}
	pool := ConnectionPool(*redisPool)
	return &pool
}
1142137
func New(processor func(Job), jobBufsize int, maxIdle, maxActive uint64) *Pool {
	p := &Pool{
		processor: processor,
		jobs:      make(chan Job, jobBufsize),
		maxIdle:   maxIdle,
		maxActive: maxActive,
	}
	return p
}
154089
func NewObjectPool(ctx context.Context, factory PooledObjectFactory, config *ObjectPoolConfig) *ObjectPool {
	return NewObjectPoolWithAbandonedConfig(ctx, factory, config, nil)
}
1017904
func NewPool(newFn func() (Conn, error), maxIdle int) *Pool {
	return &Pool{Dial: newFn, MaxIdle: maxIdle}
}
699130
func NewPooledMarshaller(atl atlas.Atlas) PooledMarshaller {
	return PooledMarshaller{
		pool: sync.Pool{
			New: func() interface{} {
				return NewMarshallerAtlased(atl)
			},
		},
	}
}
1290266
func NewChannelPool(poolConfig *Config) (Pool, error) {
	if poolConfig.InitialCap < 0 || poolConfig.MaxCap <= 0 || poolConfig.InitialCap > poolConfig.MaxCap {
		return nil, errors.New("invalid capacity settings")
	}
	if poolConfig.Factory == nil {
		return nil, errors.New("invalid factory func settings")
	}
	if poolConfig.Close == nil {
		return nil, errors.New("invalid close func settings")
	}

	c := &channelPool{
		conns:       make(chan *idleConn, poolConfig.MaxCap),
		factory:     poolConfig.Factory,
		close:       poolConfig.Close,
		idleTimeout: poolConfig.IdleTimeout,
	}

	if poolConfig.Ping != nil {
		c.ping = poolConfig.Ping
	}

	for i := 0; i < poolConfig.InitialCap; i++ {
		conn, err := c.factory()
		if err != nil {
			c.Release()
			return nil, fmt.Errorf("factory is not able to fill the pool: %s", err)
		}
		c.conns <- &idleConn{conn: conn, t: time.Now()}
	}

	return c, nil
}
764958
func NewPool(capacity int, itemFactory ItemFactory) (Pool, error) {

	q, err := queue.NewChannelQueue(capacity)
	if err != nil {
		return nil, err
	}

	itemMap := make(map[uint64]*Item)
	pool := itemPool{itemMap: itemMap, itemQ: q, capacity: capacity, itemFactory: itemFactory}
	return &pool, nil
}
805981
func (p *Pool) getPool(addr string) *redis.Pool {

	defer scopedLock(&p.mutx)()

	pool, found := p.pools[addr]
	if !found {
		pool = redis.NewPool(func() (redis.Conn, error) {
			return p.dialFunc(addr)
		}, maxIdle)

		pool.TestOnBorrow = func(c redis.Conn, t time.Time) error {

			// for testing - count how many borrows we did
			atomic.AddInt64(&p.numBorrowed, 1)
			if time.Since(t) > testOnBorrowInterval {
				_, err := c.Do("PING")
				return err
			}
			return nil
		}

		p.pools[addr] = pool
	}

	return pool

}
651501
func NewPoolCache() *poolCache {
	return &poolCache{
		mutex: sync.RWMutex{},
		dirty: true,
		pools: make([]pool.ReadPool, 0),
	}
}
732852
func (pool *Pool) With(conf Config) *Pool {
	return &Pool{redis: pool.redis, conf: conf}
}
895941
func NewPool(seed int64, size uint64) *Pool {
	groupsize := nextNearestPow2uint64(size)
	pool := &Pool{
		size:    groupsize,
		sources: make([]NumberGenerator, groupsize),
	}
	// seed the pool
	pool.seed(seed)
	return pool
}
932290
func NewDRWMutex(name string, clnt *Dsync) *DRWMutex {
	return &DRWMutex{
		Name:       name,
		writeLocks: make([]string, clnt.dNodeCount),
		clnt:       clnt,
	}
}
629046
func (m *Mutex) Lock() error {
	value, err := m.genValueFunc()
	if err != nil {
		return err
	}

	for i := 0; i < m.tries; i++ {
		if i != 0 {
			time.Sleep(m.delayFunc(i))
		}

		start := time.Now()

		n := m.actOnPoolsAsync(func(pool Pool) bool {
			return m.acquire(pool, value)
		})

		until := time.Now().Add(m.expiry - time.Now().Sub(start) - time.Duration(int64(float64(m.expiry)*m.factor)) + 2*time.Millisecond)
		if n >= m.quorum && time.Now().Before(until) {
			m.value = value
			m.until = until
			return nil
		}
		m.actOnPoolsAsync(func(pool Pool) bool {
			return m.release(pool, value)
		})
	}

	return ErrFailed
}
786456
func newFreeClientPool(db ethdb.Database, freeClientCap uint64, totalLimit int, clock mclock.Clock, removePeer func(string)) *freeClientPool {
	pool := &freeClientPool{
		db:            db,
		clock:         clock,
		addressMap:    make(map[string]*freeClientPoolEntry),
		connPool:      prque.New(poolSetIndex),
		disconnPool:   prque.New(poolSetIndex),
		freeClientCap: freeClientCap,
		totalLimit:    totalLimit,
		removePeer:    removePeer,
	}
	pool.loadFromDb()
	return pool
}
888268
func New(id string) *ResourcePool {
	pool := &ResourcePool{}
	pool.ID = id

	return pool
}