-
-
Save feyyazesat/de29a1eae145491821e1 to your computer and use it in GitHub Desktop.
an example of connection pooling implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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