Browse Source

Switch to pgxpool for concurrent database connections

master
Devin Dooley 1 year ago
parent
commit
90c1cc669e
8 changed files with 19 additions and 18 deletions
  1. +5
    -5
      api/api.go
  2. +2
    -2
      api/auth.go
  3. +3
    -3
      api/banking.go
  4. +3
    -3
      api/user.go
  5. +1
    -1
      api/weather.go
  6. +3
    -3
      db/db.go
  7. +1
    -0
      go.sum
  8. +1
    -1
      me.go

+ 5
- 5
api/api.go View File

@ -5,7 +5,7 @@ import (
"github.com/dooleydevin/me/config"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/plaid/plaid-go/plaid"
"log"
"net/http"
@ -13,18 +13,18 @@ import (
)
var (
conn *pgx.Conn
pool *pgxpool.Pool
conf *config.Config
plaidClient *plaid.Client
)
// Ideally, serve will be the only public identifier exposed by the api package
func Serve(appConf *config.Config, dbConn *pgx.Conn) {
func Serve(appConf *config.Config, dbPool *pgxpool.Pool) {
// Config must be passed into the server
conf = appConf
// We maintain a single database connection for the server
conn = dbConn
// We maintain a single database pool for the server
pool = dbPool
// Plaid environment should be development in production
// developer keys support up to 100 live items


+ 2
- 2
api/auth.go View File

@ -46,7 +46,7 @@ func signUp(loginVals *Login) error {
return err
}
_, err = conn.Exec(
_, err = pool.Exec(
context.Background(),
"INSERT INTO users (email, password) VALUES ($1, $2)",
loginVals.Email,
@ -86,7 +86,7 @@ func authenticator(c *gin.Context) (interface{}, error) {
var userId int64
var userPassword string // Hashed password from db
err := conn.QueryRow(
err := pool.QueryRow(
context.Background(),
"SELECT id, password FROM users WHERE email=$1",
loginVals.Email,


+ 3
- 3
api/banking.go View File

@ -16,7 +16,7 @@ func createPlaidItem(userId int64, publicToken *PlaidPublicToken) error {
return err
}
_, err = conn.Exec(
_, err = pool.Exec(
context.Background(),
"INSERT INTO plaid_items (item_id, user_id, access_token) VALUES ($1, $2, $3)",
accessTokenResp.ItemID,
@ -30,7 +30,7 @@ func createPlaidItem(userId int64, publicToken *PlaidPublicToken) error {
func userHasPlaidItems(userId int64) (bool, error) {
var hasItems bool
err := conn.QueryRow(
err := pool.QueryRow(
context.Background(),
"SELECT EXISTS (SELECT item_id FROM plaid_items WHERE user_id=$1)",
userId,
@ -46,7 +46,7 @@ func userHasPlaidItems(userId int64) (bool, error) {
func userAccessToken(userId int64) (string, error) {
var accessToken string
err := conn.QueryRow(
err := pool.QueryRow(
context.Background(),
"SELECT access_token FROM plaid_items WHERE user_id=$1",
userId,


+ 3
- 3
api/user.go View File

@ -19,7 +19,7 @@ type UserPassword struct {
func getUserSettings(userId int64) (UserSettings, error) {
var userSettings UserSettings
err := conn.QueryRow(
err := pool.QueryRow(
context.Background(),
"SELECT email, home_latitude::text, home_longitude::text FROM users WHERE id=$1",
userId,
@ -33,7 +33,7 @@ func getUserSettings(userId int64) (UserSettings, error) {
}
func updateUserSettings(userId int64, userSettings UserSettings) error {
_, err := conn.Exec(
_, err := pool.Exec(
context.Background(),
"UPDATE users SET email=$2, home_latitude=$3, home_longitude=$4 WHERE id=$1",
userId,
@ -56,7 +56,7 @@ func updateUserPassword(userId int64, userPassword UserPassword) error {
return err
}
_, err = conn.Exec(
_, err = pool.Exec(
context.Background(),
"UPDATE users SET password=$2 WHERE id=$1",
userId,


+ 1
- 1
api/weather.go View File

@ -83,7 +83,7 @@ func homeForecast(userId int64) (Weather, error) {
var latitude string
var longitude string
err := conn.QueryRow(
err := pool.QueryRow(
context.Background(),
"SELECT home_latitude::text, home_longitude::text FROM users WHERE id=$1",
userId,


+ 3
- 3
db/db.go View File

@ -2,17 +2,17 @@ package db
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
var (
Conn *pgx.Conn
Pool *pgxpool.Pool
)
// Takes a standard postgresql database url, including credentials, etc.
func Connect(url string) error {
var err error
Conn, err = pgx.Connect(context.Background(), url)
Pool, err = pgxpool.Connect(context.Background(), url)
if err != nil {
return err
}


+ 1
- 0
go.sum View File

@ -124,6 +124,7 @@ github.com/jackc/pgx/v4 v4.6.0 h1:Fh0O9GdlG4gYpjpwOqjdEodJUQM9jzN3Hdv7PN0xmm0=
github.com/jackc/pgx/v4 v4.6.0/go.mod h1:vPh43ZzxijXUVJ+t/EmXBtFmbFVO72cuneCT9oAlxAg=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.0 h1:musOWczZC/rSbqut475Vfcczg7jJsdUQf0D6oKPLgNU=
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=


+ 1
- 1
me.go View File

@ -22,5 +22,5 @@ func main() {
// For now, the api requires a reference to the configuration and the db.
// Ideally, the relevant variables of the configuration would be passed individually.
api.Serve(conf, db.Conn)
api.Serve(conf, db.Pool)
}

Loading…
Cancel
Save