Skip to content

Instantly share code, notes, and snippets.

@feyyazesat
Forked from rday/gist:3504712
Created March 17, 2015 18:56
Show Gist options
  • Save feyyazesat/de29a1eae145491821e1 to your computer and use it in GitHub Desktop.
Save feyyazesat/de29a1eae145491821e1 to your computer and use it in GitHub Desktop.
an example of connection pooling 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