// give the ability to discard a connection from a pool
// useful for ditching connections to the wrong node after a rebalance
194639
func (cp *connectionPool) Discard(c *memcached.Client) {
	<-cp.createsem
	c.Close()
}
546513
func (nd *Node) connectionLimitReached() (res bool) {
	if nd.cluster.clientPolicy.LimitConnectionsToQueueSize {
		cc := nd.connectionCount.IncrementAndGet()
		if cc > nd.cluster.clientPolicy.ConnectionQueueSize {
			res = true
		}

		nd.connectionCount.DecrementAndGet()
	}

	return res
}
1299534
func (s *Server) removeNodeConn(ctx *RPCContext) {
	// Hotpath the no-op
	if ctx == nil || ctx.NodeID == "" {
		return
	}

	s.nodeConnsLock.Lock()
	defer s.nodeConnsLock.Unlock()
	conns, ok := s.nodeConns[ctx.NodeID]
	if !ok {
		return
	}

	// It is important that we check that the connection being removed is the
	// actual stored connection for the client. It is possible for the client to
	// dial various addresses that all route to the same server. The most common
	// case for this is the original address the client uses to connect to the
	// server differs from the advertised address sent by the heartbeat.
	for i, conn := range conns {
		if conn.Ctx.Conn.LocalAddr().String() == ctx.Conn.LocalAddr().String() &&
			conn.Ctx.Conn.RemoteAddr().String() == ctx.Conn.RemoteAddr().String() {

			if len(conns) == 1 {
				// We are deleting the last conn, remove it from the map
				delete(s.nodeConns, ctx.NodeID)
			} else {
				// Slice out the connection we are deleting
				s.nodeConns[ctx.NodeID] = append(s.nodeConns[ctx.NodeID][:i], s.nodeConns[ctx.NodeID][i+1:]...)
			}

			return
		}
	}
}
74996
func (p *Pool) Cleanup() {
	p.Stop()
	p.lock.Lock()
	defer p.lock.Unlock()
	p.baseCancel()
}
902296
func (pool *serverPool) disconnect(entry *poolEntry) {
	stopped := false
	select {
	case <-pool.quit:
		stopped = true
	default:
	}
	log.Debug("Disconnected old entry", "enode", entry.node.ID())
	req := &disconnReq{entry: entry, stopped: stopped, done: make(chan struct{})}

	// Block until disconnection request is served.
	pool.disconnCh <- req
	<-req.done
}
2694
func (queue *redisQueue) CloseInConnection() {
	queue.redisClient.Del(queue.unackedKey)
	queue.redisClient.Del(queue.consumersKey)
	queue.redisClient.SRem(queue.queuesKey, queue.name)
}
989873
func (p *pool) Remove(network *linux_backend.Network, logger lager.Logger) error {
	p.mu.Lock()
	defer p.mu.Unlock()

	logger = logger.Session("remove")

	logger.Info("allocating-ips", lager.Data{"allocated-subnets": subnetsStr(subnets(p.allocated)), "allocated-ips": ipsStr(p.allocated[network.Subnet.String()])})

	if network.IP == nil {
		return ErrIpCannotBeNil
	}

	for _, existing := range p.allocated[network.Subnet.String()] {
		if existing.Equal(network.IP) {
			return ErrOverlapsExistingSubnet
		}
	}

	p.allocated[network.Subnet.String()] = append(p.allocated[network.Subnet.String()], network.IP)
	logger.Info("allocated-ips", lager.Data{"allocated-subnets": subnetsStr(subnets(p.allocated)), "allocated-ips": ipsStr(p.allocated[network.Subnet.String()])})

	return nil
}
409922
func (t *Transaction) Discard() error {
	if err := t.conn.SendCommand("DISCARD"); err != nil {
		return err
	}
	_, err := t.conn.RecvReply()
	return err
}
192655
func (c *Client) NodeBalancerConfigDelete(nbID int, confID int) error {
	args := make(map[string]interface{})
	args["NodeBalancerID"] = nbID
	args["ConfigID"] = confID

	_, err := c.apiCall("nodebalancer.config.delete", args)
	if err != nil {
		return err
	}

	return nil
}
362761
func (g *Graph) DiscardLowFrequencyNodes(nodeCutoff int64) NodeSet {
	return makeNodeSet(g.Nodes, nodeCutoff)
}
554083
func (pool *hostConnPool) Pick() *Conn {
	pool.mu.RLock()
	defer pool.mu.RUnlock()

	if pool.closed {
		return nil
	}

	size := len(pool.conns)
	if size < pool.size {
		// try to fill the pool
		go pool.fill()

		if size == 0 {
			return nil
		}
	}

	pos := int(atomic.AddUint32(&pool.pos, 1) - 1)

	var (
		leastBusyConn    *Conn
		streamsAvailable int
	)

	// find the conn which has the most available streams, this is racy
	for i := 0; i < size; i++ {
		conn := pool.conns[(pos+i)%size]
		if streams := conn.AvailableStreams(); streams > streamsAvailable {
			leastBusyConn = conn
			streamsAvailable = streams
		}
	}

	return leastBusyConn
}
1045302
func (ctn *Connection) Close() {
	ctn.closer.Do(func() {
		if ctn != nil && ctn.conn != nil {
			// deregister
			if ctn.node != nil {
				ctn.node.connectionCount.DecrementAndGet()
				atomic.AddInt64(&ctn.node.stats.ConnectionsClosed, 1)
			}

			if err := ctn.conn.Close(); err != nil {
				Logger.Warn(err.Error())
			}
			ctn.conn = nil
			ctn.dataBuffer = nil
		}
	})
}
1194805
func (conn *CpConn) Close() error {
	if conn.pool == nil {
		return errors.New("Connection not belong any connection pool.")
	}
	return conn.pool.Put(conn.Conn)
}
380210
func (pool *AddressBasedPool) RemoveConn(addr string) {
	pool.Lock()
	if conn, ok := pool.conns[addr]; ok {
		conn.Close()
		delete(pool.conns, addr)
	}
	pool.Unlock()
}
1004072
func (c *Connection) Close() {
	c.decrement()
	if c.count <= 0 {
		if c.expires == 0 {
			c.pool.mu.Lock()
			c.pool.errorChannel <- c.removeFromPool()
			c.pool.mu.Unlock()
			return
		}

		c.mu.Lock()
		c.closeTime = time.Now().Add(c.expires)
		c.mu.Unlock()
	}
}
944060
func (s *sessionUDP) Discard() {
	s.conn = nil
	s.raddr = nil
	s.laddr = nil
	s.m = s.m[:cap(s.m)]
	s.oob = s.oob[:cap(s.oob)]

	s.f.udpPool.Put(s)
}
695253
func (nd *Node) makeConnectionForPool(deadline time.Time, hint byte) {
	if deadline.IsZero() {
		deadline = time.Now().Add(_DEFAULT_TIMEOUT)
	}

	// don't even try to make a new connection if connection limit is reached
	if nd.connectionLimitReached() {
		return
	}

L:
	// don't loop forever; free the goroutine after the deadline
	if time.Now().After(deadline) {
		return
	}

	conn, err := nd.newConnection(false)
	if err != nil {
		// The following check can help break the loop under heavy load and remove
		// quite a lot of latency.
		if err == ErrTooManyConnectionsForNode {
			// The connection pool is already full. No need for more connections.
			return
		}

		Logger.Error("Error trying to making a connection to the node %s: %s", nd.String(), err.Error())

		time.Sleep(time.Millisecond)
		goto L
	}

	nd.putConnectionWithHint(conn, hint)
}