Comments (3)
CC @kardianos
from go.
@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.
@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)
- runtime: stack split at bad time for programs built with go1.23rc2 with optimizations disabled on linux/386 HOT 5
- cmd/compile: compile panics with generic type alias HOT 11
- time.parse cann ot parse RFC3339 reference string (or any other I tried) HOT 3
- crypto/tls: Using GODEBUG=tlsrsakex=1 behaves inconsistently between Go 1.22 and master HOT 2
- proposal: cmp: Add constants Lt, Eq, Gt HOT 2
- build: time failures HOT 1
- proposal: cmd/build: support for using go object files when no package is provided HOT 7
- proposal: slices: add AllMatch HOT 10
- x/pkgsite: -gorepo flag does not work HOT 7
- proposal: Pipeline Operator for Go HOT 3
- -
- -
- POST->OST,GET->ET??? HOT 2
- x/pkgsite: `X` is always visible on homepage search HOT 2
- x/time/rate: unexpected behaviour with 0 limit HOT 2
- cmd/trace/v2: pprof profiles always empty HOT 11
- net/http: Support Transfer-Encoding: gzip HOT 2
- doc: write release notes for Go 1.24 HOT 2
- cmd/trace/v2: pprof profiles always empty [1.22 backport] HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go.