public
Last active

ConnectionPool Implementation

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
This function creates a connection to the database. It shouldn't have to know anything
about the pool, It will be called N times where N is the size of the requested pool.
*/
func initCirrusConnections() (interface{}, error) {
dbserver, _ := configFile.GetString("default", "dbserver")
dbuser, _ := configFile.GetString("default", "dbuser")
dbpass, _ := configFile.GetString("default", "dbpass")
 
db := autorc.New("tcp", "", dbserver, dbuser, dbpass)
 
err := db.Use("cirrus")
if err != nil {
return nil, err
}
 
return db, nil
}
 
func CameraList(ctx *web.Context, groupid string) ([]Camera, error) {
// Grab a connection from the pool and type assert to our database type
db := dbPool.GetConnection().(*autorc.Conn)
// When this function exits, release our connection back to the pool
defer dbPool.ReleaseConnection(db)
 
cameras, err := GetCameras(db, groupid)
if err != nil {
return nil, web.WebError{500, err.Error()}
}
 
return cameras, nil
}
 
 
func main() {
// Create a pool of 3 connections using the initCirrusConnections function
err := dbPool.InitPool(3, initCirrusConnections)
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.