Skip to content

Commit

Permalink
Reduce DB connections leaks
Browse files Browse the repository at this point in the history
Attempt to fix the MaxConnection test bug.
Relates to #68.
  • Loading branch information
Jogo27 committed May 26, 2021
1 parent c78a999 commit 6363c12
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func RepeatDeadlocked(ctx context.Context, opts *sql.TxOptions, fct func(tx *sql
if !ok || !errors.As(err, &mySqlError) || mySqlError.Number != 1213 {
panic(exc)
}
log.Println("SQL error 1213. Restarting transaction.")
}()

fct(tx)
Expand Down
1 change: 1 addition & 0 deletions main/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (self *createPollChecker) Check(t *testing.T, response *http.Response, requ

// Check Alternatives
rows, err := db.DB.Query(qCheckAlternative, pollSegment.Id)
defer rows.Close()
mustt(t, err)
for id, alt := range query.Alternatives {
if !rows.Next() {
Expand Down
3 changes: 2 additions & 1 deletion main/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func DeleteHandler(ctx context.Context, response server.Response, request *serve
)

rows, err := db.DB.QueryContext(ctx, qTitle, segment.Id, request.User.Id)
defer rows.Close()
must(err)
if !rows.Next() {
panic(server.NewHttpError(ImpossibleStatus, ImpossibleMessage, ""))
Expand All @@ -66,9 +67,9 @@ func DeleteHandler(ctx context.Context, response server.Response, request *serve
panic(server.NewHttpError(http.StatusInternalServerError, server.InternalHttpErrorMsg,
"Two polls witht the same Id"))
}
rows.Close()

rows, err = db.DB.QueryContext(ctx, qParticipants, segment.Id)
defer rows.Close()
must(err)
for rows.Next() {
var uid uint32
Expand Down
12 changes: 12 additions & 0 deletions main/poll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package main

import (
"context"
"fmt"
"net/http"
"runtime"
"strconv"
"testing"

Expand Down Expand Up @@ -336,7 +338,15 @@ func (self *pollTest) GetName() string {
return self.Name
}

func stats(ctx string) {
stats := db.DB.Stats()
fmt.Printf("%s -- open %d, use %d, idle %d, goroutines %d\n", ctx,
stats.OpenConnections, stats.InUse, stats.Idle, runtime.NumGoroutine())
}

func (self *pollTest) Prepare(t *testing.T) {
stats("Before Prepare")

if !self.Sequential {
t.Parallel()
}
Expand Down Expand Up @@ -419,6 +429,7 @@ func (self *pollTest) Prepare(t *testing.T) {
if checker, ok := self.Checker.(interface { Before(*testing.T) }); ok {
checker.Before(t)
}
stats("After Prepare")
}

func (self *pollTest) GetRequest(t *testing.T) *srvt.Request {
Expand Down Expand Up @@ -450,6 +461,7 @@ func (self *pollTest) Check(t *testing.T, response *http.Response, request *serv
} else {
t.Errorf("Checker is not an srvt.Checker")
}
stats("After Check")
}

func (self *pollTest) Close() {
Expand Down
1 change: 1 addition & 0 deletions main/unlogged.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func UnloggedFromHash(ctx context.Context, hash uint32) (user server.User, err e
if err != nil {
return
}
rows.Close()

user.Hash = hash
user.Logged = false
Expand Down

0 comments on commit 6363c12

Please sign in to comment.