// GetConnection returns the TabletConn of the given tablet.
330641
func (fhc *FakeHealthCheck) GetConnection(key string) queryservice.QueryService {
	fhc.mu.RLock()
	defer fhc.mu.RUnlock()
	if item := fhc.items[key]; item != nil {
		return item.conn
	}
	return nil
}
1117591
func getDatabaseConnection(c context.Context) (*sql.DB, error) {
	// Update the database pointer if the database settings have changed. This operation is costly, but should be rare.
	// Done here to ensure that a connection established with an outdated connection string is closed as soon as possible.
	settings, err := settings.Get(c)
	if err != nil {
		return nil, err
	}
	connectionString := fmt.Sprintf("%s:%s@cloudsql(%s)/%s", settings.Username, settings.Password, settings.Server, settings.Database)

	// If the connection string matches what we expect, the current pointer is correct so just return it.
	dbLock.RLock()
	if connectionString == dbConnectionString {
		defer dbLock.RUnlock()
		return db, nil
	}
	dbLock.RUnlock()

	// The connection string doesn't match so the db pointer needs to be created or updated.
	dbLock.Lock()
	defer dbLock.Unlock()

	// Releasing the read lock may have allowed another concurrent request to grab the write lock first so it's
	// possible we no longer need to do anything. Check again while holding the write lock.
	if connectionString == dbConnectionString {
		return db, nil
	}

	if db != nil {
		if err := db.Close(); err != nil {
			logging.Warningf(c, "Failed to close the previous database connection: %s", err.Error())
		}
	}
	db, err = sql.Open("mysql", connectionString)
	if err != nil {
		return nil, fmt.Errorf("failed to open a new database connection: %s", err.Error())
	}

	// AppEngine limit.
	db.SetMaxOpenConns(12)
	dbConnectionString = connectionString
	return db, nil
}
1369940
func GetRemoteConnection(path string) (client.Connection, error) {
	managerLock.RLock()
	remoteclient, ok := manager[remote]
	managerLock.RUnlock()

	if !ok || remoteclient.client == nil {
		return nil, ErrNotInitialized
	}
	return remoteclient.GetConnection(path)
}
1060372
func (rc *Connector) getConnector(scope, namePrefix string) (dosa.Connector, error) {
	router := rc.config.FindRouter(scope, namePrefix)

	c, ok := rc.connectors[router.Connector]
	if !ok {
		return nil, fmt.Errorf("can't find %q connector", router.Connector)
	}

	return c, nil
}
651955
func (c *Connection) Get(property string) (interface{}, error) {
	value, err := c.Call("get_property", property)
	return value, err
}
889646
func (c *Chain) getConn() (conn net.Conn, err error) {
	if c.IsEmpty() {
		err = ErrEmptyChain
		return
	}
	nodes := c.Nodes()
	node := nodes[0]

	cn, err := node.Client.Dial(node.Addr, node.DialOptions...)
	if err != nil {
		node.MarkDead()
		return
	}

	cn, err = node.Client.Handshake(cn, node.HandshakeOptions...)
	if err != nil {
		node.MarkDead()
		return
	}
	node.ResetDead()

	preNode := node
	for _, node := range nodes[1:] {
		var cc net.Conn
		cc, err = preNode.Client.Connect(cn, node.Addr)
		if err != nil {
			cn.Close()
			node.MarkDead()
			return
		}
		cc, err = node.Client.Handshake(cc, node.HandshakeOptions...)
		if err != nil {
			cn.Close()
			node.MarkDead()
			return
		}
		node.ResetDead()

		cn = cc
		preNode = node
	}

	conn = cn
	return
}
93824
func (d *logDrain) getConnection(forceNew bool) (net.Conn, error) {
	// If we have a connection, it's not old, and we're not focing a new one...
	if d.conn != nil && !forceNew {
		// then return the existing connection
		return d.conn, nil
	}
	// If ANY of those conditions weren't met, it's time for a new connection.
	// If we have an existing one, close it and nil it out, too for good measure.
	if d.conn != nil {
		if err := d.conn.Close(); err != nil {
			log.Println("drain: Error closing connection.  Drain may be leaking connections.", err)
		}
		d.conn = nil
	}
	// Try a few times...
	var err error
	for attempt := 1; attempt <= maxFailedConns; attempt++ {
		d.conn, err = net.DialTimeout(d.proto, d.uri, dialTimeout)
		if err == nil {
			// We got our connection...
			// Make it good for only so long.  See comment above on connRefreshInterval.
			err = d.conn.SetWriteDeadline(time.Now().Add(connRefreshInterval))
			if err != nil {
				return nil, err
			}
			// Break out of the loop and return
			return d.conn, nil
		}
	}
	// Multiple attempts to dial have failed.  Whatever the problem is, we shouldn't expect that
	// it will resolve itself quickly.
	log.Printf("drain: Experienced %d consecutive failed connection attempts; muting drain for %s.", maxFailedConns, mutePeriod)
	// Immediately "mute" the drain.  This will prevent us from wasting resources repeatedly dialing
	// and failing while the message queue gets backed up.  This will give the network a break and
	// allow us to empty the queue.
	d.muted = true
	// Unmute the drain when the mute interval has elapsed
	go func() {
		time.Sleep(mutePeriod)
		d.muted = false
	}()
	// Return the error from the last failed connection attempt
	return nil, err
}
537511
func (b *Bucket) getRandomConnection() (*memcached.Client, *connectionPool, error) {
	for {
		var currentPool = 0
		pools := b.getConnPools(false /* not already locked */)
		if len(pools) == 0 {
			return nil, nil, fmt.Errorf("No connection pool found")
		} else if len(pools) > 1 { // choose a random connection
			currentPool = rand.Intn(len(pools))
		} // if only one pool, currentPool defaults to 0, i.e., the only pool

		// get the pool
		pool := pools[currentPool]
		conn, err := pool.Get()
		if err != errClosedPool {
			return conn, pool, err
		}

		// If conn pool was closed, because another goroutine refreshed the vbucket map, retry...
	}
}
130957
func (nd *Node) getConnection(deadline time.Time, timeout time.Duration) (conn *Connection, err error) {
	return nd.getConnectionWithHint(deadline, timeout, 0)
}
124411
func (e *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
	e.mutex.Lock()
	defer e.mutex.Unlock()

	if !(bus == e.GetDefaultBus()) {
		return nil, errors.New("Unsupported I2C bus")
	}
	if e.i2cBus == nil {
		if bus == 6 && e.board == "arduino" {
			e.arduinoI2CSetup()
		}
		e.i2cBus, err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
	}
	return i2c.NewConnection(e.i2cBus, address), err
}
1116017
func (r Network_Interconnect_Tenant) GetConnection(serviceKey *string, provider *string) (resp string, err error) {
	params := []interface{}{
		serviceKey,
		provider,
	}
	err = r.Session.DoRequest("SoftLayer_Network_Interconnect_Tenant", "getConnection", params, &r.Options, &resp)
	return
}
1348521
func getConnection() (conn redis.Conn, err error) {
	if currClient == nil {
		return nil, errors.New("No current Redis client: create a new client before getting a connection from it")
	}

	conn = currClient.Pool.Get()
	return conn, nil
}
455828
func (c *Adaptor) GetSpiConnection(busNum, chipNum, mode, bits int, maxSpeed int64) (connection spi.Connection, err error) {
	c.mutex.Lock()
	defer c.mutex.Unlock()

	if (busNum < 0) || (busNum > 1) {
		return nil, fmt.Errorf("Bus number %d out of range", busNum)
	}

	if c.spiBuses[busNum] == nil {
		c.spiBuses[busNum], err = spi.GetSpiConnection(busNum, chipNum, mode, bits, maxSpeed)
	}

	return c.spiBuses[busNum], err
}
1347853
func (e TabletStats) GetTabletHostPort() string {
	vtPort := e.Tablet.PortMap["vt"]
	return netutil.JoinHostPort(e.Tablet.Hostname, vtPort)
}
367425
func GetNetwork(tx ReadTx, id string) *api.Network {
	n := tx.get(tableNetwork, id)
	if n == nil {
		return nil
	}
	return n.(*api.Network)
}
962920
func (e *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
	if (bus < 0) || (bus > 2) {
		return nil, fmt.Errorf("Bus number %d out of range", bus)
	}
	if e.i2cBuses[bus] == nil {
		e.i2cBuses[bus], err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
	}
	return i2c.NewConnection(e.i2cBuses[bus], address), err
}
767023
func (qe *QueryEngine) getQueryConn(ctx context.Context) (*connpool.DBConn, error) {
	waiterCount := qe.queryPoolWaiters.Add(1)
	defer qe.queryPoolWaiters.Add(-1)

	if waiterCount > qe.queryPoolWaiterCap.Get() {
		return nil, vterrors.New(vtrpcpb.Code_RESOURCE_EXHAUSTED, "query pool waiter count exceeded")
	}

	timeout := qe.connTimeout.Get()
	if timeout != 0 {
		ctxTimeout, cancel := context.WithTimeout(ctx, timeout)
		defer cancel()
		conn, err := qe.conns.Get(ctxTimeout)
		if err != nil {
			return nil, vterrors.Errorf(vtrpcpb.Code_RESOURCE_EXHAUSTED, "query pool wait time exceeded")
		}
		return conn, err
	}
	return qe.conns.Get(ctx)
}