summaryrefslogtreecommitdiffstats
path: root/db.go
diff options
context:
space:
mode:
authorraylu <raylu@mixpanel.com>2011-08-08 00:29:57 -0700
committerraylu <raylu@mixpanel.com>2011-08-08 00:29:57 -0700
commit0164e2515ae5ff6cedf37c4ae8344eae4d1b9b94 (patch)
tree5c2b7ebd5349ebc6fe1be7059073f8fe19249d21 /db.go
parente6b9900d3ba394eba04b84a8a01be644d43d642c (diff)
downloadaudioaxis-0164e2515ae5ff6cedf37c4ae8344eae4d1b9b94.tar.xz
dbpool!
Diffstat (limited to 'db.go')
-rw-r--r--db.go27
1 files changed, 16 insertions, 11 deletions
diff --git a/db.go b/db.go
index 8ded5ec..9434273 100644
--- a/db.go
+++ b/db.go
@@ -6,20 +6,25 @@ import (
"log"
)
-var db *mysql.Client
+var dbPool chan *mysql.Client
func initDb() {
log.SetFlags(log.Ltime | log.Lshortfile)
- var err os.Error
- db, err = mysql.DialTCP("173.228.31.111", "audio", "audio", "audio")
- if err != nil {
- log.Panicln(err)
+ const dbPoolSize = 4
+ dbPool = make(chan *mysql.Client, 4)
+
+ for i := 0; i < dbPoolSize; i++ {
+ db, err := mysql.DialTCP("173.228.31.111", "audio", "audio", "audio")
+ if err != nil {
+ log.Panicln(err)
+ }
+ db.Reconnect = true
+ dbPool <- db
}
- db.Reconnect = true
}
-func prepare(sql string, params ...interface{}) (*mysql.Statement, os.Error) {
+func prepare(db *mysql.Client, sql string, params ...interface{}) (*mysql.Statement, os.Error) {
query, err := db.Prepare(sql)
if err != nil {
log.Println(err)
@@ -38,8 +43,8 @@ func prepare(sql string, params ...interface{}) (*mysql.Statement, os.Error) {
return query, err
}
-func queryInt(sql string, params ...interface{}) (int, os.Error) {
- query, err := prepare(sql, params...)
+func queryInt(db *mysql.Client, sql string, params ...interface{}) (int, os.Error) {
+ query, err := prepare(db, sql, params...)
if err != nil {
return 0, err
}
@@ -61,8 +66,8 @@ func queryInt(sql string, params ...interface{}) (int, os.Error) {
}
// given an id ('abcd1234'), return the pid (1)
-func getpid(id string) int {
- pid, err := queryInt("SELECT `pid` FROM `playlist` WHERE `id` = ?", id)
+func getpid(db *mysql.Client, id string) int {
+ pid, err := queryInt(db, "SELECT `pid` FROM `playlist` WHERE `id` = ?", id)
if err != nil {
return -1
}