mirror of
https://github.com/golang/go
synced 2024-11-18 11:34:45 -07:00
database/sql: use RWMutex for driver registration
Change-Id: I0f494c9f17cb6bb0cf5e7214cf033fdbd48f27f7 Reviewed-on: https://go-review.googlesource.com/16240 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
f75f2f3fcc
commit
7f34a2dac8
@ -24,7 +24,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
driversMu sync.Mutex
|
driversMu sync.RWMutex
|
||||||
drivers = make(map[string]driver.Driver)
|
drivers = make(map[string]driver.Driver)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -52,8 +52,8 @@ func unregisterAllDrivers() {
|
|||||||
|
|
||||||
// Drivers returns a sorted list of the names of the registered drivers.
|
// Drivers returns a sorted list of the names of the registered drivers.
|
||||||
func Drivers() []string {
|
func Drivers() []string {
|
||||||
driversMu.Lock()
|
driversMu.RLock()
|
||||||
defer driversMu.Unlock()
|
defer driversMu.RUnlock()
|
||||||
var list []string
|
var list []string
|
||||||
for name := range drivers {
|
for name := range drivers {
|
||||||
list = append(list, name)
|
list = append(list, name)
|
||||||
@ -465,9 +465,9 @@ var connectionRequestQueueSize = 1000000
|
|||||||
// function should be called just once. It is rarely necessary to
|
// function should be called just once. It is rarely necessary to
|
||||||
// close a DB.
|
// close a DB.
|
||||||
func Open(driverName, dataSourceName string) (*DB, error) {
|
func Open(driverName, dataSourceName string) (*DB, error) {
|
||||||
driversMu.Lock()
|
driversMu.RLock()
|
||||||
driveri, ok := drivers[driverName]
|
driveri, ok := drivers[driverName]
|
||||||
driversMu.Unlock()
|
driversMu.RUnlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
|
return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user