Skip to content
This repository has been archived by the owner on Apr 21, 2024. It is now read-only.

Commit

Permalink
fix: use pgx.Pool to avoid busy connections
Browse files Browse the repository at this point in the history
  • Loading branch information
GetPsyched committed Oct 9, 2023
1 parent 75da5fd commit 5b911cb
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions database/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"path/filepath"
"strings"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)

func Init(conn *pgx.Conn) {
func Init(conn *pgxpool.Pool) {
filenames := []string{"announcement", "student", "faculty", "guild", "club", "course"}
script := []string{}
for _, filename := range filenames {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
Expand Down
5 changes: 3 additions & 2 deletions handlers/announcement.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/net/html"
)

Expand Down Expand Up @@ -219,7 +220,7 @@ func parseDate(date string) (parsedDate pgtype.Date, err error) {
// some formatting
//
// TODO: try to use sqlc or some other intermediate to store this query
func FetchAnnouncements(conn *pgx.Conn) {
func FetchAnnouncements(conn *pgxpool.Pool) {
insert_query := query_prefix
ctx := context.Background()
queries := query.New(conn)
Expand Down Expand Up @@ -263,7 +264,7 @@ func FetchAnnouncements(conn *pgx.Conn) {
// GetAnnouncements returns all the announcements stored in database
//
// It is a wrapper function around
func GetAnnouncements(conn *pgx.Conn) http.HandlerFunc {
func GetAnnouncements(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
27 changes: 14 additions & 13 deletions handlers/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gorilla/mux"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)

type Club struct {
Expand All @@ -32,7 +33,7 @@ type Faculty struct {
}

// CreateClubFaculty creates a new faculty incharge for a group.
func CreateClubFaculty(conn *pgx.Conn) http.HandlerFunc {
func CreateClubFaculty(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -72,7 +73,7 @@ func CreateClubFaculty(conn *pgx.Conn) http.HandlerFunc {
}

// CreateClubMember adds a new member to a club.
func CreateClubMember(conn *pgx.Conn) http.HandlerFunc {
func CreateClubMember(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)

Expand Down Expand Up @@ -111,7 +112,7 @@ func CreateClubMember(conn *pgx.Conn) http.HandlerFunc {
}

// CreateClubSocial adds a new social media handle of a group.
func CreateClubSocial(conn *pgx.Conn) http.HandlerFunc {
func CreateClubSocial(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -147,7 +148,7 @@ func CreateClubSocial(conn *pgx.Conn) http.HandlerFunc {
}

// DeleteClubFaculty deletes an existing faculty incharge of a group.
func DeleteClubFaculty(conn *pgx.Conn) http.HandlerFunc {
func DeleteClubFaculty(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -174,7 +175,7 @@ func DeleteClubFaculty(conn *pgx.Conn) http.HandlerFunc {
}

// DeleteClubMember deletes an existing member of a club.
func DeleteClubMember(conn *pgx.Conn) http.HandlerFunc {
func DeleteClubMember(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)

Expand Down Expand Up @@ -207,7 +208,7 @@ func DeleteClubMember(conn *pgx.Conn) http.HandlerFunc {
}

// DeleteClubSocial deletes an existing social media handle of a group.
func DeleteClubSocial(conn *pgx.Conn) http.HandlerFunc {
func DeleteClubSocial(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -232,7 +233,7 @@ func DeleteClubSocial(conn *pgx.Conn) http.HandlerFunc {
//
// This handler takes in a name argument which is first
// checked as an alias and then as the name of a group.
func GetClub(conn *pgx.Conn) http.HandlerFunc {
func GetClub(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -254,7 +255,7 @@ func GetClub(conn *pgx.Conn) http.HandlerFunc {
}

// GetClubs retrieves the group details from the database.
func GetClubs(conn *pgx.Conn) http.HandlerFunc {
func GetClubs(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -273,7 +274,7 @@ func GetClubs(conn *pgx.Conn) http.HandlerFunc {
}

// GetClubFaculty retrieves the management faculty of a group from the database.
func GetClubFaculty(conn *pgx.Conn) http.HandlerFunc {
func GetClubFaculty(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -289,7 +290,7 @@ func GetClubFaculty(conn *pgx.Conn) http.HandlerFunc {
}

// GetClubSocials retrieves the social media links of a group from the database.
func GetClubSocials(conn *pgx.Conn) http.HandlerFunc {
func GetClubSocials(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -305,7 +306,7 @@ func GetClubSocials(conn *pgx.Conn) http.HandlerFunc {
}

// ReadClubMembers retrieves the members of a club.
func ReadClubMembers(conn *pgx.Conn) http.HandlerFunc {
func ReadClubMembers(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)

Expand All @@ -331,7 +332,7 @@ func ReadClubMembers(conn *pgx.Conn) http.HandlerFunc {
}

// UpdateClubMember updates a club member's details.
func UpdateClubMember(conn *pgx.Conn) http.HandlerFunc {
func UpdateClubMember(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)

Expand Down Expand Up @@ -370,7 +371,7 @@ func UpdateClubMember(conn *pgx.Conn) http.HandlerFunc {
}

// UpdateClubSocials updates the link of a social media handle for a group.
func UpdateClubSocials(conn *pgx.Conn) http.HandlerFunc {
func UpdateClubSocials(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
7 changes: 4 additions & 3 deletions handlers/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/google/go-github/github"
"github.com/gorilla/mux"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v2"
)
Expand All @@ -36,7 +37,7 @@ type Course struct {
Specifics []BranchSpecifics
}

func CreateCourse(conn *pgx.Conn) http.HandlerFunc {
func CreateCourse(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -89,7 +90,7 @@ func CreateCourse(conn *pgx.Conn) http.HandlerFunc {
}

// GetCourse is a handler for retrieving a single course via the `code` argument.
func GetCourse(conn *pgx.Conn) http.HandlerFunc {
func GetCourse(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -106,7 +107,7 @@ func GetCourse(conn *pgx.Conn) http.HandlerFunc {

// GetCourses is a handler for retrieving all the courses matching the given
// query parameters. It outputs all the courses if no parameter is passed.
func GetCourses(conn *pgx.Conn) http.HandlerFunc {
func GetCourses(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
7 changes: 4 additions & 3 deletions handlers/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"github.com/gorilla/mux"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)

func GetDiscordLinkStatus(db *pgx.Conn) http.HandlerFunc {
func GetDiscordLinkStatus(db *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(db)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -33,7 +34,7 @@ func GetDiscordLinkStatus(db *pgx.Conn) http.HandlerFunc {
}

// GetHostels retrieves all the hostels and their meta data from the database.
func GetHostels(conn *pgx.Conn) http.HandlerFunc {
func GetHostels(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -43,7 +44,7 @@ func GetHostels(conn *pgx.Conn) http.HandlerFunc {
}

// GetStudent retrieves a single student's details based on their roll number, email, or Discord ID.
func GetStudent(conn *pgx.Conn) http.HandlerFunc {
func GetStudent(conn *pgxpool.Pool) http.HandlerFunc {
ctx := context.Background()
queries := query.New(conn)

Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import (

"github.com/go-co-op/gocron"
"github.com/gorilla/mux"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/rs/cors"
)

type server struct {
conn *pgx.Conn
conn *pgxpool.Pool
router *mux.Router
}

// NewServer returns a new app instance.
func NewServer() *server {
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
conn, err := pgxpool.New(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatalln("Unable to connect to database:\n", err)
}
Expand Down

0 comments on commit 5b911cb

Please sign in to comment.