Git Product home page Git Product logo

Comments (3)

ianlancetaylor avatar ianlancetaylor commented on July 23, 2024

CC @kardianos

from go.

kardianos avatar kardianos commented on July 23, 2024

@kishoresenji I'm skeptical. However, if you provide a proof of concept or sketch that shows this could be reasonably extracted from the current code base, I'd be game to re-evaluate. If the connection use strategy was able to be extracted to some internal interface, and the type is used cleanly throughout, then perhaps this could clean up existing code.

However, if any implementation resulted in more complicated or harder to understand logic surrounding this, that would be an instant no.

from go.

kishoresenji avatar kishoresenji commented on July 23, 2024

@kardianos As the freeConn primitive in both strategies (most recently used or least recently used) is the same (i.e both strategies will order from oldest to newest on returedAt), we can keep it very simple and not add a new interface. The only place that needs a change is in the conn method on how the free connection is picked. There should not be any change anywhere else in the code.

We can have a new enum which models which strategy to be used.

type cachedConnPickStrategy uint8
const (
	mostRecentlyUsedStrategy cachedConnPickStrategy = iota
	leastRecentlyUsedStrategy
)

and LeastRecentlyUsed strategy can be enabled with an option on the DB. By default, it will be MostRecentlyUsed strategy.

func (db *DB) SetLeastRecentlyUsedCachedConnectionStrategy() {
	db.mu.Lock()
	defer db.mu.Unlock()
	db.cachedConnPickStrategy = leastRecentlyUsedStrategy
}

The conn method would change like this:

	// Prefer a free connection, if possible.
	if strategy == cachedOrNewConn {
		if conn := db.getCachedConnDBLocked(); conn != nil {
			conn.inUse = true
			if conn.expired(lifetime) {
				db.maxLifetimeClosed++
				db.mu.Unlock()
				conn.Close()
				return nil, driver.ErrBadConn
			}
			db.mu.Unlock()

			// Reset the session if required.
			if err := conn.resetSession(ctx); errors.Is(err, driver.ErrBadConn) {
				conn.Close()
				return nil, err
			}

			return conn, nil
		}
	}

where getCachedConnDBLocked is like the following:

func (db *DB) getCachedConnDBLocked() *driverConn {
	var conn *driverConn
	if len(db.freeConn) == 0 {
		return conn
	}
	switch db.cachedConnPickStrategy {
	case leastRecentlyUsedStrategy:
		conn = db.freeConn[0]
		db.freeConn = db.freeConn[1:]
	default:
		last := len(db.freeConn)-1
		conn = db.freeConn[last]
		db.freeConn = db.freeConn[:last]
	}
	return conn
}

from go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.