Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions api/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
)

type DBSingleton struct {
Expand All @@ -25,18 +27,24 @@ var once sync.Once

func ConnectDB() *mongo.Client {
once.Do(func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(GetEnvMongoURI()))
clientOptions := options.Client().
ApplyURI(GetEnvMongoURI()).
SetMinPoolSize(5).
SetMaxPoolSize(100).
SetMaxConnIdleTime(10 * time.Minute).
SetConnectTimeout(10 * time.Second).
SetReadPreference(readpref.SecondaryPreferred()).
SetReadConcern(readconcern.Local())

client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
log.Fatalf("Unable to create MongoDB client")
}

defer cancel()

// ping the database
err = client.Ping(ctx, nil)
if err != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = client.Ping(ctx, nil); err != nil {
log.Fatalf("Unable to ping database")
}

Expand Down Expand Up @@ -115,18 +123,17 @@ var clubOnce sync.Once

func ConnectClubsDB() *sql.DB {
clubOnce.Do(func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

db, err := sql.Open("pgx", GetClubsDBUri())
if err != nil {
log.Panic("Unable to connect to clubs database.")
}

// ping the database
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// ping the database
err = db.PingContext(ctx)
if err != nil {
if err = db.PingContext(ctx); err != nil {
log.Panic("Unable to ping database")
}

Expand Down
2 changes: 1 addition & 1 deletion api/controllers/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,4 @@ func buildSectionPipeline(endpoint string, sectionQuery bson.M, paginate map[str
}

return append(append(append(baseStages, lookupStages...), replaceStages...), paginateStages...)
}
}
2 changes: 1 addition & 1 deletion api/schema/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ type AcademicCalendar struct {
MidtermsDue string `bson:"midterms_due" json:"midterms_due"`
UniversityClosings [][]string `bson:"university_closings" json:"university_closings"`
NoClasses [][]string `bson:"no_classes" json:"no_classes"`
URL string `bson:"url" json:"url"`
URL string `bson:"url" json:"url"`
}
type AcademicCalendarSession struct {
Name string `bson:"name" json:"name"`
Expand Down
4 changes: 4 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func main() {
Dsn: "https://530f8e39f757b71ab26ad1aa12e17a4d@o4504918397353984.ingest.us.sentry.io/4509397160493056",
TracesSampleRate: 1.0,
EnableTracing: true,
IgnoreErrors: []string{
"tls: internal error",
"socket was unexpectedly closed",
},
}); err != nil {
log.Printf("Sentry initialization failed: %v\n", err)
}
Expand Down
Loading