1
This commit is contained in:
parent
f2b83a8732
commit
9e85076c6d
26
dbpool.go
26
dbpool.go
@ -2,6 +2,7 @@ package f5
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"q5"
|
"q5"
|
||||||
|
"time"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
@ -16,6 +17,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type dataSource struct {
|
type dataSource struct {
|
||||||
|
name string
|
||||||
conn *q5.Mysql
|
conn *q5.Mysql
|
||||||
entry q5.ListHead
|
entry q5.ListHead
|
||||||
}
|
}
|
||||||
@ -45,6 +47,7 @@ func (this *dbPool) RegisterDataSource(name string, host string, port int32,
|
|||||||
}
|
}
|
||||||
for i := int32(0); i < size; i++ {
|
for i := int32(0); i < size; i++ {
|
||||||
ds := dataSource{}
|
ds := dataSource{}
|
||||||
|
ds.name = name
|
||||||
ds.conn = q5.NewMysql(host, port, user, passwd, dataBase)
|
ds.conn = q5.NewMysql(host, port, user, passwd, dataBase)
|
||||||
ds.entry.Init(&ds)
|
ds.entry.Init(&ds)
|
||||||
head.AddTail(&ds.entry)
|
head.AddTail(&ds.entry)
|
||||||
@ -182,16 +185,35 @@ func (this *dbPool) PageQuery(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *dbPool) borrowConn(dataSource string) *dataSource {
|
func (this *dbPool) borrowConn(name string) *dataSource {
|
||||||
|
tryCount := 0
|
||||||
|
for tryCount < 5 {
|
||||||
|
{
|
||||||
this.lock.Lock()
|
this.lock.Lock()
|
||||||
defer this.lock.Unlock()
|
defer this.lock.Unlock()
|
||||||
|
if head, ok := this.dataSourceHash[name]; ok {
|
||||||
|
if !head.Empty() {
|
||||||
|
next := head.Next()
|
||||||
|
next.Del()
|
||||||
|
return next.GetData().(*dataSource)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
tryCount++
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *dbPool) returnConn(ds *dataSource) {
|
func (this *dbPool) returnConn(ds *dataSource) {
|
||||||
this.lock.Lock()
|
this.lock.Lock()
|
||||||
defer this.lock.Unlock()
|
defer this.lock.Unlock()
|
||||||
|
|
||||||
|
if head, ok := this.dataSourceHash[ds.name]; ok {
|
||||||
|
head.AddTail(&ds.entry)
|
||||||
|
} else {
|
||||||
|
panic(fmt.Sprintf("returnConn error %s", ds.name))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *dbPool) joinSelectFields(fields []string) string {
|
func (this *dbPool) joinSelectFields(fields []string) string {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user