@@ -36,8 +36,16 @@ import (
36
36
// PooledDBConnection objects.
37
37
type ConnectionPool struct {
38
38
* smartconnpool.ConnPool [* DBConnection ]
39
+
40
+ name string
39
41
}
40
42
43
+ // usedNames is for preventing expvar from panicking. Tests
44
+ // create pool objects multiple time. If a name was previously
45
+ // used, expvar initialization is skipped.
46
+ // through non-test code.
47
+ var usedNames = make (map [string ]bool )
48
+
41
49
// NewConnectionPool creates a new ConnectionPool. The name is used
42
50
// to publish stats only.
43
51
func NewConnectionPool (name string , stats * servenv.Exporter , capacity int , idleTimeout time.Duration , maxLifetime time.Duration , dnsResolutionFrequency time.Duration ) * ConnectionPool {
@@ -47,7 +55,18 @@ func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleT
47
55
MaxLifetime : maxLifetime ,
48
56
RefreshInterval : dnsResolutionFrequency ,
49
57
}
50
- return & ConnectionPool {ConnPool : smartconnpool .NewPool (& config )}
58
+ cp := & ConnectionPool {ConnPool : smartconnpool .NewPool (& config ), name : name }
59
+ if name == "" || usedNames [name ] {
60
+ return cp
61
+ }
62
+ usedNames [name ] = true
63
+
64
+ if stats == nil {
65
+ // This is unnamed exported so it will use the stats functions directly when adding to the expvar.
66
+ stats = servenv .NewExporter ("" , "" )
67
+ }
68
+ cp .ConnPool .RegisterStats (stats , name )
69
+ return cp
51
70
}
52
71
53
72
// Open must be called before starting to use the pool.
0 commit comments