diff --git a/dbpool.go b/dbpool.go index 848390f..d7404f7 100644 --- a/dbpool.go +++ b/dbpool.go @@ -2,6 +2,7 @@ package f5 import ( "q5" + "time" "fmt" "math" "strings" @@ -16,6 +17,7 @@ const ( ) type dataSource struct { + name string conn *q5.Mysql entry q5.ListHead } @@ -45,6 +47,7 @@ func (this *dbPool) RegisterDataSource(name string, host string, port int32, } for i := int32(0); i < size; i++ { ds := dataSource{} + ds.name = name ds.conn = q5.NewMysql(host, port, user, passwd, dataBase) ds.entry.Init(&ds) head.AddTail(&ds.entry) @@ -182,16 +185,35 @@ func (this *dbPool) PageQuery( }) } -func (this *dbPool) borrowConn(dataSource string) *dataSource { - this.lock.Lock() - defer this.lock.Unlock() - +func (this *dbPool) borrowConn(name string) *dataSource { + tryCount := 0 + for tryCount < 5 { + { + this.lock.Lock() + 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 } func (this *dbPool) returnConn(ds *dataSource) { this.lock.Lock() 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 {