Files
tasks/common/persistence/sql/sqlplugin/sqlite/conn_pool.go
T
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

85 lines
1.9 KiB
Go

package sqlite
import (
"sync"
"github.com/jmoiron/sqlx"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/resolver"
)
// This pool properly enabled the support for SQLite in the temporal server.
// Internal Temporal services are highly isolated, each will create at least a single connection to the database violating
// the SQLite concept of safety only within a single thread.
type connPool struct {
mu sync.Mutex
pool map[string]entry
}
type entry struct {
db *sqlx.DB
refCount int
}
func newConnPool() *connPool {
return &connPool{
pool: make(map[string]entry),
}
}
// Allocate allocates the shared database in the pool or returns already exists instance with the same DSN. If instance
// for such DSN already exists, it will be returned instead. Each request counts as reference until Close.
func (cp *connPool) Allocate(
cfg *config.SQL,
resolver resolver.ServiceResolver,
create func(cfg *config.SQL, resolver resolver.ServiceResolver) (*sqlx.DB, error),
) (db *sqlx.DB, err error) {
cp.mu.Lock()
defer cp.mu.Unlock()
dsn, err := buildDSN(cfg)
if err != nil {
return nil, err
}
if entry, ok := cp.pool[dsn]; ok {
entry.refCount++
return entry.db, nil
}
db, err = create(cfg, resolver)
if err != nil {
return nil, err
}
cp.pool[dsn] = entry{db: db, refCount: 1}
return db, nil
}
// Close virtual connection to database. Only closes for real once no references left.
func (cp *connPool) Close(cfg *config.SQL) {
cp.mu.Lock()
defer cp.mu.Unlock()
dsn, err := buildDSN(cfg)
if err != nil {
return
}
e, ok := cp.pool[dsn]
if !ok {
// no such database
return
}
e.refCount--
// todo: at the moment pool will persist a single connection to the DB for the whole duration of application
// temporal will start and stop DB connections multiple times, which will cause the loss of the cache
// and "db is closed" error
// if e.refCount == 0 {
// e.db.Close()
// delete(cp.pool, dsn)
// }
}