Akagi201
2/3/2016 - 12:50 AM

ConnectionPool Implementation

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)
}