Skip to content

Instantly share code, notes, and snippets.

@rday
Created August 28, 2012 21:59
Show Gist options
  • Save rday/3504712 to your computer and use it in GitHub Desktop.
Save rday/3504712 to your computer and use it in GitHub Desktop.
ConnectionPool Implementation
/**
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment